Yongwei Xing Yongwei Xing - 1 month ago 5
C Question

How can I autoformat/indent C 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?

Answer

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.

Comments