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?
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?
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.
:%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 8
. Try astyle
. –
Tussis set tabstop=4
. –
Jerricajerrie 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;
}
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 %
sudo apt install indent
to install the indent utility. –
Boise 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
sudo apt install clang-format
–
Morehead 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.
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:
:!indent
.astyle
takes stdin too, so you can use the same trick there.
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.
: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 Maybe you can try the followings $indent -kr -i8 *.c
Hope it's useful for you!
Their is a tool called indent
. You can download it with apt-get install indent
, then run indent my_program.c
.
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
.
=
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.