Learning Vim Editor – Part 1

Vim is an extremely powerful text editor, with which you can do complicated editing without your hand ever leaving the keyboard. Personally I do not like GUI oriented editors such as notepad++ because: A. I am too lazy to reach the mouse and B. GUI menu system often confuses me.

I am only a starter user of Vim and only know basic commands. I am trying to learn more of Vim, and this series of posts consist of my notes/cheatSheet from reading Learning the vi and Vim Editors.

Vim is an enhanced version of vi editor, which itself is an enhanced version(visual version) of ex editor. Therefore I’ll try to organize my notes into these three parts: Part 1: vi, Part 2: ex and Part 3: vim.

1. Part 1: vi

There are two modes in vi: Command Mode and Insert Mode. As the name suggests, Insert Mode is the mode where vi behaves like a normal text pad where you can type in characters and delete text using your keyboard one character at a time. However, the real power of vi comes from Command Mode, because in this mode you can do all sorts of high-level navigation and editing.

Based on functionality, vi commands can be roughly binned into the following 3 categories:

  • Navigation Commands are commands used to help us move around in the file(they only change position of your cursor, you always stay in Command Mode). For most of the commands you can apply a multiple number to execute the navigation command multiple times([multiple number]command will execute the command multiple number times)

 

h Left
j Down
k Up
l Right
CTRL+f Scroll forward one screen
CTRL+b Scroll backward one screen
CTRL+d Scroll forward half a screen
CTRL+u Scroll backward half a screen
CTRL+e Scroll screen up one line
CTRL+y Scroll screen down one line
H Move to top line on the screen
M Move to middle line on the screen
L Move to bottom line on the screen
z ENTER Move current line to top of screen
z. Move current line to center of screen
z- Move current line to bottom of screen
0 Move to beginning of line
$ Move to end of line
^ Move to first non-blank character of the line
num{|} Move to column num of the line
w Move cursor forward one word at a time
W Move cursor forward one word at a time, ignoring symbols
b Move cursor backward one word at a time
B Move cursor backward one word at a time, ignoring symbols
e To end of word
E To end of word, ignoring symbols
( Move to beginning of current sentence
) Move to beginning of next sentence
{ Move to beginning of current paragraph
} Move to beginning of next paragraph
% Move to matching bracket. Useful for programming
+ To first character of next line
To first character of previous line
G Go to end of file
numG Go to line num
/pattern Search regular expression pattern
?pattern Search regular expression pattern in backward direction
n Repeat search in same direction
N Repeat search in opposite direction
fchar Find next occurance of char in the line
Fchar Find previous occurance of char in the line
tchar Find character before next occurance of char in the line
Tchar Find character after previous occurance of char in the line
; Repeat previous find operation in the same direction
mchar Mark current position with char (char can be any letter)
`char Move cursor to character marked by char
char Move cursor first character of line marked by char
Return cursor to your original position
Return cursor to the first character of the line of original position

  • Editing Commands contains commands to do editing. Depending on the type of the edit, You will either go from Command Mode into Insert Mode, or you stay in Command Mode. You can always get back to Command Mode by pressing ESC key. General syntax for commands in this category is command[multiple number][destination] and [multiple number]command[destination], which basically mean executing command multiple number times from current position to the position indicated by destination. destination is usually one of Navigation Commands described above.
    i Insert
    I Insert at beginning of line
    a Append
    A Append to end of line
    o Open blank line below cursor for text
    O Open blank line above cursor for text
    c Change
    C Replace characters from current cursor position to end of current line
    cc/S Replace entire current line
    s Delete character at cursor and substitute text
    S/cc Delete entire line and substitute text
    r replace a single character
    R replace any number of characters
    x Delete a single character
    X Delete a single character before current position
    d Delete
    D Delete to end of line
    p Put
    P Put before current position
    y Yank
    Y/yy Yank the whole line
    J Join two lines
    ~ Change case
    . Repeat last edit command
    u Undo last command
    U Undo all edits on a single line
    char Access buffer char

     

  • Other Commands contain commands to do miscellaneous things, like saving the file, quit vi, change vi environment etc.
    ESC Change to Command Mode
    Q Go to ex editor. Type vi to get back to vi
    ZZ/:wq Save and quit
    CTRL+l Redraw the screen
    CTRL+g Show current line position information
    vim +num file Open file and go to line num
    vim + file Open file and go to last line
    vim +/pattern Open file and go to first occurrence of pattern
    vim -R file Open file in read-only mode
    vim -r file Open file and recover edit buffer
    :set wm=num Automatically insert a newline after num characters
    :set nowrapscan Disable wrap from beginning of file when doing search
    :set nu/number Display line numbers
    :set nonu/nonumber Disable display line numbers
    :set autoindent Enable autoindentation for programming
    :set noautoindent Disable autoindentation
    :set list Show tab as special character
    :set nolist Show tab as normal tab
    :set showmatch Show matching bracket in Insert Mode
    :sh Create a shell. Type EXIT to terminate the shell and come back to vi
    :ab abbr phrase Abbreviate phrase to abbr
    :unab abbr Disable abbreviation abbr
    :map x sequence Define character x as a sequence of editing commands
    :unmap x Undefine mapping x
    :!cmd execute shell command cmd

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s