3

I'm using basically the formatting of the Google C++ Style Guide. How can I configure neovim to use these settings for C++? On the other hand, for pure C, I would like it to use the BSD style.

Currently, I just type code and then run clang-format (through the google codefmt plugin), which takes care of formatting. But it would be nicer to automatically get the correct indentation levels etc. when I hit return after an opening bracket.

eof
  • 51
  • 1
  • 3

1 Answers1

2

You can configure different styles of C/C++ indenting by setting 'cindent' and 'cinoptions'.

See :help cinoptions-values for more details on what can be configured and :help C-indenting for information on C indenting in general.

You can have different settings for C and C++ by placing your configuration in two locations:

In ~/.vim/after/ftplugin/c.vim:

set cindent
set cinoptions=<the options you want for C>

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

set cindent
set cinoptions=<the options you want for C++>

You might also like to take a look at this question about using clang-format for the = command.

Vim also allows you to set up completely custom indenting using an indent expression. (See :help 'indentexpr') This is what the script you mention in the comments does. You can install that for CPP by renaming the file and placing it in the location:

~/.vim/indent/cpp.vim

You can find more details at :help 30.3 and :help indent-expression.

Rich
  • 31,891
  • 3
  • 72
  • 139
  • Are these put in the same place in neovim? There's also the following file: https://github.com/vim-scripts/google.vim/blob/master/indent/google.vim do you know how to use it? – eof Jan 25 '18 at 08:19
  • This doesn't seem to be working. So I put the google.vim file into ~/.config/nvim/after/ftplugin/cpp and when starting Neovim in debug mode, I see the following in the logs: line 17: sourcing "/home/eof/.config/nvim/after/ftplugin/cpp/google.vim" finished sourcing /home/eof/.config/nvim/after/ftplugin/cpp/google.vim However, if I type int main(void) { and hit enter in a buffer named test.cc (I see cpp in airline, so it's in the right mode), the cursor jumps to column 8 instead of column 2. – eof Jan 25 '18 at 08:59
  • @eof See my updated answer. – Rich Jan 25 '18 at 09:53