Wednesday, September 5, 2012

All About Unix VI Editor

Mastering in VI Editor
Not all of the commands mentioned here are covered in the VI Introductory Guide. There are also far more commands available in vi than are covered here - for further information check your system documentation or get one of the books available on vi.
Special Keys
KeyCommand ModeText Mode
Arrow Keys cursor movement N/A
[CTRL] used with other keys for extra commands insert control characters in text
[ENTER] or [RETURN] down 1 line normal function
[ESC] N/A leave text mode
Space Bar move right 1 character normal function
[TAB] N/A normal function
When a command is shown as a combination of the `^' (caret) and another character, as in ^V, this means press the [CTRL] key first, and hold it down while you press and release the other key.

2. Command Mode vs Text Mode

Vi is always either in command mode or text (input) mode. In text mode any character key pressed is enetered into the file. Most other typewriter keys have their normal effect. On some systems to enter a [CTRL] key sequence, you may first have to enter ^V before the sequence you want. In command mode nearly every key on the keyboard performs some command or modifies the next command. Some of these commands may be difficult to recover from, so be careful in command mode to enter only the commands you wish to enter.
The [RETURN] key is not needed in command mode except with commands that begin with a `:' (colon) and with the search comands.

Mode Change Commands
aappend text after cursor
iinsert text before cursor
oopen new line after current line & add text
Oopen new line before current line & add text
[ESC]leave text mode

3. Cursor Motion

Cursor motion should nominally include use of the cursor keys. If your terminal lacks or has problems with arrow keys in vi command mode, the `h', `j', `k', and `l' keys will perform the same functions, as noted below.
Cursor Motion Commands
hmove back 1 character
lmove forward 1 character
jmove down 1 line
kmove up 1 line

bback to beginning of word
eforward to end of word
wforward to beginning of next word
^go to first displayable character of line
0go to beginning of line
$go to end of line

Fforward 1 screen
Bbackward 1 screen
Ddown (forward) 1/2 screen
Uup (backward) 1/2 screen

4. Editing Commands

These are the commands that actually manipulate text. the commands listed here include those which delete, replace, search, cut, and paste text, as well as those used for saving text and abandoning an edit session. Finally, a few miscellaneous commands are included which don't fit into the other categories.

4.1. Delete (Cut) Commands

In some editors the delete commands may be called cut commands. The last item deleted is saved in a buffer and may be put (or pasted) elsewhere in the file as noted later.
Delete Commands
xdelete character
dwdelete rest of word
d$delete rest of line
Ddelete rest of line
dddelete line

4.2. Replace, Change and Substitute Commands

Some editors provide commands to substitute new text occurances of search strings; this is not the same thing. These following vi commands allows you to replace some number of characters, words, lines or parts of lines with new text. The change and substitute commands, the editor puts you into insert mode until you press [ESC]. The replace command replaces 1 (or more) characters with the next character you type.

Replace Commands
rreplace character
cwchange rest of word
c$change rest of line
Cchange rest of line
Nssubstitute text for N characters

4.3. Search Commands

These commands let you search for a text string, which may include regular expressions. They must be followed by a [RETURN].
Search Commands
/textsearch forward for text
?textsearch backward for text
nsearch in same direction for next occurance of last-searched-for text
Nsearch in other direction for next occurance of last-searched-for text

4.4. Undo Command

IN the real vi, the undo command only undoes the last command, even if that is an undo command. Repeated undo commands simply toggle the effect of the last command before the series of undo commands. Some vi clones, such as vim, allow multiple undo's. These usually offer a way to set the undo level to the standard vi mode. See your editor's man page or reference manual for details.

Undo Command
uundo last command (BE CAREFUL WITH THIS)

4.5. Saving & Exiting Commands

These commands must be followed by a [RETURN].
Saving & Exiting Commands
:wwrite (save) the file
:w NAMEwrite the file and name it NAME
:w! NAMErewrite the file named NAME
:wqwrite the file and quit the editor
:qquit the editor
:q!quit the editor (abandoning any changes)

4.6. Search & Replace Command

To find & replace all occurances of a particular text string, use the command:
:%s/text1/text2/g[RETURN]
which replaces all occurances of text1 with text2.

4.7. Yank (Copy) Commands

The vi yank command is similar to the copy command in many editors. It copies the text into a buffer. The text may be put (pasted) elsewhere in the file as described under the Put command.
Yank Commands
ywyank rest of word
y$yank rest of line
yyyank entire line
Yyank entire line

4.8. Put (Paste) Commands

The vi put command is similar to the paste command in many other editors. It will paste whatever is in the buffer from either the previous delete or yank command.
Put Commands
pput yanked/deleted text before cursor
Pput yanked/deleted text after cursor
If you yanked/deleted a whole line (or group of lines), p and P paste the text before or after the current line, respectively; otherwise, they paste the text before or after the cursor, respectively, on the current line.

4.9. Miscellaneous Commands


Miscellaneous Commands
.repeat last command (BE CAREFUL WITH THIS)
Jjoin next line to end of this line

NOTE
The period, or dot, command repeats most commands, but not quite all. It should work with all of the commands listed in this document; if you aren't sure of what you're doing, save your work before you try something.

No comments:

Post a Comment