Vi: The Text Editor
The Vi or Vim (i.e. Vi improved) is the most used key-board based text editor in Unix like system. Here I have summarize the most used (and also not so commonly used) key-board shortcuts in Vim.
What you must understand is that there is no rigid way of using keyboard shortcuts(hereafter ksc). The beauty is that you can always configure kscs to make them more handy for you. Here I have listed the defaults only.What you need not know
Well, there is hardly anything you don't want to know, but as a beginner, you may skip few thing as how Vi works. Like the terminal you use or, even more importently, your shell. As far as text editing is concerned, it hardly matters if you use Bash/Csh/Sh/Ksh. I personally prefer Bash and its preety similar to sh, and I am yet to found any instance where vi fails for other shell.
You may check your shell type by echo $SHELL and if you are not satisfied with the shell , you can always change them by simply typing the shell name in which you are most comfortable. But my advice is to stay with the shell that your system admin. offers you.
Let's Start with Vi(or Vim)
Remember, the BEST tutorial available for vim is :help. and ofcourse, man. Here, I am just keeping a very few commands as a cheatsheet. With this breif introduction, you are now ready to start vi. But if you are a first time user, don't experiment on any existing file; better create a new file by typing vi test and the terminal will open a new file for you, named test. now first go to insert mode by pressing i and you are ready to write on it.
When you want to finish your writing, go to command mode, and then type :wq where w is to save your last edit and q means quiting the current file.
Now I ll breakup the whole game in few part and try to explain you how to gwt maximum out of vim, well , as far as I know (remember, I can only tell you wat I know and even after so many years of using Vi and linux in general, when I still see other people doing, I always get amused by learning new tricks and commands. There are hundreds of way of doing the same thing afterall)!
- Save and Quit
- Moving the Cursor
- Inserting Text
- Editing: Cut and Paste
- Search/Replace
- One More for Advanced User
a) Save and quit
:w : Save
:x : Exit, saving changes
:q : Exit as long as there have been no changes
ZZ : Exit and save changes if any have been made
:q! : Exit and ignore any changes
Go to listb) Moving the Cursor
Vi is command line editor and its cursor position is not controlled by mouse(a very irritating component in my view), but by keyboard. Here is a list of controlling cursor position. Those commands are to be used in command line mode only.
h : Move left
l : Move right
j : Move down
k : Move up
w : Move to next word
W : Move to next blank delimited word
b : Move to the beginning of the word
B : Move to the beginning of blank delimted word
e : Move to the end of the word
E : Move to the end of Blank delimited word
( : Move a sentance back
) :Move a sentance forward
{ : Move a paragraph back
} : Move a paragraph forward
0 : Move to the begining of the line
$ : Move to the end of the line
1 : GMove to the first line of the file
G : Move to the last line of the file
nG : Move to nth line of the file
:n : Move to nth line of the file
fc :Move forward to c
Fc : Move back to c
H :Move to top of screen
M :Move to middle of screen
L : Move to botton of screen
Go to listc) Inserting Text
Now, the most importent thing is editing or writing to a new document. By command line, you can control where to put your new letter:
i : Insert before cursor
I : Insert before line
a : Append after cursor
A : Append after line
o : Open a new line after current line
O : Open a new line before current line
r : Replace one character
Go to listd) Editing: Cut and Paste
Obviously you may be satisfied with using delete button to delete text through out your life and still remain happy, but you can't copy,right? There are so many other options in vim, tailor cutted for your requirments. Remember, this mode is also a command mode.
d^ : Deletes from current cursor position to the beginning of the line.
d$ : Deletes from current cursor position to the end of the line.
dw : Deletes from current cursor position to the end of the word.
dd : Deletes three lines from current cursor position downwards.(also :d)
yy : Yank the current line (also :y)
x : Delete a character next to the cursor
X :Delete character to the left of cursor
P : Paste line before the cursor
p : Paste line after the cursor
. : Repeat last edit command
u : Undo last edit
U : Undo changes to current line
J : Join two lines
Go to liste) Search/Replace
/pattern : search for pattern
?pattern : search backward for pattern
n :repeat search in same direction
N : repeat search in opposite direction
:%s/old/new/g : replace all old with new throughout file
:%s/old/new/gc : replace all old with new throughout file with confirmations
Go to listf) One More for Advanced
There are so many way to save your time: allowing you to type less; and I am afraid I hardly know all of them. But here is my favourite one: abbriviationWhen you are in need to use some given phrase(or word, to say) over and over, you can save time just by making an abbriviation All you have to do is to let vim understand when you type "g", you actually mean http://www.google.com. When you invoke vim, it reads the .vimrc file either in root or your own account. So to let vim know this, just put
. then whenever you want to apply this, type "g" followed "space"; this is necessary so that vim will not confuse itself when you want to type "go".