Take the 2-minute tour ×

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.


share|improve this question
add comment

7 Answers

up vote 33 down vote accepted

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++)
:%!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level)
:1,40!astyle --mode=c --style=ansi (ansi C++ style, filter only lines 1-40)

share|improve this answer
wow, this is really great, thanks a lot –  flow Mar 24 '10 at 10:56
Is it possible to map this to a key combination like ctrl+shift+F? –  dani-h Mar 27 at 12:00
add comment

you can do the following:

share|improve this answer
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. –  Sarah Mar 24 '10 at 10:54
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 = –  hasenj Mar 26 '10 at 7:40
I don't like gg=G. Why? Because when you break long list with commas (argument list, long booleans) it will break manual alignment. –  Benoit Feb 10 '11 at 9:17
thanks for this tip. I've found it to be very helpful. –  Nathan Ernst Nov 15 '11 at 22:53
= does not format code, it re-indents it. –  Brian Vandenberg Oct 2 '13 at 19:50
add comment

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:


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|improve this answer
add comment

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 =

share|improve this answer
add comment

I don't write C++ code, but I write some Java code.

Instead, Vim supports the formatting of some common languages. I have set up a short cut for me to format the whole code in the buffer. It will return to the line I just edited :)

" format the file
map <leader>fm gg=G'. 
share|improve this answer
add comment

A generic solution along the lines of m000's idea is to use UniversalIndentGUI as an external tool.

share|improve this answer
thanks for the info, but i prefer VIM because i have to debug code on remote machines –  flow Mar 24 '10 at 21:12
add comment

Just had to solve this exact problem, so I thought I'd contribute to save others some time.

You can use gg=G to indent your code. But things get hard to understand the moment you want to tweak how that auto-indenting happens. Therefore, if you only care that errant whitespace is removed and don't really care about formatting style, gg=G is the quickest way to go about it, because its built-in.

If you do want to control the style (for example, you're trying to make your code conform to a style guide), then you're going to need an external tool to process your file. You can invoke that tool from within vim with: :%!<toolname> <options>. This pipes the file through the tool and re-loads the processed result. (You can obviously use this for anything else you want to do to your file too)

So the next question is, what external tool should you choose? Regardless, the method is the same:

  1. Install the tool of choice
  2. Make sure its in your path
  3. Add a line to your vimrc file that creates a shortcut key to use so you save time
  4. Use it.

Now, which tool you use depends on the style you're trying to replicate. If you're trying to replicate a widely used style, then chances are astyle is all you need.

If you're trying to replicate a custom style, then you will need two things:

  1. UniversalIndentGui - a front end that lets you play around with various options and live-preview their effect on the source file
  2. A set of source code formatting tools installed and in your path

Between uncrustify and greatcode, you should be able to completely replicate the style you want.

Actually, I lied. There is another way and its called clang-format. However, you're going to want to read the documentation on it and its still in early stages so some options don't work very well. It is a beautiful tool though (definitely the smartest of the lot because constructs an AST of your code) and it is even available for Windows.

If you're going to take the time to read the manual, you also want to check out GNU Indent.

Of course, there is the last way, which is actually taking the time to learn vim's indent rules and writing one for your style. It will take time, but it will work with gg=G.

Some notes on astyle vs uncrustify vs greatcode:

  1. Astyle is good for general formatting, but can't do things like align the declaration of variables and re-style comments very well.
  2. Uncrustify can do a LOT of stuff that astyle can't, but be prepared to spend an hour playing around until you've found the correct combination of options you need. (Or if you feel like wasting a lot of time, use genetic algorithms to figure out the best combination of options for your style and when you do share the code and give me a link so I can use it too :) )

Note that you don't have to choose one tool. With vim, you can map one keystroke to execute several commands in succession, so theoretically you could use a combination of these tools to get exactly what you're looking for.

Last but not least, here's an excerpt from my .vimrc file, where I have mapped F12 to invoke astyle with some options:

"A2 = attached brackets
"-s8 indent 8 spaces
"-xc attached braces to class declarations
"-xj remove braces for single statement ifs and elses
"-c convert tabs to spaces in the non-indentation part of the line
map <F12> :%!astyle -A2 -s8 -xc -xj -c<CR>

Don't judge me on the style. Just use the tool to reproduce what you want.

share|improve this answer
add comment

Your Answer


