How to Make Vim Do Syntax Highlighting on C++ Headers That Don't Have Extensions

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

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.

Detect filetype in vim without valid file extension

This was an interesting puzzle. :)

aug SmartDiffType
au!
au VimEnter * :if &diff && len(&ft) | call setwinvar(2/winnr(),'&ft',&ft) | elseif &diff | let &ft=getwinvar(2/winnr(),'&ft') | endif
aug END

Notes:

  • Of the 4 lines above, you only need the au VimEnter line, but it is generally a good practice to put autocommands in some autocommand group with a reset (au!) at the top.
  • Autocommand on VimEnter because otherwise diff or the windows are not properly intialized yet
  • vimdiff might have been triggered with the old file on the right or the left of the split, so we consider both cases.
  • The 2/winnr() is a math trick to flip between 1 and 2 (2/2 = 1, 2/1=2)

class & function names highlighting in Vim

Interestingly, the syntax highlighters in VIM don't support applying a syntax to identifiers or function names - at least not the syntax highlighters for C and C++. So, even if you do:

:hi Function guifg=red

or

:hi Identifier guifg=red

it doesn't give these a color. I just seems to be not much more than keywords and constants for these languages.

Here, someone has started extending the cpp syntax file to support method names. It's a start I guess.
http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition

Syntax highlighting not looking at shebang line on known filetypes

Usually, the static file pattern comparison (in filetype.vim) is run before the detection based on contents (in scripts.vim), but since it's all Vimscript, nothing prevents you from messing with that order.

If this is just about the .cfg extension and Perl scripts, put the following into ~/.vim/filetype.vim:

if exists('did_load_filetypes')
finish
endif

augroup filetypedetect
autocmd BufNewFile,BufRead *.cfg if getline(1) =~# '^#!/usr/bin/perl\>' | setf perl | endif
augroup END

Detecting file type for C++ standard headers with vim

n.m's answer does the trick, but this is better:

au BufRead * if search('\M-*- C++ -*-', 'n', 1) | setlocal ft=cpp | endif

The extra argument to search is the stopline, and ensures that this rule will only be applied to files with the pattern in line 1.

This is important because, without the stopline any files that contain the pattern, including your vimrc, will satisfy the match and potentially be highlighted using the wrong syntax rules.

Also, using stopline the w flag is unnecessary.

Look at :help search for more information.

Add syntax highlighting to certain file extensions for VIM or GVIM by default

Add an auto-command to your .vimrc to do that for you:

autocmd BufNewFile,BufRead *.v,*.vs set syntax=verilog

Set vim filetype script for c++ headers only

In your ~/.vim/ftplugin/cpp.vim:

if expand('%:e') ==? 'hpp'
" your script here
endif

Better yet:

if expand('%:e') =~? '\v^h%(pp|h|\+\+|xx)?$'
" your script here
endif

C.Vim plugin, adding Automatic Header to *.h file

Add this line to your .vimrc file:

let g:C_SourceCodeExtensions  = 'h cc cp cxx cpp CPP c++ C i ii'

The important part here to notice is the h at the begining of the string. For more information on why this works see :h csupport-comm-frame.

Note: in order to see the help documentation, make sure to run :helptags ~/.vim/doc after installing the plugin. Otherwise the documentation that comes with the plugin will not be installed.

vim:filetype equivalent in vscode

I know this question has been around for quite some time now but I thought I should share that this is now possible through a third party extension:
https://marketplace.visualstudio.com/items?itemName=chrislajoie.vscode-modelines

I have not tested this myself but it has a lot of installs with absolutely no bad press so it should work properly.

Repo: https://github.com/ctlajoie/vscode-modelines

Opening the header file to a C/C++ source file with vim from multiple directories and multiple extension

I recommend using the FSwitch plugin. https://github.com/derekwyatt/vim-fswitch
This does exactly what you need out of the box. It is better than a.vim in more than one way, being a rewrite of the idea behind a.vim.

The link you posted presents it as a solution, too.
I have just installed it to my vim configuration and it does its job well.

Enjoy.



Related Topics



Leave a reply



Submit