Is it possible to format C++ code with VIM?

33,782

Solution 1

While vim is a true Swiss-knife I still prefer external tools for some jobs. This approach is some times much more intuitive and easy to remember than using the built-in equivalent.

In the case of indenting, I filter the whole file buffer through astyle. The astyle parameters are much easier to grasp in a couple of minutes, especially if you are not a vim guru. Also astyle provides much more flexibility in fine-tuning the output.

First install astyle:
# apt-get install astyle

Then inside vim:
:%!astyle (simple case - astyle default mode is C/C++)
or
:%!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level)
or
:1,40!astyle --mode=c --style=ansi (ansi C++ style, filter only lines 1-40)

Solution 2

you can do the following:

gg=G

Solution 3

I would highly recommend clang-format nowadays. It allows simple integration of clang-format into Vim, once you have clang-format installed:

http://clang.llvm.org/docs/ClangFormat.html#vim-integration

It is the only code beautifier that really understands your C++ code, and it is really intelligent to beautify the code more like a human being than a machine. E.g.:

void TestFunction(int argument1, int argument2,
                  int argument3);
void TestFunctionVeryLongName(int argument1,
                              int argument2,
                              int argument3);
void TestFunctionWithRidiculouslyLongName(
    int argument1, int argument2, int argument3);

Solution 4

Vim will definitely do this, although the results may not be perfect:

  1. First, select the entire file in visual mode: ggVG
  2. Then hit = to reindent everything.

You can learn more about the equal command with: :help =

Solution 5

There is a vim plugin that enables formatting on your code from within vim. It's called vim-autoformat and you can download it here:

https://github.com/vim-autoformat/vim-autoformat

It integrates external code-formatting programs into vim. For example, if you want to format C, C++, C# or Java code, you need to install the program astyle, and vim sets it as the format program automatically.

Share:
33,782
Open the way
Author by

Open the way

Updated on July 14, 2021

Comments

  • Open the way
    Open the way almost 3 years

    I am rather new to VIM. I got some source code and this is a mess. At a first sight I would like at least to get a clear and organised view of the code, so I like to get it rightly formatted, I mean indented depending on the depth of the functions and so.

    I wonder if it can be done with VIM, and otherwise which other commandline tools for that can you recommend.

    Thanks

  • Sarah
    Sarah about 14 years
    To clarify, =[motion] indents the region encompassed by the motion. gg moves to the beginning of the file and G moves to the end of the file.
  • Open the way
    Open the way about 14 years
    thanks for the info, but i prefer VIM because i have to debug code on remote machines
  • hasen
    hasen about 14 years
    or start with V to enter visual line mode, then move down with j to select all the lines you want to format, then hit =
  • Benoit
    Benoit about 13 years
    I don't like gg=G. Why? Because when you break long list with commas (argument list, long booleans) it will break manual alignment.
  • danihodovic
    danihodovic about 10 years
    Is it possible to map this to a key combination like ctrl+shift+F?
  • Tom Ozx
    Tom Ozx over 9 years
    You can also override Vim's indent operator (=) to use Astyle by setting equalprg to astyle (e.g: set equalprg=~/astyle\ --style=google)
  • Daniel
    Daniel over 7 years
    An alternative to the keybindings suggested in the clang-format manual is to use the equalprg binding in vim. This allows you to invoke clang-format with G=gg or other = indent options. Just put the following in your .vimrc file: autocmd FileType c,cpp setlocal equalprg=clang-format