There're several magnificent utilities on Unix/Linux platforms and one of the best is
vi
, a somewhat raw, but powerful text editor that has been available now for over three decades. It is fairly standard and thus makes you feel comfortable whatever platform you're working on. It is there on all Unix/Linux setups (I mean desktop and server environments ... it's probably not part of most embedded systems

) so you don't have to find the text editor ... it's already there and can do anything you need for system administration. There're lots of tutorials and
cheat sheets, so my point will be to collect some examples with the most frequently used commands.
Let's start in the middle.
How to copy, paste and delete a part of text.
- move the cursor to the starting line of the block you want to move or copy
- mark the start of the block:
ms
(note: the letter "m" is the command, the letter "s" is the marker/label that you assign to the current position)
- move the cursor to the last line of the block to be moved/copied
- mark the line:
me
- copy the lines between (and including) the two markers to a buffer:
:'s,'ey
- move the cursor to the line where you want to copy to
- insert contents of buffer into file:
p
or P
- optionally (if you want to remove the lines from their original location - thus move the selected block) delete the lines between the markers:
:'s,'ed
To make life easier, there're direct commands for copy and move.
After you've marked the start and end of the block, you can move the cursor to the destination and:
- copy the marked block to current line with
:'s,'eco.
- move the block to current position with
:'s,'em.
The dot at the end of the command sequence made the destination of the operation the current line. The copied/moved text will be placed after the current line, so position the cursor accordingly.
How to replace a regular expression in the whole file
:%s/regexp/replacement/g
The
% means that the whole file should be selected for the operation. The
g at the end means that the replace should take place for every occurance within a line.
Basic vi
commands
The following commands work in command mode which is the default mode of vi.
- Move one character to left, down, up, right:
h
, j
, k
, l
- Move one word to right, left:
w
, b
(note: W
and B
do the same)
- Move to start, end of the current line:
0
, $
- Move to start, end of file:
1G
, G
- Move to line number n:
nG
- Scroll a page down, up:
Ctrl + f
, Ctrl + b
- Search forward, backward for regexp pattern:
/regexp
, ?regexp
- Repeat last search in selected, opposite direction:
n
, N
- Display current line number:
Ctrl + g
- Mark current line as x:
mx
(note: x can be any letter in the English alphabet, a-z)
- Copy the current line to the buffer:
yy
- Copy n lines starting with current line to the buffer:
nyy
- Delete current line:
dd
- Delete n lines starting with current line:
ndd
- Undo last edit:
u
- Join lines (the current and the following):
J
(note: this a capital!)
- Join n lines starting with current line:
nJ
- Enter editing mode with insert, replace:
i
, R
Comments
Copy/cut/paste
To copy the area:
y's
To cut the area:
d's
This also makes step 8 obsolete. Remember, after
y
andd
commands you can use any cursor move command.Re: copy/cut/paste
"Remember ..."
I've got a tiny little problem with that: I never knew it so I cannot remember it.
“Remember…” I like
I like this word. It could also mean: “From now on remember that…”
Keep reaching for the limits!