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.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- 如何让 vim 支持 .cshtml 文件的代码高亮
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Auto-save in VIM as you type
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
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:
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
: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. Theft
option, also known asfiletype
, is what determines the syntax highlighting language. The valuecpp
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:
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; checkhelp pattern
for details.With default vim settings, add this to the top of a file to have vim pick up the filetype:
If you are in another language, adjust accordingly, e.g.:
modeline Versus modelines Clarification
In the answer, https://stackoverflow.com/a/10584645,
Should be:
See docs: https://stackoverflow.com/a/10584645. Specifically, modeline is a boolean enable flag that is on by default http://vimdoc.sourceforge.net/htmldoc/options.html#%27modeline%27, and modelines takes an integer argument (defaulting to 5 in any case) that sets the number of lines to be looked at if modeline is enabled http://vimdoc.sourceforge.net/htmldoc/options.html#%27modelines%27.
None of this is of interest to the OP, but I add it here for anyone who arrives from a search to remind themselves how to tell vim the filetype at the top of a file.