在文件中逐块更改文件类型(或其他 Vim 设置)?

发布于 2024-11-26 15:18:01 字数 276 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷爱 2024-12-03 15:18:02

您可以为单个文件设置多个文件类型:

:set ft=perl.css

这样您就可以获得两种语言的全能补全(以及片段,如果适用)和半正确的突出显示:如果您将 CSS 规则放在引号中,它将突出显示为字符串。

我刚刚(昨天)发现了一个很酷的插件,其灵感来自于 Emacs 功能,名为 NrrwRgn< /a>.它允许您选择代码的“区域”,例如 Perl 文件的 CSS 部分,并在您 :set ft=css 的临时窗口中对其进行编辑。每次保存都会反映在原始窗口中。在处理充满 PHP/HTML/JS/CSS 的 PHP 模板时非常有用。

You can have multiple filetypes set for a single file :

:set ft=perl.css

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.

新一帅帅 2024-12-03 15:18:02

如果您不想提出自己的混合突出显示规则:

:set syntax=perl

那么当您想要编辑 css 时,

:set syntax=css

为了更容易,您可以在 .vimrc 中为两者映射一些键,这将方便切换回来和四。

map <F3> :execute "set syntax=perl" <CR>
map <F4> :execute "set syntax=css" <CR>

If you don't want to come up with your own hybrid highlighting rules:

:set syntax=perl

then when you want to edit css,

:set syntax=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.

map <F3> :execute "set syntax=perl" <CR>
map <F4> :execute "set syntax=css" <CR>
想挽留 2024-12-03 15:18:02

我不熟悉 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 中添加这样的内容:

syntax on

unlet b:current_syntax
syntax include @CSS syntax/css.vim
syntax region cssSnip matchgroup=Snip start=+<<['"]CSS['"].*;\s*$+ end=+^\s*CSS$+ contains=@CSS

hi link Snip SpecialComment

CSS 将会突出显示,只要你这样写:

my $css = <<"CSS";
  a {
    background-color: blue;
  }
CSS

如果你想使用注释作为分隔符,您可以修改上面的代码片段,特别是 startend 模式。例如,这样:

syntax region cssSnip matchgroup=Snip start=+^\s*# -- CSS --\s*$+ end=+^\s*# -- END CSS --\s*$+ contains=@CSS

让您在特定评论中突出显示,如下所示:

# -- CSS --
  a {
    background-color: blue;
  }
# -- END CSS --

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:

syntax on

unlet b:current_syntax
syntax include @CSS syntax/css.vim
syntax region cssSnip matchgroup=Snip start=+<<['"]CSS['"].*;\s*$+ end=+^\s*CSS$+ contains=@CSS

hi link Snip SpecialComment

The CSS will be highlighted, as long as you write it like this:

my $css = <<"CSS";
  a {
    background-color: blue;
  }
CSS

If you want to use comments as delimiters instead, you could hack on the above snippet, particularly the start and end patterns. For example, this:

syntax region cssSnip matchgroup=Snip start=+^\s*# -- CSS --\s*$+ end=+^\s*# -- END CSS --\s*$+ contains=@CSS

Gets you highlighting within specific comments like so:

# -- CSS --
  a {
    background-color: blue;
  }
# -- END CSS --
紙鸢 2024-12-03 15:18:02

您可以使用我的 SyntaxRange 插件 来实现这一点。假设您使用 @cssbegin@ / @cssend@ 分隔 CSS 行,请将以下内容放入 ~/.vim/after/syntax/perl/perl_cssinclude.vim :

:call SyntaxRange#Include('^@cssbegin@', '^@cssend@', 'css', 'NonText')

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:

:call SyntaxRange#Include('^@cssbegin@', '^@cssend@', 'css', 'NonText')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文