ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Emergency vi (vi tutorial)

Xah Lee, , …,

This page is a basic tutorial for vi or vim.

vi is one of unix's fsckup. Sometimes in a emergency situation you are forced to deal with vi, and fscking unix admins refuse to install emacs. Here's your help:

vi man
vi man

To insert text, move your cursor to the right place, then press i, then type your text. After you are done, type Esc to exit the insertion mode.

To delete text, move cursor to the right place and press x. (if x is inserted, that means you forgot to exit the insert mode. Type Esc to exit insertion mode. Type u to undo.)

When something doesn't work, or if you accidentally typed something and have no idea what is going on, try press Esc, then type u for undo. If a cat jumped onto your keyboard and you cannot exit vi, type the shell suspend command 【Ctrl+z】, which should get you back on the unix command prompt, then you can kill. (by first find out the pid by ps auwwx | grep vi, then kill -9 ‹pid›).

Congratulation! You've learned emergency vi.

It may be confusing, but with the above you can edit any text files with vi.

Installing vim

By default, the vi in Ubuntu 12.04 (as of ) is “vim.tiny”, a very basic vi-like vim. You should install full vim. For how to install and other detail, see: Ubuntu Linux: vim Location and Versions.

Basic vi Commands

Remember, in vi, it has modes. At any one time, you are either in command mode or text insertion mode. To go to the command mode, just press Esc. To go into editing mode, press i.

Goto Insertion Mode

KeyMeaning
iinsert
ainsert after cursor (append)
oinsert a new line

Undo & Redo

KeyMeaning
uundo
Ctrl+rredo

Cursor Movement

KeyMeaning
jdown
kup
hleft
lright
wnext word
bprev word
0beginning of line
$end of line
:nline n
:$end of file
Ctrl+fpage down
Ctrl+bpage up

Deleting Text

KeyMeaning
xdelete char
9xdelete 9 chars
dwdelete word
9dwdelete 9 words
Ddelete from cursor to end of line
dddelete whole current line

Copy & Paste

KeyMeaning
yycopy current line
3yycopy 3 lines
ppaste

Note that when you delete a word or many words, the word is automatically put into the clipboard.

Search & Replace

KeyMeaning
/aasearch forward for text “aa”
?aasearch backward for text “aa”
nnext occurrence
Nprevious occurrence
:s/aa/bb/gsearch and replace ALL “aa” by “bb” in CURRENT LINE. “g” (global) means do it for all occurrences.
:%s/aa/bb/gcsearch and replace ALL “aa” by “bb” in whole file. “c” means ask for confirmation before each replacement.
:20,30s/aa/bb/gcsearch and replace “aa” by “bb” in line 20 to 30.

Syntax Coloring

KeyMeaning
:set syn=langthe “lang” can be perl, c, cpp, html, php, JavaScript, python etc.
:syntax onTurn on syntax coloring
:syntax offTurn off syntax coloring

Line Numbers

KeyMeaning
:set numberTurn on line numbers
:set nonumberTurn off line numbers

misc

to find out what a key or key-combo does, type :help then press the key, then Enter ↵.

vi clippy assistant
blog comments powered by Disqus