Ict-innovation/LPI/103.8

103.8 Using Vi
''Candidates should be able to edit text files using vi. This objective includes vi navigation, basic vi modes, inserting, editing, deleting, copying and finding text.''

Key Knowledge Areas


 * Navigate a document using vi.
 * Use basic vi modes.
 * Insert, edit, delete, copy and find text.

In most Linux distributions vi is the text editor of choice. It is considered an essential admin tool such as grep or cat.

Modes
In order to perform complex operations such as copy/paste, vi can operate in different modes.

Command mode

This is the editing and navigation mode. Commands are often just a letter. For example use j to jump to the next line.

As a rule of thumb if you want to perform an operation several times you can precede the command by a number. For example 10j will jump 10 lines.

In some situations the arrow keys on the keyboard are not mapped properly, it is still possible to navigate using the commands h j k l with the following effect:



Last Line (or column) Mode

You enter this mode from the command mode by typing a colon'''. '''The column will appear at the bottom left corner of the screen. In this mode you can perform a simple search operation, save, quit or run a shell command.

Insert Mode

The easiest way to enter this mode while in command mode is to use i or a. This mode is the most intuitive and is mainly used to interactively enter text into a document.

 The Esc key will exit the insert mode and return to command mode

Text Items
Items such as words and paragraphs are defined in command mode to allow editing commands to be applied to text documents without using a mouse.

Word, sentences and paragraphs

Beginning and End

All these text items can be used to navigate through the text one word (w) or paragraph (})at a time, go to the beginning of a line (^) the end of the file (G) etc. One can also use these text items to execute commands such as deleting and copying.

Inserting Text

When in command mode typing i will allow you to enter text in the document interactively. As with all other features in vi there are many other ways of doing this. The table below lists the commands used to enter insert mode.

Insert commands

A very useful option when modifying a document is to delete a section of text you wish to replace just before entering insert mode. This is done by the change c command. As the other commands in this section c will put you into INSERT mode but you can specify which portion of the text needs to be deleted before. For example:

c$

will delete all the text from the current cursor position to the end of the line.

Another command used to replace a single character (nothing else!) is r. First choose which character needs to be replaced and put the cursor on this character. Next press r followed by a new character. The new character will replace the old one. This command will leave the editor in COMMAND and not INSERT mode!

Cut and Paste

If you want to delete a single character while in command mode you would use x. Use dd to delete the current line. One can then paste the deleted item with the command p.

Remark: Nearly all vi commands can be repeated by specifying a number in front of the command. You can also apply the command to a text item (such as word., sentence, paragraph ...) by placing the entity after the command.

Examples:

dw Delete a word:

d$ Delete text from here to the end of the current line

d} D elete text from here to the end of the current paragraph

One can simultaneously delete an item and switch to insert mode with the c command. As usual you can use this command together with a text item such as w or {.

Copying and Pasting

The copy action in vi is the command y (for yank, the letter c was already taken for change), and the paste action is still p.

If an entire line is yanked the pasted text will be inserted on the next line below the cursor.

The text selection is made with the familiar text items w, l, }, $ etc ... There are a few exceptions such as the last example.

Examples:

y$ Copy the text from here to the end of the current line

yy Copy the entire current line

3yy Copy 3 lines

 The latest deleted item is always buffered and can be pasted with the p command. This is equivalent to a cut-and-paste operation.

Search and Replace

Since searching involves pattern matching we find ourselves once again dealing with regular expressions (regex). Like many UNIX text manipulation tools such as grep or sed, vi recognises regular expressions too.

To perform a search one must be in COMMAND mode. The / (forward slash) command searches forward and the ? command searches backwards.

One can also perform search and replace operations. The syntax is similar to sed.

Example:

 /\<comp Downward search for words beginning with ‘comp’ in all the text

?^zUpward search for lines starting with the letter z



Undo and Redo

At this stage is is worth mentioning that one can always undo changes! This must be done in COMMAND mode with the u command (works as long as one hasn’t yet saved the file). The redo command is ^R.

Running a Shell Command
While in LASTLINE mode everything following an exclamation mark ! is interpreted as a shell command.

For example while editing lilo.conf or grub.conf you may need to find out the name of the root device. This can be done with:



Save and Quit

The command for saving is :w. By default the complete document is saved. In some case vi will refuse to save changes made to a document because of insufficient rights. In such cases one can attempt to force a write with :w!

One can also specify an alternative name for the file. Portions of the text can be saved to another file while other files can be read and pasted in the current document. Here are the examples which illustrate this.

Examples:





The text will be pasted at the cursor

Warning - In the column mode context we have the following

.  - is the current line

$ - is the end of the document

The following are different ways available to quit vi:











ZZ - same as :x

Used files, terms and utilities:


 * vi
 * h,j,k,l
 * i, o, a
 * c, d, p, y, dd, yy
 * ZZ, :w!, :q!, :e!
 * ZZ, :w!, :q!, :e!

Previous Chapter | Next Chapter