4

I want to write c++ code in vim using lisp indentation style (closing curly braces on the same line as last statement) but I can't figure out how to configure indentation to use that style.

Is there any workaround to use and indent end curly braces at the end of block (rather than at the next line)?

Example:

int func() {
    print("Hello\n");}

EDIT:

I'd like vim to recognise this formatting and indent next line properly. Normally, Vim doesn't recognise the curly brace at the end of line as end of block and use the same indent as previous line.

1
  • 1
    This has nothing to do with indentation. You're typing the { and } so just type them in the correct places. On the other hand, if you want something to reformat your code into that style, then you may want to look at the (poorly named) indent program.
    – jamessan
    Commented Nov 17, 2015 at 19:09

1 Answer 1

1

I'd like vim to recognise this formatting and indent next line properly. Normally, Vim doesn't recognise the curly brace at the end of line as end of block and use the same indent as previous line

Vim does recognize the curly brace at the end of the line if it is configured to do so. The most relevant option on this regard is the 'autoindent' (see :help autoindent). I tried your example in a .cpp file and Vim adjusts the indent of the next line correctly.

The Vim FAQ 14.10 explain how to disable it, but you can use it to understand how to configure it:

14.10. How do I turn off the automatic indentation of text?

By default, the automatic indentation of text is not turned on. Check the configuration files (.vimrc, .gvimrc) for settings related to indentation. Make sure the ":filetype indent on" command is not present. If it is present, remove it. Also, depending on your preference, you may also want to check the value of the 'autoindent', 'smartindent', 'cindent' and 'indentexpr' options and turn them off as needed.

For more information, read

:filetype-indent-off
'autoindent'
'smartindent'
'cindent'
'indentexpr'

Edit:

At first my understanding was that you were unable to get any automatic indentation; from your comment I get that you want to change the default behavior of 'cindent'. You can do that by changing a number of options:

27.2. How do I configure the indentation used for C/C++ files?

You can configure the Vim C indentation by modifying the value of the 'cinoptions', 'cinkeys' and 'cinwords' options.

For more information, read

'cindent'
'cinoptions'
'cinkeys'
'cinwords'
C-indenting
cinoptions-values
'smartindent'

The documentation on each entry is quite extensive, as it allows for very flexible configuration. After a quick search and tests I think it is not possible to achieve what do you want by using 'autoindent', 'cindent', or 'smartindent', thus you will have to use 'indentexpr'. From :help C-indenting:

There are in fact four main methods available for indentation, each one
overrides the previous if it is enabled, or non-empty for 'indentexpr':
'autoindent'    uses the indent from the previous line.
'smartindent'   is like 'autoindent' but also recognizes some C syntax to
                increase/reduce the indent where appropriate.
'cindent'       Works more cleverly than the other two and is configurable to
                different indenting styles.
'indentexpr'    The most flexible of all: Evaluates an expression to compute
                the indent of a line.  When non-empty this method overrides
                the other ones.  See indent-expression.

Besides giving many details, the :help 'indentexpr' also suggests using the files on $VIMRUNTIME/indent directory as a starting point. The file $VIMRUNTIME/indent/ada.vim shows that you can set 'indentexpr' to call a function, and inside that function you can parse the current and surrounding lines before returning the correct indentation number.

You could use some existing C/C++ indentation plugin in vim.org as a base, such as Indent file for Google C++ Coding Style.

2
  • I've checked autoindent documentation but it only copies indentation from previous line. But I want Vim to recognize end curly brace and decrease indentation on next line.
    – jendas
    Commented Nov 18, 2015 at 17:32
  • @jendas now I think I understand your question; I've updated my answer
    – mMontu
    Commented Nov 18, 2015 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.