When you are using Vim, you want to know three things - getting in, moving about and getting out. Of course, while doing these three basic operations, you would like to do something meaningful as well. So, we start with getting into Vim.
Assuming that you are in a shell, or in the command line, you can simply type 'vim' and the application starts:
root@gateway [~]# vim
Exiting the VIM application is easily accomplished: type ':' followed by a 'q', hit the 'Enter' key and you are out:
~ VIM - Vi IMproved
~ version 7.0.237
~ by Bram Moolenaar et al.
~ Vim is open source and freely distributable
~ Become a registered Vim user!
~ type :help register<Enter> for information
~ type :q<Enter> to exit
~ type :help<Enter> or <F1> for on-line help
~ type :help version7<Enter> for version info
That's how you start and stop the Vim car. Now, let's try to learn how to steer the car.
You can move around in Vim, using the four arrow keys. However, a faster way is to use the 'h', 'j', 'k' and 'l' keys. This is because the keys are always under your right hand and you do not need to move your hand to access them as with the arrow keys. The 'j' moves the cursor down, 'k' moves it up. The 'h' key moves the cursor left, while 'l' moves it to the right. That's how you steer the Vim car.
You can edit a file using Vim. You either have an existing file, or you make a new one. If you start with 'vim filename', you edit the file represented by the 'filename'. If the file does not exist, Vim will create a new file. Now, if you want to edit a file from within Vim, open the file using ':e filename'. If this file is a new file, Vim will inform you. You can save the file using the ':w' command.
If you need to search the file you are editing for a specific word or string, simply type forward-slash '/' followed by the word you would like to search for. After hitting 'enter', VIM will automatically take you to the first match. By typing forward-slash '/' again followed by 'enter' it will take you to the next match.
To write or edit something inside the file, you can start by typing ':i' and Vim will enter the 'Insert' mode. Once you have finished, you can exit the Insert mode by pressing the 'Esc' key, and undo the changes you made with ':e!'. You also have a choice to either save the file using the ':w' command, or save & quit by using ':wq'. Optionally, you can abort the changes and quit by ':q!'.
If you have made a change and want to quit without explicitly informing Vim whether you want to save the file or not, Vim will rightly complain, but will also guide you to use the '!'.
Start VIM: vim
Quit Program: :q
Move Cursor: Arrow keys or j, k, h, l (down, up, left, right)
Edit file: vim filename
Open file (within VIM): :e filename e.g :e bash.rc
Search within file: /'string' e.g /firewall
Insert mode: :i
Save file: :w
Save and Quit: :wq
Abort and Quit: :q!
Advanced Features of VIM
Now that you know your way in and out of Vim, and how to edit a file, let us dig a little deeper. For example, how can you add something at the end of a line, when you are at its starting point? Well, one way is to keep the right arrow pressed, until you get to the end. A faster way is 'Shift+a' and you are at the end of the line. To go to the beginning of the line, you must press 'Shift+i'. Make sure you are out of the 'Insert' mode shown at the bottom; use the 'Esc' for this.
Supposing you are in the middle of a line, and would like to start inserting text into a new line, just below it. One way would be to move the cursor right and hit 'Enter' when you reach the end. A faster way is to enter 'o'. If you enter 'o' or 'shift+o', you can start entering text into the new line created above the cursor. Don't forget to exit the 'Insert' mode by pressing 'Esc'.
How do you delete lines? Hold the 'delete' button and wait until the lines are gone. How can you do it faster? Use the 'd' command. If you want to delete 10 lines below your cursor position and the current line, try 'd10j'. To delete 5 lines above your current position and the current line, try 'd5k'. Note the 'j' and 'k' (down, up) covered in our previous section. If you’ve made a mistake, recover it with the undo command, 'u'. Redo it with 'Ctrl+r'.
Tip 1: To delete the current line alone, use 'dd'.
Tip 2: To delete the current line and the one below it, use 'd2d'.
Did you know you can have windows in Vim? Oh yes, you can. Try 'Ctrl+w+s' if you want a horizontal split, and 'Ctrl+w+v' if you want a vertical split. Move from one window to another by using 'Ctrl+w+w'. After you have finished traveling through all the windows, close them one by one using 'Ctrl+w+c'.
Here is an example with four (4) windows within the Vim environment:
You can record macros in Vim and run them. To record a macro you have to start it with an 'm'. To stop recording it, hit 'q'. To play the macro, press '@m'. To rerun it, press '@'. Macros are most useful when you require to perform the same commands within a file.
Vim also has extensive help facilities. To learn about a command, say 'e', type ':h e' and hit 'Enter'. You will see how the command 'e' can be useful. To come back to where you were, type ‘:q’ and then ‘Enter’. Incidentally, typing ':he' and 'Enter' will open up the general help section. Come back with the same ':q'.
As an example, here's what we got when we typed ':h e' (that's an ":" + "h" + space + "e"):
When we typed ':he', we were presented with the main help file of VIM:
Move cursor to end of line: Shift+a
Move cursor to beginning of line: Shift+o
Delete current line: dd
Delete 10 lines below cursor position: d10j
Delete 5 lines above cursor position: d5k
Window Mode - Horizontal: Ctrl+w+s
Window Mode - Vertical Split: Ctrl+w+v
Move between windows: Ctrl+w+w
Close Window: Ctrl+w+c
Enable Macro recording: m
Play Macro: @m
Help: :h 'command' from within VIM. e.g :h e