|
|
|
Using the VI editor (basic commands
only) |
|
The VI editor is a screen-based text editor
available on all Unix computers (and available for all other kinds of computers,
sometimes as "vim" rather than "vi").
Given that it takes some effort, why bother to learn VI? Because:
- sometimes it`s the only available editor
- when you log on remotely (ssh) to a Unix host from a Mac or
PC, only a text editor (like VI or emacs or pico)
can be used to edit files in a text-only terminal window
- mouse movements (e.g., menus, highlighting, clicking, scrolling)
slow down the touch-typist. VI requires none
- as mentioned above, VI is the editor sure to be on every
Unix computer in the world
- VI is a very powerful editor
for those who learn more than just the beginner commands, and even more
powerful for those who are familiar with Unix commands
If you will be using Unix/Linux computers, especially via ssh, save yourself
headaches and learn the basics of VI.
In the following, ^X denotes a control character.
For example, " ^D " means to
hold down the Control key and press the "d"
key. "Rtn" means to press the Return
(or Enter) key, while "Esc" means to press
the Escape key, located in the far upper left
corner of the keyboard. |
1. Starting:
To edit a file named (say) "mytext" on a Unix computer, type the command
"vi mytext". Note that you must type
the command with lowercase letters. |
|
| 2. Two Modes:
Pay attention, this is the crucial feature of VI!   There
are two modes, command and insert.
When in insert mode, everything
you type appears in the document at the place where the blinking cursor
is. When in command mode, keystrokes
perform special functions rather than actually inserting text to the
document. (This makes up for the lack of mouse, menus, etc.!) You
must know which keystroke will switch you from one mode to the other: |
| |
Switch to insert
mode: |
press i
(or a, or o)
|
|
| |
Switch to command
mode: |
press Esc
|
|
|
3. Getting
out:
When you want to get out of the editor, switch to command mode (press
Esc) if necessary, then: |
| |
:wq Rtn
|
to save the edited file and quit |
|
| |
:q! Rtn |
press Esc
|
|
| |
ZZ |
to save and quit (a shortcut
for :wq Rtn) |
|
| |
:w |
to save the edited file to new
file "filename" |
|
|
4. Moving
Around:
When in command mode you can use the arrow keys to move the
cursor up, down, left, right. In addition, these keystrokes will move
the cursor: |
| |
h |
left one character |
|
b |
back one word |
|
^B |
back one page |
|
| |
l |
right one character |
|
f |
forward one word |
|
^F |
forward one page |
|
| |
k |
up one line |
|
{ |
up one paragraph |
|
17G |
to line #17 |
|
| |
j |
down one line |
|
} |
down one paragraph |
|
^G |
to the last line |
|
| |
|
|
|
$ |
to end of the line |
|
|
|
|
|
5. Inserting
Text:
From command mode, these keystrokes switch you into insert mode with
new text being inserted |
| |
i |
just before the current
cursor position |
|
a |
just after the current
cursor position |
|
| |
o |
into a new line below
current cursor |
|
O |
into a new line above
current cursor |
| |
I |
at the beginning
of the current line |
|
A |
at the end of the
current line |
|
6. Cutting,
Copying, Pasting: From command
mode, use these keystroke (or keystroke-combination) commands for
the described cut/copy/paste function: |
| |
x |
delete (cut) character under
the cursor |
|
24x |
delete (cut) 24 characters |
|
| |
dd |
delete (cut) current line |
|
4dd |
delete (cut) four lines |
|
| |
dw |
delete to the end
of the current word |
|
D |
delete to the end
of the line from the cursor |
|
| |
yy |
copy (without cutting) current line |
|
5yy |
copy (without cutting) 5 lines |
|
| |
p |
paste after current
cursor position/line |
|
P |
paste before current
cursor position/line |
|
|
7. Searching
for Text:
Instead of using the "Moving Around" commands, above, you can go directly
forward or backward to specified text using "/"
and "?". Examples: |
| |
/wavelet Rtn
|
jump forward to the
next occurrence of the string "wavelet" |
|
| |
?wavelet Rtn |
jump backward to
the previous occurrence of the string "wavelet" |
|
| |
n Rtn |
repeat the last search given by "/"
or "?" |
|
|
8. Replacing
Text:
This amounts to combining two steps; deleting, then inserting text. |
| |
r |
replace 1 character (under the cursor) with another
character |
|
| |
8r |
replace each of the next 8 characters with a given
character |
|
| |
R |
overwrite; replace text with typed input,
ended with Esc |
|
| |
C |
replace from cursor to end of line, with typed
input (ended with Esc) |
|
| |
S |
replace entire line with typed input (ended with
Esc) |
|
| |
4S |
replace 4 lines with typed input (ended with Esc)
|
|
| |
cw |
replace (remainder of) word with typed input (ended
with Esc) |
|
|
9. Miscelleous:
The commands on these two pages are just the start. Many more powerful
commands exist in VI, especially those which invoke other Unix text-filtering
commands (sort, fmt, uniq, cat, sed, awk, grep, etc.). More complete
descriptions of all the possible commands are available on the web;
see the list of VI manuals/tutorials maintained at
The Vi Lovers Home Page or use a generic web
search to search for "vi tutorial" or "vim tutorial".
Some useful or spectacular "miscellaneous" commands (not in categories
1-8 above): |
| Typed Command |
what it does |
| u |
undo the last change to the file
(and type "u" again to re-do the change)
|
| U |
undo all changes to the current line |
| ^G |
show the current filename and status and line number |
| :set nu Rtn |
show all line numbers
(":set nonu" gets rid of the numbers) |
| ^L |
clear and redraw the screen |
| :%s/Joe/Bob/g Rtn |
change every "Joe" to "Bob" throughout the document
|
| J |
join this line to the next line |
| 5J |
join 5 lines |
| xp |
exchange two characters
(actually the two commands x=delete and p=paste) |
| :w Rtn |
write (save) the current text, but don`t quit VI
|
| :12,17w filename
Rtn |
write lines #12-17 of the current text to a (new)
text file |
| :r filename
Rtn |
read (and insert) contents of a text file |
| !]]sort -u | cat -n |
sort all lines from cursor downwards, deleting
duplicates,
and number the lines |
| :26,$s/\<[a-z]/\U&/g
Rtn |
Capitalize the first letter of each word from line
#26 through
the end of the file |
|