How can I autoformat/indent C code in vim?
Asked Answered
K

10

305

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?

Kickback answered 1/3, 2010 at 12:50 Comment(1)
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.Citron
J
691

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.

Jerricajerrie answered 1/3, 2010 at 12:52 Comment(11)
this only indents the code. can something not be done to AUTOFORMAT the entire thing?Delayedaction
@Ton van: See my answer for the difference (Could not have explained here in comments).Reproachful
-1 This only fixes indentation, not formatting (which was what being asked for).Tude
@Tude The OP's example only included indentation errors and he accepts the answer, so I guess that's what he meant.Jerricajerrie
@Tude Also, he specified he copies the code from another file, which makes indentation problems more likely than other formatting problems.Jerricajerrie
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.Ticker
I haven't known such combination. I used == for single line, but I thought that = works only in visual mode,Obscurantism
You can then hit <C-o><C-O> to go back to where you were.Taritariff
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==.Palmerpalmerston
Warning for readers: this set tabstop to 8. Try astyle.Tussis
@asuka you can set tabstop=4.Jerricajerrie
C
94

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;
}
Citron answered 2/3, 2010 at 13:46 Comment(3)
Is there anything like this for other formats; ruby, xml, json, etc.?Ribosome
@Ryan check vim-autoformat, see answer below. BTW vim-autoformat also uses astyle among others.Alchemy
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!Fanchan
Q
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 %
Quarantine answered 1/3, 2010 at 13:4 Comment(1)
Before using this command, ensure to do sudo apt install indent to install the indent utility.Boise
B
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
Broad answered 27/10, 2016 at 2:34 Comment(1)
Need to install it first, e.g for ubuntu 16.04+: sudo apt install clang-formatMorehead
A
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.

Alchemy answered 2/12, 2012 at 20:9 Comment(0)
M
8

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.

Machiavelli answered 13/2, 2015 at 14:41 Comment(0)
K
6

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.

Kikelia answered 2/2, 2017 at 9:1 Comment(2)
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.Acetophenetidin
You can also setup a hotkey for this. I wish I didn't have to but it's apparently a necessary evil.Abecedarian
S
4

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

Hope it's useful for you!

Scabrous answered 23/9, 2011 at 5:59 Comment(0)
B
1

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

Blithesome answered 6/2, 2017 at 8:44 Comment(2)
This GNU tool sucks is weird... I did not like it!Pinwork
its very customiseable. I use it as cb (C beautify) in my .bashrc as: alias cb='indent -kr -l200 -lc200 -cli2 -i4 -nut -linux $1'Tense
G
-1

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.

Gavan answered 3/2, 2023 at 21:42 Comment(3)
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.Gavan
This is a forum based on the written word. To refer, with no relevant detals, the reader to a video is likely to be ill received by the SO community.Grata
@Grata - Thanks, that seems fair. The video has a fair bit that would need to summarized beyond the = gq and formatprg that I tersely mention in the answer. It's still a really useful video; if I can find time (and the recollection) to go back and add relevant transcript excerpts, I will. YouTube seems to have dropped providing transcripts, at least for non-premium accounts, so it'll take some extra effort. In any case, I really appreciate the explanation!Gavan

© 2022 - 2024 — McMap. All rights reserved.