2.2: Vi Editor

Chapter X

Vi Editor

Getting Started

Warning

This topic is still in progress…

Open a new file

$ vim

Press i to enter insert mode and start typing. Once done, press ESC to go to normal mode to save and continue editing or exit.

save the new file

:w FILE

save and exit

:wq FILE

Open a new file with a name or existing file

$ vim sample.txt

Press i for insert and ESC to exit insert mode and

# save file
:w    

# save and exit
:wq

Cursor Movement

Command Description
h move one character left
j move one character down
k move one character up
l move one character right
0 move one column one of current line
^ move to the first non-blank character of the current line
$ move to the end of the line
gg move to the first line
G move to the last line
<n>G move to line <n>

Cursor Movement by Word

Command Description
w move to the next word; separated by a non-word character [^0-9A-Za-z_]
W move to the next word; separated by blank characters [^ \t\r\n\v\f]
e move forward to the end of the word; separated by a non-word character [^0-9A-Za-z_]
E move forward to the end of the word; separated by blank characters [^ \t\r\n\v\f]
b move to the previous word; separated by a non-word character [^0-9A-Za-z_]
B move to the previous word; separated by blank characters [^ \t\r\n\v\f]

Cursor Movement in the Current Window

Command Description
H move to the first line of the current window
M move to the middle line of the current window
L move to the last line of the current window

Cursor Movement: Page up/down (scrolling)

Command Description
^F go to next page (Forward)
^B go to previous page (Back)
^U go to previous half page (Up)
^D go to next half page (Down)

Getting into Insert Mode

Command Description
i insert text at the cursor
I insert text at the beginning of the line; same as ^i
a append text after the cursor
A append text at the end of the line; same as $a
o open a line after current line for imsert
O open a line before current line for imsert

Cut/Delete, Copy and Paste

When we issue a cut / copy / delete, we need to specify what is that we are copying or deleting; as in are we doing this operation on a character, line, word, few lines etc…

These commands are always accompanied by a cursor movement command. Example w, G, $ etc..

Command Description
d<C> cut / delete followed by cursor command
y<C> copy followed by cursor command; y means yank
p paste after cursor
P paste before cursor

Let us say we use X as subsititue for d or y. Here are some examples

Command Description
XG from current position to end of file
Xw cut/copy the current word
X5w cut/copy the 5 words from current position
X$ cut/copy from current position till end of the line

Search and Replace

Command Description
f<CH> find forward a character CH on the current line
F<CH> find backward a character CH on the current line
t<CH> like f<CH> but the cursor will be one char before the `
T<CH> like t<CH> but the cursor will be one char after the `