Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make vim do syntax highlighting on C++ headers that don't have extensions?

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.

like image 808
Jason R Avatar asked May 14 '12 13:05

Jason R


People also ask

How do I enable syntax highlighting in Vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

Does vim support syntax highlighting?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.

How do I highlight keywords in vim?

Press 1 to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group hl1 is used. Press 2 for highlight hl2 , 3 for highlight hl3 , etc. Press 0 to remove all highlights from the current visually selected text, or the current word.


1 Answers

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

like image 119
Amadan Avatar answered Sep 19 '22 10:09

Amadan