Here is a quick guide to learn and use the Vim editor.

Basically, you always switch between two modes in Vim, which are command mode and insert mode:

  • In command mode, you move around the file, delete text, or execute other commands.
  • In insert mode, you insert and edit text.

During start, Vim is always at command mode.

  • To change from command mode to insert mode, press i/I/a/A/o/O (refer table below).
  • To change from insert mode to command mode, press Esc (escape key).

Insert mode

To enter insert mode, use any of the following commands.

KeyAction
aAppend text following current cursor position
AAppend text to the end of current line
iInsert text before the current cursor position
IInsert text at the beginning of the cursor line
oOpen up a new line following the current line and add text there
OOpen up a new line in front of the current line and add text there

In insert mode, you can type and edit text as you would in a graphical text editor.

To create a page break, press CTRL+L.

Command mode

These are the commands that are used in the command mode.

KeyAction
Cursor Movement Commands
hMoves the cursor one character to the left
lMoves the cursor one character to the right
kMoves the cursor up one line
jMoves the cursor down one line
nG or :nCursor goes to the specified (n) line. (eg: 10G goes to line 10)
Note: the caret (^) symbol is used by vim to indicate the Ctrl key.
^FForward screenful
^BBackward screenful
^fOne page forward
^bOne page backward
^UUp half screenful
^DDown half screenful
$Move cursor to the end of current line
0Move cursor to the beginning of current line
wForward one word
bBackward one word
Exit Commands
:wqWrite file to disk and quit the editor
:q!Quit (no warning)
:qQuit (a warning is printed if a modified file has not been saved)
ZZSave workspace and quit the editor (same as :wq)
: 10,25 w tempwrite lines 10 through 25 into file named temp. Of course, other line numbers can be used. (Use :f to find out the line numbers you want.
Text Deletion Commands
xDelete character
dwDelete word from cursor on
dbDelete word backward
ddDelete line
d$Delete to end of line
d^(d caret, not CTRL d) Delete to beginning of line
Yank (has most of the options of delete)– VI’s copy commmand
yyyank current line
y$yank to end of current line from cursor
ywyank from cursor to end of current word
5yyyank, for example, 5 lines
Paste (used after delete or yank to recover lines.)
ppaste below cursor
Ppaste above cursor
"2ppaste from buffer 2 (there are 9)
uUndo last change
URestore line
JJoin next line down to the end of the current line
File Manipulation Commands
:wWrite workspace to original file
:w fileWrite workspace to named file
:e fileStart editing a new file
:r fileRead contents of a file to the workspace
Other Useful Commands
Most commands can be repeated n times by typing a number, n, before the command. For example 10dd means delete 10 lines.
.Repeat last command
cwChange current word to a new word
rReplace one character at the cursor position
RBegin overstrike or replace mode – use ESC key to exit
:/pattern Search forward for the pattern
:?pattern Search backward for the pattern
n(used after either of the 2 search commands above to continue to find next occurrence of the pattern.
:g/pat1/s//pat2/gReplace every occurrence of pattern1 (pat1) with pat2. Examples:
:g/tIO/s//Ada.Text_IO/g - Find and replace “tIO” by “Ada.text_IO” everywhere in the file.
:g/a/s// /g - Replace any letter “a” by a space
:g/a/s///g - Delete all letters “a”
Note: this command can be undone by u.

Vim Configuration File

For Debian-based linux, the Vim configuration file is located at /etc/vim/vimrc. You may comment or uncomment some of the lines to tweek Vim to your likes. Here are some useful options that can be enabled.

  1. Enable syntax highlighting.

    syntax on
    
  2. Makes the syntax highlighting color suits a dark background. Enable this if your terminal is dark.

    set background=dark
    
  3. Perform case insensitive matching.

    set ignorecase
    
  4. Show matching brackets.

    set showmatch
    
  5. Ask Vim to preserve tabs (instead of converting them to spaces), and set tab width to 4.

    set noexpandtab
    set softtabstop=0
    set shiftwidth=4
    set tabstop=4
    
  6. Enable smart indenting.

    set smartindent
    

Reference: http://www.radford.edu/~mhtay/CPSC120/VIM_Editor_Commands.htm