Monthly Archives: November 2013

vi. Quick notes.

Unless you’ve been a long time *nix user, you may not be part of the EMacx, vs vi school. Perhaps like me, you prefer using something like Nano to edit text files in the terminal.

Problem is, on many systems such as those running embedded Linux and using something like Busybox, these more user friendly editors simply aren’t available. Even if the system has a package manager and their exists your favourite editor in a package repositry, you may not have the permissions to install them. But you still need to edit a text file. Granted, if you’re not root or listed in sudoers, you’re not going to be able to edit anything in /etc anyway but maybe you just need to write a note in /home.

Thankfully the text editor vi should always be included in a Posix compatible environment, like those running Busy Box. But you load it up and realise you can’t use your mouse and you don’t have time to read the manual. Fear not, see below.

Vi operates in 2 modes. Command and insert. Insert is where you actually enter text, command operate upon the text.

Press i to enter insert mode and esc to back out to command mode.

If you’re not sure you’re in command or insert mode. Press escape a couple of times. You may hear a ping indicating you’re already in command mode.

These days cursor keys should work moving around in command mode, otherwise:

Left: UP: Down: Right:
h. j. k. l.

Enter insert mode
i

Some comomn commands: (Note caps.)

Exit with out save, ignore write protect.
:q!

Quit.
:q

Save / write
:w

Save and overwrite protection.
:w!

Save and exit
:x. Or, :wq. Or ZZ

Save buffered file as:
:w newfile

Return to point of last save.
:E!

Edit another file with out leaving vi:
:e file2

Some editting commands.

Give a number after command if desired.

Delete from cursor to end of word:
dw

Delete from cursor to end of line.
D

Delete lines.
d

Delete from current line to end of file.
:.,$D

Delete character under cursor:
x

Yank (copy) word from cursor
yw

Yank (copy) line.
yy

Paste after cursor.
p

Paste before cursor.
P

Undo
u

Repeat
.

There are many more of course. Have a look at:
http://www.unix-manuals.com/refs/vi-ref/vi-ref.htm