Indenting lines in vim

Vim has always been my primary editor for programming. There was always one part I hated doing the most, indenting lines. I did not know how to indent lines without manually doing it, as my laziness got the better of me. This time, I had to indent a huge block of text and I couldn’t let it be as it was. Here are some ways of indenting in vim.

1. <number of lines> <direction> <indent direction>
e.g. 5jj> indents 5 lines below to the right. jj represents the down direction. kk can be used for the opposite direction. < can be used for indenting to the left. Increasing the number of > or < , repeats the indentation that many times. E.g. >> indents it twice.

2. Indenting using Visual mode
You can use the visual mode to select the text you want to indent and then type ‘>‘ or ‘<‘ to indent the block.

3. Indenting a block between curly braces
If you want to indent an entire text block between curly braces, place the cursor on one of the curly braces and type %>
This is particularly useful for indenting loops and if/switch statements.

Happy indenting!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s