Last Updated: February 25, 2016
·
1.686K
· NIA

Vim: re-indent an area

When editing HTML, it often happens to me to paste a snippet of code from browser or from another file and see broken identation:
Picture
Ok, I selected this area, but how many times should I indent it? Should I do 5> or 7>? Actually, 6>, but I'm tired of guessing!

There are two solutions to this situation.

Number 1: Paste with ]p, not just p. This will fix identation on paste.
Instead of Shift+Ins use "*]p to paste from system clipboard as well.

(As you may guess, [p will also work with only difference that it pastes before, not after current position)

Number 2: If you already have misformated code and want to fix it, use = to fix selected area, == to re-indent current line, 5== for 5 lines, gg=G to fix the entire file, etc (use any motion you like)
Picture
Even more tricks are included in this exhaustive SO answer.

2 Responses
Add your response

Pro tip for screenshots:

" Remove menu bar
set guioptions-=m

" Remove toolbar
set guioptions-=T
over 1 year ago ·

@cypok TRUE

over 1 year ago ·