Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For example, to render:

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

into

while (head == tail)
{
    head++;
    a[head] = 0
}
share|improve this question

migrated from programmers.stackexchange.com May 6 '11 at 3:55

This question came from our site for professional programmers interested in conceptual questions about software development.

add comment

5 Answers 5

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

:'<,'>!astyle

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

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

share|improve this answer
add comment

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 -

or

!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.

share|improve this answer
add comment

One-liner:

: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):

:%s/{/{^M/g
:%s/}/^M}/g

insert newline after ';' characters:

:%s/;/;^M/g

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.

share|improve this answer
    
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
add comment

Simply install this vim plugin, and off you go.

https://github.com/Chiel92/vim-autoformat

One button press, and you're done.

share|improve this answer
add comment

Select code in visual selection mode (V) and hit =

share|improve this answer
    
The = normal mode command does indenting, but it will not insert (or remove) newlines in code. –  Heptite May 7 '11 at 3:11
    
Yes, my answer is wrong, I should have checked it before posting. –  AlexD May 8 '11 at 2:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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