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!


Vim : Things you thought you could never do Part I

When i first started out programming using Vim, my first reaction was “What a boring editor, ancient piece of crap” . So i started using gedit regularly. It was only recently that i was enlightened about the Awesome features of Vim!! So i thought i need to enlighten some of the vim critics out there.

Vim Modes

There are 2 main modes in Vim
a. Normal mode: The one when you open it initially in Vim
b. Editing Mode: When you press one of the following keys when in normal mode
i   –  insert at current position
a  –  insert AFTER current position
o  – open (create) a new line below current line
I  – insert AT START of current line
A – insert AFTER end of current line
O – open (create) a new line ABOVE current line
Here, as you can see they key ‘o‘ would turn out to be pretty useful.
Note: The following tips are useful in normal mode. The quotes are given only for understanding that they are strings. You dont need them while entering them in normal mode.



To make navigation easier i.e to make use of the main keys on the keypad h , j , k , l are used. To remember better
a. h is on far left -> so stands for left
b. l is on far right -> so stands for right
c. Since j looks like it is somewhat pointing down -> stands for down
d. Obv -> k stands for up


Use of combos to achieve magic !

d- delete
c- change
w- word
b- word before
a. So type “wwwwww” (and) “bbbbbbbbbb” and see what happens. Cool right!
b. Similarly place the cursor before a word and type “dw” -> a word is deleted!
c. So now, you can make combo’s of the above letters to make super things happen
i. d6w – delete 6 words after cursor posn
ii. d6j – delete 6 lines down
iii. d4b – delete 4 words before cursor posn
iv.  cw – change current word
etc. etc…. All this just in a few keystrokes !!


Auto-complete Feature

Lets say i am typing a document/program and i repeat the use of many words/keywords. The auto-complete feature shows me a list of complete options allowing me to fill in the word of my choice
Ctrl – P – Search previous
Ctrl – N – Search Next
Once the a drop-down of possible options(for multiple fills) is shown, go down to the appropriate one and press <enter >
Try it out!


Visual Mode

Now, we are always faced with the stigma as to
“How do i select multiple lines without a mouse? How do i copy/paste/delete multiple lines”.
Vim has the solution in the form of the “Visual Mode”
1. Type “v” – allows you to select multiple characters
2. Type “V”(Shift-v) : allows you to select multiple lines
Once youve selected the lines, you can  use
a. “d” – to delete
b. “y” – copy (short for yank)
c. “p” – paste (once you have copied)
d. Use “<“ and “>” to indent left and right
e.  “o” moves to start and of select
Other useful shortcuts
“dd”-delete current line
“d2d” – delete 2 lines
“yy” – yank current line
LOOK OUT FOR PART 2 !! Coming up soon!