Wednesday, September 14, 2011

Most useful vi editor ( vim editor ) commands

Backspace and arrow keys behave differently in vi editor. Better alternative is vim editor.
Install vim editor as below
$sudo apt-get install vim

Alright, before we start, vim editor has two modes.
  1. Command Mode: This is the default mode. When vim editor is first started, it enters in this mode. Command mode is useful for tasks like copying, deleting, seaching etc..
  2. Insert Mode: This is the mode for entering text 
Since vim is in command mode initially, to get to insert mode, all you need to do is, hit key i 
To get back to command mode from insert mode, hit key Esc

Note: there are other keys that can change the editor from command mode to insert mode but to keep this post simple, I am not going to discuss that

Now to the commands

Open File
$vim sample_file.txt

Change to insert mode by hitting the key i and type few lines so that we can use some other commands.
Now, come back to command mode by hitting Esc


Save file
:wq

Quit modified file without saving
:q!

Quit unmodified file
:q

Great. Now you can open a file, type some text, save and exit.
Lets open the file again with vim editor and try some more commands. I am assuming that you typed in atleast 10 lines.

Copy a line
yy

Copy 5 lines
5yy (you get the idea)

paste the copied line/s from buffer
p

delete a line
dd

delete 5 lines (home work :) )

Greate now you can copy n paste too. saves lot of time, isn't it. Now the most useful command

Search
:/word (after hitting enter, use n to go to next match) 

Case-Insensitive Search
:/cword

Go to the end of file
:$

That's it. These are the basic commands that you need to know for doing some simple things with vim editor.





No comments:

Post a Comment

Followers