Random Bits

Technical Log from Didik Setiawan

Vim Quick Reference

For unfimiliar user, vim is a little bit confusing at first. But, after you try it several times and configure properly, you will found this text editor is very powerful.
Here I will note several subject how to use vim, because sometimes navigate to vim help is quite difficult.

Vim is modal editor which means you use different mode to edit text. Some important mode you will use very often are:

  • Normal Mode

You can enter any command using : or search using / and ?. Press Esc to enter this mode.

  • Insert Mode

Text you type is inserted into the buffer. Press i to enter this mode.

  • Visual Mode

In this mode, you can select the text we want in order to copy, cut or delete. Press v to enter this mode. The cursor position will be starting point to your selection.

Navigation

You can move through the buffer with the following key:

Move left h
Move right l
Move up k
Move down j

Using this key will make your navigation faster than using arrow keys or mouse.

Move one word w
Move one word backwards b
Move to end of line $
Move to beginning of line 0
Insert text i
Insert text at the end of line A
Insert text at the beginning of line I
Insert text above line O
Insert text below line o
Replace one character r
Replace character consecutively R (enter “replace mode”)

Page navigation:

Move page down ctrl + f
Move page up ctrl + b
Move page half down ctrl + d
Move page half up ctrl + u
Move top gg
Move bottom G

Navigate through command history q: Navigate through search history q/ Then you can select command or search result with navigation key. To execute command, press Enter.

Open and Save File

Open file :e filename
Save file :w
Saveas file :w filename
Quit :q
Quit without save:q!
Save file and quit:wq

Undo and Redo

Undo u
Redo ctrl + r

Visual Selection

Use v on text you want to select. You will enter visual mode. Navigate using hjkl key. Then we can do other action such as copy, cut or delete.

Copy and Paste Text

After you select a text, you can do following action:

Copy y
Cut/Delete d or x
Copy entire line yy
Cut entire line dd
Paste p

Search and Replace Text

Find text /query
Find text upwards ?query
Navigate between search results n to move forward and N to move backwards

Find text using visually selected text, firstly add this line to vimrc file

vnoremap // y/<C-R>"<CR>

then select text in visual mode and press //

Replace text :%s/old/new/g
Replace all :%s/old/new/gc

Word Completion

You can autocomplete text with several method to get different result:

Search text before cursor ctrl + p
Search text after cursor ctrl + n
Search for filename complete with its path ctrl + x ctrl + f

Multi Window Editing

Open new window :new
Open new vertical window :vnew
Move through window ctrl + ww
Swap window position ctrl + wr

Compare Buffer

If you have two split windows containing buffers that you want to compare, then you can diff them by running :windo diffthis
You can turn diff mode off just as easily, by running :windo diffoff

That’s how I use vim in daily life. For my personal preference, I have modified my vimrc which you can find here. Thanks for reading.

Comments