298

When I copy code from another file, the formatting is messed up, like this:

fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}

How can I autoformat this code in vim?

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Yongwei Xing
  • 12,983
  • 24
  • 70
  • 90
  • 4
    For anyone looking to format and not just indent the code, the second answer (mine) addresses that. If you just want to fix indenting, the accepted answer is the simplest way. – Derek Apr 02 '15 at 21:07

10 Answers10

677

Try the following keystrokes:

gg=G

Explanation: gg goes to the top of the file, = is a command to fix the indentation and G tells it to perform the operation to the end of the file.

Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
  • 36
    this only indents the code. can something not be done to AUTOFORMAT the entire thing? – N 1.1 Mar 01 '10 at 12:54
  • 3
    @Ton van: [See my answer](http://stackoverflow.com/questions/2355834/auto-format-c-file-in-vim/4236918#4236918) for the difference (Could not have explained here in comments). – Lazer Nov 21 '10 at 07:47
  • 4
    -1 This only fixes indentation, not formatting (which was what being asked for). – oligofren Apr 19 '13 at 06:58
  • 6
    @oligofren The OP's example only included indentation errors and he accepts the answer, so I guess that's what he meant. – Amir Rachum Apr 19 '13 at 17:40
  • 3
    @oligofren Also, he specified he copies the code from another file, which makes indentation problems more likely than other formatting problems. – Amir Rachum Apr 19 '13 at 17:42
  • 1
    Suggested edit: change title to "Auto indent C code in Vim" otherwise, some people (like me) find this answer while looking for the AStyle answer. – xor007 Jan 06 '15 at 05:13
  • I haven't known such combination. I used == for single line, but I thought that = works only in visual mode, – jmmk Oct 27 '15 at 17:02
  • You can then hit to go back to where you were. – domi91c Oct 16 '16 at 20:20
  • In "Ex mode" (which you typically enter with :), the equivalent is `:%normal ==` (where % means all lines in the current buffer--aka file in vim's memory--and == means autoindent the current line). This approach allows you to limit auto-indent by line number, like `:5,10normal ==` (meaning, lines 5-10). And you can abbreviate `normal` to `norm` to save typing, as well eliminate the space: `:%norm==`. – mellow-yellow Mar 05 '18 at 15:02
  • Warning for readers: this set tabstop to `8`. Try `astyle`. – user26742873 Feb 14 '21 at 17:37
  • 1
    @asuka you can `set tabstop=4`. – Amir Rachum Mar 20 '21 at 05:09
93

I like to use the program Artistic Style. According to their website:

Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.

It runs in Window, Linux and Mac. It will do things like indenting, replacing tabs with spaces or vice-versa, putting spaces around operations however you like (converting if(x<2) to if ( x<2 ) if that's how you like it), putting braces on the same line as function definitions, or moving them to the line below, etc. All the options are controlled by command line parameters.

In order to use it in vim, just set the formatprg option to it, and then use the gq command. So, for example, I have in my .vimrc:

autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ -T4pb

so that whenever I open a .cpp file, formatprg is set with the options I like. Then, I can type gg to go to the top of the file, and gqG to format the entire file according to my standards. If I only need to reformat a single function, I can go to the top of the function, then type gq][ and it will reformat just that function.

The options I have for astyle, -T4pb, are just my preferences. You can look through their docs, and change the options to have it format the code however you like.

Here's a demo. Before astyle:

int main(){if(x<2){x=3;}}

float test()
{
if(x<2)
x=3;
}

After astyle (gggqG):

int main()
{
    if (x < 2)
    {
        x = 3;
    }
}

float test()
{
    if (x < 2)
        x = 3;
}
starball
  • 20,030
  • 7
  • 43
  • 238
Derek
  • 3,087
  • 1
  • 21
  • 23
  • 3
    Is there anything like this for other formats; ruby, xml, json, etc.? – derGral Jun 17 '11 at 19:22
  • 3
    @Ryan check vim-autoformat, see answer below. BTW vim-autoformat also uses astyle among others. – chtenb Apr 14 '13 at 20:13
  • 1
    Thank you! I was beginning to think I was the only person in the word who didn't want their opening brackets on the same line as the function declaration! – user3640967 Feb 02 '16 at 09:59
34

The builtin command for properly indenting the code has already been mentioned (gg=G). If you want to beautify the code, you'll need to use an external application like indent. Since % denotes the current file in ex mode, you can use it like this:

:!indent %
soulmerge
  • 73,842
  • 19
  • 118
  • 155
24

I find that clang-format works well.

There are some example keybindings in the clang documentation

I prefer 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
Daniel
  • 3,243
  • 2
  • 32
  • 31
19

The plugin vim-autoformat lets you format your buffer (or buffer selections) with a single command: https://github.com/vim-autoformat/vim-autoformat. It uses external format programs for that, with a fallback to vim's indentation functionality.

chtenb
  • 14,924
  • 14
  • 78
  • 116
9

I like indent as mentioned above, but most often I want to format only a small section of the file that I'm working on. Since indent can take code from stdin, its really simple:

  1. Select the block of code you want to format with V or the like.
  2. Format by typing :!indent.

astyle takes stdin too, so you can use the same trick there.

dsummersl
  • 6,588
  • 50
  • 65
7

I wanted to add, that in order to prevent it from being messed up in the first place you can type :set paste before pasting. After pasting, you can type :set nopaste for things like js-beautify and indenting to work again.

atripes
  • 1,683
  • 4
  • 20
  • 23
  • 1
    I use `:set paste!` for this, which toggles the paste value instead of setting it. This makes it easier to turn off as you can simply scroll back a few commands and press enter. – a'r May 02 '20 at 09:05
  • You can also setup a hotkey for this. I wish I didn't have to but it's apparently a necessary evil. – qwr Jul 31 '22 at 05:34
4

Maybe you can try the followings $indent -kr -i8 *.c

Hope it's useful for you!

lattimore
  • 51
  • 1
1

Their is a tool called indent. You can download it with apt-get install indent, then run indent my_program.c.

P....
  • 17,421
  • 2
  • 32
  • 52
n0va_sa
  • 19
  • 4
0

For a good overview and demo of many of the options mentioned here, @Gavin-Freeborn has a great video on YouTube:

https://www.youtube.com/watch?v=tM_uIwSucPU

It covers some Vim plugins as well as built-in capabilities such as =, gq, and formatprg.

Randall
  • 2,859
  • 1
  • 21
  • 24
  • I'm a bit confused why this is voted down; I found the video far more useful than trying to sort through all the options here. I'd be happy to add further description here in this answer of the capabilities mentioned; I'd just felt like the other answers already filled out that part. – Randall Feb 14 '23 at 23:07