我可以让vim做没有扩展的C ++头文件高亮吗? [英] Can I make vim do syntax highlighting on C++ headers that don't have extensions?

查看:266
本文介绍了我可以让vim做没有扩展的C ++头文件高亮吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,一堆C ++头文件遵循标准的C ++头命名约定;也就是说,一个名为Foo的类将在一个名为 Foo 的文件中声明,而不是 Foo.h Foo.hh 。有没有一个好的方法配置vim只做这些文件的语法高亮?一个稍微不悦的回退将是对所有没有扩展名的文件启用C ++风格的高亮显示。我不知道是否有任何更复杂的方法来检测文件类型,而不是只依赖于其扩展名。

解决方案

您可以使用 modeline 功能。 Modelines允许您在文件的第一行/最后几行中的注释内设置某些选项。



这使它成为编码指南的参数设置的好地方,折叠。出于安全原因,某些选项不能设置。有关详细信息,请参见文档



将它放在文件的顶部或底部:

  / * vim:setlocal ft = cpp:* / 

编辑:更多细节,由注释提示:):



只有在启用modeline时才会工作。在正常情况下,它应该是默认。要确保已启用,或更改检测到的区域大小,请在 .vimrc modeline 选项c $ c>:

  set modeline = 5 

将确保在每个文件的前五行或后五行中检测到类似上面引用的行。



在模型中, setlocal 表示为文件加载的缓冲区设置选项。 ft 选项,也称为 filetype ,是什么决定语法高亮语言。 cpp 是C ++文件使用的值。



编辑2:没有模式,更多的工作,如果你可以识别一个魔术模式:

  au BufRead *如果search('MagicPattern','nw' | setlocal ft = cpp | endif 

含义:每次打开一个文件时,检查MagicPattern 在那里。如果是,将其视为C ++。模式参数是在vim方言的正则表达式;有关详细信息,请检查帮助模式


I have a project with a bunch of C++ header files that follow the standard C++ header naming convention; that is, a class called Foo would be declared in a file called Foo, not Foo.h or Foo.hh. Is there a good way to configure vim to do syntax highlighting for these files only? A slightly-less-pleasing fallback would be to enable C++-style highlighting for all files that don't have an extension. I'm not sure if there's any more sophisticated way to detect the type of file instead of relying solely on its extension.

解决方案

You can use the modeline feature for this. Modelines allow you to set certain options from within a comment in the first/last few lines of your file.

This makes it a great place to set parameters for coding guidelines, folding. Some options cannot be set for security reasons. See the documentation for more information.

Put this at the top or bottom of the file:

/* vim: setlocal ft=cpp: */

EDIT: More details, prompted by the comments :) :

It will only work if modeline is enabled. In normal circumstances it should be by default. To make sure it is enabled, or to change the size of the area it is detected in, set the modeline option in your .vimrc:

set modeline=5

will make sure the line like the one quoted above will be detected in the first five or the last five lines of each file.

Inside the modeline, setlocal means to set options for the buffer the file is loaded in. The ft option, also known as filetype, is what determines the syntax highlighting language. The value cpp is the one that is used by C++ files.

EDIT 2: Without the modeline, with a bit more work, if you can identify a magic pattern:

au BufRead * if search('MagicPattern', 'nw') | setlocal ft=cpp | endif

Meaning: Every time you open a file, check if "MagicPattern" is in there. If it is, treat it as C++. The pattern argument is in vim dialect of regular expressions; check help pattern for details.

这篇关于我可以让vim做没有扩展的C ++头文件高亮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆