Editing Text Files on SUN Computers (the vi interactive full-screen
editor)
Two modes of operation exist:
a) command mode - editing commands can be executed
b) insert mode - invoked by several commands. All keystrokes typed
while the editor is in this mode are entered into the file. The
command mode is reentered by pressing the "esc"
(escape) key.
To invoke the vi editor:
condor{smithj}5: vi file_name
- Basic vi commands:
i - insert text at the
cursor (enters insert mode)
a - add text (enters insert
mode)
esc - (escape key) returns back to command mode from insert
mode
dd - deletes the entire
line the cursor is on (and stores it in the buffer)
x - delete the character
under the cursor
o - insert text on a new
blank line after the current line (enters insert mode)
O - insert text on a new
blank line before the current line (enters insert mode)
R - replace text (enters
insert mode)
rc - replace the character under the cursor with c
D - delete the
rest of the current line and store it in the buffer
S - delete the rest of
the current line and then enter the insert mode
J - joins the current line
with the next line
G - go to the last line
of the file
^ - go to the beginning
of the current line
$ - go to the end of the
current line
u - undoes the most recent
change or insertion
:n - go to line number n
:w - save file
without quitting vi editor (do it often!)
:wq - save and
quit
ZZ - another way to save
and quit
:q! - quit vi,
without writing changes to the file
- More vi commands:
All the arrow keys move the cursor as expected
ctrl-d - scrolls down (forward) by half a screen
ctrl-u - scrolls up (backward) by half a screen
ctrl-f - scrolls down (forward) one screen
ctrl-b - scrolls up (backward) one screen
ctrl-l - regenerates the screen
w - move to the beginning
of the next word
b - move to the beginning
of the previous word
- Cut and paste operations:
ndd - delete n
lines and store to a buffer (cut operation)
nyy - copy (yank)
n lines and store to a buffer (copy operation)
p - put the contents of
the buffer after the cursor (paste operation)
- Search and replace operations:
/pattern
- search forward to the first occurrence
of pattern
?pattern
- search backward to the first occurrence
of pattern
n - search forward
for the next occurence of pattern
N - search backwards
for the next occurrence of pattern
:s/pattern/replace/ - replace pattern
by replace on the current line only
:n,m s/pattern/replace/ - replace first
occurrence of pattern by replace on lines n
to m
:n,m s/pattern/replace/g - replace all
occurrences of pattern by replace on lines n
to m
:1,$ s/pattern/replace/g - replace all
occurrences of pattern by replace in the entire
file.