Is There a C++11 Syntax File for Vim

Is there a C++11 syntax file for vim?

There is now a C++11 script from http://www.vim.org/scripts/script.php?script_id=3797, which no longer mark the braces inside parenthesis as error.

vim and c++11 lambda: auto indentation

Vim 7.4 now has an jN cinoption for "Indent Java anonymous classes correctly." (:help java-cinoptions) This improves indenting behavior for C++11's lambdas.

With these options (put in your ~/.vim/after/ftplugin/cpp.vim):

setlocal cindent cino=j1,(0,ws,Ws

And if I move your for loop's opening brace to the same line (otherwise it's crazy) then vim indents your code like this:

MyLoop( [](int a, int b){
{
::i++;
for (;;) {
SomeFunc();
}
cout << "Result: " << a*b<<endl;
}
});

It doesn't give the hanging indent that you want either. If you move the initial opening brace it its own line, then you get your desired hanging indent.

For all the options, see :help cinoptions-values.


If you want at smarter indenting program, this user recommends set equalprg=clang-format to use ClangFormat so =ip will indent the current paragraph. This won't make vim correctly indent as you type (you need to setup indentexpr for that and that's quite complicated).

There's also a vim plugin that seems to do the same as setting equalprg, but with more code. Not sure if it's any better. It's supposed to be an alternative to clang-format.py (from Cyprian Guerra's answer).

VI to recognize C++11 keywords

Someone already wrote some syntax files for C++11: http://www.vim.org/scripts/script.php?script_id=3797. It even makes initializer lists and lambdas work fine without vim thinking the curlies are errors.

I remember I had to manually add some missing keywords to it, but that's not terribly complicated (just grep the file for the other keywords and add new ones). My current setup is up on GitHub. If you are using vundle you can install it with Bundle 'rmartinho/vim-cpp11'.

How to apply syntax file for vim?

All you need to do is make sure the filetype is loaded properly. So if the syntax file is called cpp.vim make sure set ft? returns cpp. You should place it in $HOME/.vim/syntax. It should get loaded automatically. Assuming filetype on and syntax on is set.

The only way to make sure the syntax file isn't loaded properly is to use :scriptnames. Make sure the file isn't listed if it is your problem is something different then what you have described.

VIM: Certain .c files opening without syntax highlighting

Try to remove all files in ~/.vim/view (eventually make a backup before deleting it).

Syntax highlighting in vim

First, try :set background=dark, which will cause vim to change to a color scheme that works better for reading on a black background.

If that doesn't work well enough, you can create your own color scheme by following these directions: http://vim.wikia.com/wiki/Create_a_color_scheme_based_on_another

Vim 8.0 extended syntax highlighting for scheme

There seem to be three ways to do what I wanted, I needed to (try to) understand variables, autocmd and modelines

  1. Adding to the to the vimrc that will be used for Gambit-C files (i.e. vi -u {vimrc} [Gambit-C files] ) any one of the following

             let g:is_gambit=1  "" This works on all the buffers 

or

            :autocmd BufRead *.scm let b:is_gambit "" This works for all the buffers but is redundant given the above.

and placing the syntax file of the form

   if exists("b:is_gambit") || exists("is_gambit")
"syntax extensions
....."
endif

as set up above to ".vim/after/syntax/scheme.vim".

Note: Setting b:is_gambit in the -u {vimrc} only sets it for the current buffer and g:is_gambit means all open (scheme .i.e. ".scm" ) buffers (if b:is_gambit is set in the ".vim/syntax/scheme.vim" it works in all buffers though). For variables, b = current buffer and g = global.

or......


  1. Adding to the to the vimrc that will be used for Gambit-C files (i.e. vi -u {vimrc} [Gambit-C files] )

         :autocmd BufEnter *.scm source /path/to/file/gambitc.vim ""Note BufEnter not BufRead

Where gambitc.vim is the syntax file for Gambit-C.

Note: The gambitc.vim here only needs the extra syntax to be added to the scheme syntax

and......


  1. As explained properly here setting modelines in your general vimrc and placing the Gambit-C syntax file in ".vim/syntax/" under a different name (e.g ".vim/syntax/gambitc.vim") and sourcing the original scheme syntax by placing this in the file

           :source /usr/share/vim/vim80/syntax/scheme.vim

or whatever the path to the scheme syntax file is.

Then in every file you want to use the Gambit-C syntax type

            ;; vim: filetype=gambitc

i.e comment this line from the current language and then write the statement.

If there is anything wrong with any of my solutions please let me know, I personally prefer the second one as it involves less work when porting it to different machines, though I suspect using the "g:is_gambit" method is more efficient



Related Topics



Leave a reply



Submit