在文件中逐块更改文件类型(或其他 Vim 设置)?
我在 Vim 中编写了很多 Perl 程序。通常我只是将一些 CGI 脚本组合在一起,然后将 CSS 直接放入程序代码中。所以最近我问自己是否有可能在我的代码中的这些块周围放置一些 Vim 特定的注释,以便 vim 突出显示特定区域而不是 Perl 脚本,而是作为级联样式表。
当使用 Mojolicious 时,这也很方便,您可以将整个模板嵌入到数据区域中,但随后会丢失 HTML 的所有突出显示。
当然,我可以在文件类型/突出显示之间手动切换。但我想知道是否有更好的方法。
谢谢, 斯文
I program a lot of Perl in Vim. Often I just hack together some CGI script and put the CSS right into the program code. So lately I asked myself if it was possible, to put some Vim-specific comments around such blocks in my code, so that vim highlights the specific area not as a Perl script, but as a cascading stylesheet.
This also would be neat when working with Mojolicious where you can embed entire templates just into the DATA-area but lose all the highlighting of the HTML then.
Of course, I could switch manually between the filetypes/highlighting. But I wonder if there is better way.
Thanks,
Sven
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为单个文件设置多个文件类型:
这样您就可以获得两种语言的全能补全(以及片段,如果适用)和半正确的突出显示:如果您将 CSS 规则放在引号中,它将突出显示为字符串。
我刚刚(昨天)发现了一个很酷的插件,其灵感来自于 Emacs 功能,名为 NrrwRgn< /a>.它允许您选择代码的“区域”,例如 Perl 文件的 CSS 部分,并在您
:set ft=css
的临时窗口中对其进行编辑。每次保存都会反映在原始窗口中。在处理充满 PHP/HTML/JS/CSS 的 PHP 模板时非常有用。You can have multiple filetypes set for a single file :
With that you get omnicompletion (and snippets if applicable) for both languages and semi-correct highlighting: if you put your CSS rules in quotes it will be highlighted as a string.
I've just found (yesterday) a cool plugin inspired by an Emacs feature called NrrwRgn. It allows you to select a "region" of code, say the CSS part of your Perl file and edit it in a scratch window for which you
:set ft=css
. Each save is reflected in the original window. Very useful when dealing with PHP templates full of PHP/HTML/JS/CSS.如果您不想提出自己的混合突出显示规则:
那么当您想要编辑 css 时,
为了更容易,您可以在 .vimrc 中为两者映射一些键,这将方便切换回来和四。
If you don't want to come up with your own hybrid highlighting rules:
then when you want to edit css,
To make it easier, you could map some keys for both in your .vimrc, which would make it easy to toggle back and forth.
我不熟悉 Perl 以及 CSS 如何适应代码,但如果您使用heredocs,这里有一篇很好的文章处理类似的问题:http://blogs.perl.org/users/ovid/2011/06/syntax-highlight-your-sql-heredocs-in-vim.html
作者想要做什么在此处文档中突出显示 SQL,并用
SQL
分隔符标记。在你的情况下,你可以在.vim/after/ftplugin/perl.vim
中添加这样的内容:CSS 将会突出显示,只要你这样写:
如果你想使用注释作为分隔符,您可以修改上面的代码片段,特别是
start
和end
模式。例如,这样:让您在特定评论中突出显示,如下所示:
I'm not familiar with Perl and how the CSS fits within the code, but if you use heredocs, here's a nice article that deals with a similar problem: http://blogs.perl.org/users/ovid/2011/06/syntax-highlight-your-sql-heredocs-in-vim.html
What the author is trying to do is highlight SQL within heredocs marked with an
SQL
delimiter. In your case, you could put something like this in.vim/after/ftplugin/perl.vim
:The CSS will be highlighted, as long as you write it like this:
If you want to use comments as delimiters instead, you could hack on the above snippet, particularly the
start
andend
patterns. For example, this:Gets you highlighting within specific comments like so:
您可以使用我的 SyntaxRange 插件 来实现这一点。假设您使用
@cssbegin@
/@cssend@
分隔 CSS 行,请将以下内容放入~/.vim/after/syntax/perl/perl_cssinclude.vim :
You can use my SyntaxRange plugin for that. Assuming you delimit the CSS lines with
@cssbegin@
/@cssend@
, put the following into~/.vim/after/syntax/perl/perl_cssinclude.vim
: