vi

vi, pronounced “vee-eye,” is the standard text editor for UNIX. The command used to enter the vi editor and create a new file is vi. Normally, when we use the editor, we specify a filename as follows:

vi filename <cr> 

When you enter the command and do not specify a filename, you’ll be editing an unnamed file.

vi modes

Text editors with which you are probably already familiar will allow you to open a file and begin entering text. To delete text, you simply press the Delete key, and the copy and paste functions are usually handled through a pull-down menu. With vi however, it’s not quite that easy. For one, it does not have a toolbar, icons, or pull-down menus.

Because the vi editor grew out of the ed editor, you’ll recognize many similarities, one of which is the use of modes. vi has two primary modes: command mode and input mode. Understanding modes in vi is the key to using vi efficiently, but it is a difficult concept for new users to understand.

In the command mode, you are not performing tasks that directly input text into the file. In command mode, you’ll specify commands to save, edit, search, and replace. For example, to save a file, you’ll enter the w (write) command. To copy and paste text, you’ll enter y (yank) and p (paste).

In input mode, you’ll enter text into your file. You will not be able to perform the operations available in command mode while you are in input mode. In other words, you won’t be able to cut and paste while in input mode.

To determine which mode you are currently in, type the following command while in command mode in the vi editor:

:set showmode 

The showmode option will indicate the vi mode you are in with one of the following messages displayed in the lower-right corner of the screen:

INSERT MODE This indicates that you are in insert mode, where you can begin entering text. Text will be inserted before the cursor.
APPEND MODE This indicates that you are in append mode, where you can begin entering text. Text will be inserted after the cursor.
OPEN MODE This opens a blank line within the existing file, where you can begin inputting text.
REPLACE MODE This indicates that you are in replace mode, where you can overwrite text (replacing the current text with new text).
No message If no message displays, you are in command mode.

Command Mode

Upon entering the vi editor, you’ll be placed into command mode. Within command mode, you’ll be able to type in various commands, which are typically one-letter commands. (I’ll describe these more in later sections.) Whenever you want to exit input mode and get back into command mode, press the Esc key.

Note

Don’t be afraid of pressing the Esc key more than once. If you’re not sure which mode you are in, press Esc. It’s not uncommon to see vi users hit Esc multiple times just to make sure they are in command mode.


Input Mode

The four input modes are insert, append, replace, and open. While vi is in one of the input modes, you are able to place new text into the buffer. The commands used to get into input mode from command mode are described in Table 2.1. When in command mode, you’ll type the letter representing one of the commands in Table 2.1, and the character will not be displayed on the screen. In fact, if you do not have showmode set as described in the preceding section, you won’t know that anything has happened until you start to type and actually see characters being displayed on the screen.

Table 2.1. Input Commands
Command Description
i The lowercase i inserts text that you enter before the character the cursor is currently on.
I The uppercase I also places you into input mode but begins placing text at the beginning of the current line.
a The lowercase a is similar to the i command except that new text will be placed after the current character.
A The uppercase A is similar to the I command except that new text will be placed after the last character on the current line.
o The lowercase o command opens a blank line within the existing text. The new line will be opened below the current line, and the cursor will be placed at the beginning of the new line.
O The uppercase O command opens a blank line within the existing text. The new line will be opened above the current line, and the cursor will be placed at the beginning of the new line.
r The lowercase r command causes the next character that you enter to overwrite (replace) the character that the cursor is currently on. Only one character will be replaced. To replace additional characters, type the r command again.
R The uppercase R command causes the new text that you enter to overwrite (replace) the existing text that the cursor is currently on. Unlike the lowercase r, however, you can continue to replace text until the Esc key is pressed.

Example 1:

In this example, I’ll insert new text into a text file. When I enter vi with the following command:

vi file2 <cr> 

the following lines of text appear on the screen. Notice where I’ve positioned the cursor.

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

To get into input mode, I type i and start typing:

This is a new text file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

Notice how the text was entered before the current cursor position, and all of the existing text was pushed to the right.

Example 2:

In this example, I’ll append new text into a text file. When I enter vi with the following command:

vi file2 <cr> 

the following lines of text appear on the screen. Notice where I’ve positioned the cursor.

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

To get into input mode, I type a and start typing:

This is a file which I've named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

Text is appended after the cursor, and the existing text is pushed out to the right.

Example 3:

In this example, I’ll overwrite existing text within an existing text file. When I enter vi with the following command:

vi file2 <cr> 

the following lines of text appear on the screen. Notice where I’ve positioned the cursor.

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

To get into replace mode, I’ll type R and start typing:

This is a file named TEST1. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

The word file2 gets overwritten with the word TEST1.

Example 4:

In this example, I’ll add a new line of text within an existing text file. When I enter vi with the following command:

vi file2 <cr> 

the following lines of text appear on the screen. Notice where the cursor is positioned.

							This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

I’ll type a lowercase o to insert a new line of text after the current line, and the screen shows the following:

This is a file named file2. 

							
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

As I type, text will appear on the new line.

Deleting Text

To delete text or lines of text within a text file, use one of the delete commands displayed in Table 2.2. These commands are only issued from within command mode. Therefore, get used to pressing the Esc key before issuing these commands, just to make sure you are in command mode.

Table 2.2. Delete Commands
Command Description
<n>x The x command deletes the current character only. You can precede the x with a repeat factor. This repeat factor repeats the command a number of times to delete several characters at once, beginning at the current cursor position.
d<n> The d command removes text from the buffer. Use the repeat factor after the command to repeat the command any number of times. There are several delete commands that can be used. Here are some of the more common ones:
  d0 Delete all characters in the current line from the beginning of the line to the current cursor position.
  dw Delete all characters from the current cursor position to the end of the word.
  d<n>w Delete all characters from the current cursor position to the end of the <n> word.
  dd Delete the current line.
  <n>dd Delete <n> lines starting with the current line.

Note

You’ll notice the use of the repeat factor in several vi commands. A repeat factor is a value that specifies the number of times a command is to be executed.


Example 1:

In this example, I’ll delete all characters from the beginning of the line to the current cursor position using d0. Note the current cursor position.

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 

The result will look like this:

file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 

Example 2:

In this example, I have the following five lines of text in my file:

							This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't like ed, I like vi better. 
vi rules! 

To delete four lines of text beginning from the current line, I’ll use d4d and the result will look like this:

vi rules! 

Note

The . (dot) command repeats the most recent command that made a change. To re-execute the previous command, simply enter a . (dot) from the command mode.


Searching for a String

You can search forward or backward in the vi buffer to find a specific string of text. To search forward for a text string, first make sure you are in command mode by pressing the Esc key. Press the / (slash) key, and the cursor will go to the bottom of the screen. After the /, enter the text string to search for. After entering the text string, press the Return key, and the cursor will jump to the first occurrence of the text string. Press the / key followed by Return, and the cursor will jump to the next occurrence.

Use N (uppercase) or n (lowercase) to repeat the last search without entering the text string again. The lowercase n repeats the original search exactly; the uppercase N searches in the opposite direction.

Substitution

A substitute command in vi uses the same syntax that we used in the ed editor. Remember that ed and vi are the same editor; vi is simply a full-screen version of ed. To substitute a text string, we’ll use an ed editor command. Remember that, in the ed editor, we used

1,$s/ed/vi/ 

to substitute every occurrence of the word ed with vi. We’ll do it the same way in vi. To call an ed command in the vi editor, type a : (colon) from the command mode. After typing the colon, the cursor will jump to the bottom of the screen and will wait for you to enter the ed command. Type in the command, press Return, and the substitution will occur.

Copy and Paste

As previously stated, vi utilizes a work buffer to hold text during an editing session. To copy and paste text, we first “yank” (copy) text and then “put” (paste) the text to another location within the work buffer. Therefore, in vi “yank and put” refers to copy and paste. To cut and paste text, we first “delete” (cut) text from one location of the buffer and then “copy” (paste) it to another location.

The yank commands are identical to the delete commands except they do not actually delete text from the work buffer. vi places a copy of the yanked text into the general-purpose buffer so that you can use put to place another copy of it elsewhere in the work buffer.

Note

vi stores the text that you most recently changed, deleted, or yanked in the general-purpose buffer. Do not confuse the general-purpose buffer with the work buffer that holds the entire file while we are working within vi.


See Table 2.3 for a description of the commonly used yank commands.

Table 2.3. Yank Commands
Command Description
y<n> The y command copies text from the buffer. Use the repeat factor after the command to repeat the command any number of times. Several yank commands can be used. Here are some of the more common ones:
  y0 Copies all characters in the current line from the beginning of the line to the current cursor position.
  yw Copies all characters from the current cursor position to the end of the word.
  y<n>w Copies all characters from the current cursor position to the end of the <n> word.
  yy Copies the current line.
  <n>yy Copies <n> lines starting with the current line.

The put commands, P and p, copy text from the general-purpose buffer into the work buffer. Use P (uppercase) to insert the yanked characters before the current cursor position. Use p (lowercase) to insert the yanked characters after the current cursor position.

Note

After yanking text, you can use any of the cursor-positioning commands described in the next section to move around within the text file before inserting the text using a put command.


Just as the yank commands will save text in the general-purpose buffer, the delete commands also save text in this buffer. Therefore, after deleting text, you can use the put command to reinsert the text to a different location to perform a “cut and paste” operation.

Navigating in vi

Most of us use the arrow keys to navigate within the vi editor. These are the easiest to remember and are the most straightforward to use. If, however, your keyboard does not have arrow keys, you can use the following keys to move around within vi:

h Moves left one character at a time
j Moves down one line at a time
k Moves up one line at a time
l Moves right one character at a time

In addition to the arrow keys, you can use the following keystrokes to move around within vi:

^ Use the ^ (caret) to move to the beginning of the current line.
$ Use the $ (dollar sign) to move to the end of the current line.
:1 Type a : (colon) and then 1 to move to the first line of the text file.
:$ Type a : (colon) then a $ (dollar sign) to move to the last line of the text file.
Ctrl+d Press Ctrl+d to scroll down, one half screen at a time.
Ctrl+u Press Ctrl+u to scroll up, one half screen at a time.
Ctrl+b Press Ctrl+b to scroll backward, one full screen at a time.
Ctrl+f Press Ctrl+f to scroll forward, one full screen at a time.
:<n> Type a : (colon) and then a line number to move to that specific line within the text file.

Note

To display line numbers in your text file, enter the following command while in command mode:

:set number <cr> 

The line numbers are displayed but are not stored with the file and are not printed. They only appear on the screen while you are using vi.


Saving Your Work

There are many different ways to save (write) your work to disk. Sometimes you just want to exit vi without saving any of the work you’ve done. Table 2.4 lists some of the more common vi commands you can use from the command mode to save and/or exit the vi editor.

Table 2.4. Save and Terminate Commands
Command Description
:wq, ZZ, :x Writes the contents of the work buffer to disk and terminates the vi session.
:w! Writes the contents of the work buffer to disk but stays in the current vi session. The exclamation point infers a forced write. If you are logged in as root, you can use this command to write to a filename even though the file is write protected.
:w <filename> Saves the contents of the work buffer to a different filename.
:q! Quits the editor without saving any of the modifications that were made to the text file.

Example 1:

To save the current modifications to a file named test, use the following command from within vi:

:w test <cr> 

Example 2:

The following command will save the modifications you’ve made to the text file and will terminate your vi session:

ZZ 

Setting Your vi Environment

I’ve already described how to set a few options in the vi editor such as using

:set number 

to display line numbers and using

:set showmode 

to display which mode you are using in vi. To set a parameter while in vi, enter a : (colon), the word set, a space, and the parameter. The command appears at the bottom of the vi screen (status line) as you type it and goes into effect when you press the Return key. Table 2.5 describes a few of the more common parameters you can set while in vi.

Table 2.5. vi Parameters
Parameter Description
number Turns on line numbers.
nonumber Turns off line numbers.
wrapmargin=<n> The line wrap margin causes vi to break each line of text at approximately the specified number of characters <n> from the right margin. vi inserts a newline character at the closest blank-delimited word boundary. The default value is 0.
showmode Displays the current mode in the lower-right side of the screen.
flash Causes the terminal to beep or flash when an invalid command is entered.

Example 1:

To set the vi session to display numbers, display the mode, and flash when an invalid command is entered, type the following:

:set number showmode flash <cr> 

If you are using the Bourne or Korn shell, you can set vi parameters by setting the following shell variable in the shell before entering the vi editor:

EXINT='set number showmode flash' 
Export EXINT 

This variable also could be set in your .profile to make it go into effect every time you log in.

Another way to set these parameters is to create a file named .exrc in your $HOME directory by making the following entry:

set number showmode flash 

When you start the vi editor, vi looks for an .exrc file in your current directory. If it doesn’t find one there, it looks in your $HOME directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset