For example, to render:

while (head == tail) { head++; a[head] = 0 }


while (head == tail)
    a[head] = 0

You could install astyle and get it do the work for you. In vim you can then select a block of code and do:


(where the '<,'> was added for you when you press colon.)

This will replace your selected text with the beautified version from astyle.


Is this for Windows or Linux? I'll assume Linux since it is a vim question, even tho' vim is available for Windows.

The indent program is probably already installed on your Linux box, so no additional installed are needed. Similar to ngm's answer, you can use

:'<,'>!indent -


!Gindent -

to pretty-ify a block of code or all the code from the current spot to the end of the file. Be sure to include the '-' at the end of that command

See the man page for indent man indent for the voluminous amount of formatting options.



:setfiletype sh|%s/{/{^M/g|%s/}/^M}/g|%s/;/;^M/g|normal ggVG=

Breakdown: set the file type so that vim knows what syntax it's using ( shell script in this case ):

:setfiletype sh  

insert newlines after '{' characters and before '}' characters ( type CTRL-V enter key to get a literal newline character):


insert newline after ';' characters:


same as typing ggVG= in normal mode:
'gg' = goto top, 'V' = select lines visually, 'G' goto bottom, '=' = auto format:

:normal ggVG=

This is very simple minded and will fail in a lot of cases e.g. if there's a ';' in a string literal. But it should get the code more readable.

  • This isn't shell code, and while it (sortof) works for this code, it may not work for more extensive chunks of code. Also, instead of inserting literal control-m's in the command line, you can use "\r".
    – Heptite
    May 7 '11 at 3:16

Use vim-autoformat to automatically integrate external formatters (e.g. clang-format and astyle for c-like languages) and run them with one button press.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.