| ]

The Visual Editor (vi) is the standard text editor used in Linux operating system. You can use the vi editor to write, edit, and save simple text files, programs, shell scripts, Structured Query Language (SQL) queries, and procedures. The vi editor also provides commands that enable you to work with multiple files.

This ReferencePoint introduces the vi editor and discusses the commands to create a text file, insert text, and save text in the file. It also discusses advanced commands used to view the display status, customize settings, and work with multiple files.

Introducing the vi Editor

The vi editor is used to edit ASCII text files in Linux. You can open the vi editor using the vi command. The vi editor opens in the default command mode. The three modes of operation in the vi editor are:

  • The command mode

  • The insert mode

  • The ex mode

You can switch between these modes by using the keys listed in Table 5-1-1:

Table 5-1-1: Keys Used to Switch Between Modes
Open table as spreadsheet

Mode

Keys Used

command mode to insert mode

A, a, I, i, O, o, R, r, S, s

insert mode to command mode

Escape

command mode to ex mode

Colon

ex mode to command mode

Enter

The Command Mode

The command mode is the default mode and is used to execute commands in the vi editor. If the vi editor is in the insert or ex mode, you need to press the ESC key to switch to the command mode. When you press a key in this mode, the Linux shell interprets these keystrokes as commands.

The Insert Mode

To insert text in a file, you need to switch to the insert mode. In this mode, the characters that you type appear on the screen and are stored in the buffer. A buffer is a memory space where data is stored temporarily. Figure 5-1-1 shows the vi editor in the insert mode:

Click to collapse
Figure 5-1-1: The vi Editor in the Insert Mode

The ex Mode

You use this mode for file-handling operations, such as quitting from and saving a file. You can switch to the ex mode by typing a colon in the command mode. Figure 5-1-2 shows the vi editor in the ex mode with the command to quit the vi editor:

Click to collapse
Figure 5-1-2: The vi Editor in the ex Mode

Working with Files

The vi editor provides commands to create, save, modify, and edit a file. In addition, it provides various commands to insert text in a file and access multiple files.

Creating a File

To create a file called abc, in the vi editor, type:

vi abc

If a file called abc already exists, the vi editor opens it; otherwise, the vi editor creates a new file called abc. Figure 5-1-3 shows the vi editor window with a new file, abc:

Click to collapse
Figure 5-1-3: The vi Editor Window with a New File

When you open a file in the vi editor, the cursor is always positioned at the first character of the first line. The tilde character at the beginning of a line indicates that the line is empty.


Note

In the vi editor window, the position where the cursor is placed is the current cursor position and the line at which the cursor is placed is the current line.

Inserting Text in a File

To insert text in the abc file:

  1. Open the abc file in the vi editor, using the command:

    vi abc
  1. Press I or i to switch to the insert mode.

  2. Type text in the file. Figure 5-1-4 shows the vi editor window after inserting text:

    Click to collapse
    Figure 5-1-4: The vi Editor Window After Inserting Text

To insert a word in the abc file:

  1. Open a file called abc in the vi editor, using the command:

    vi abc
  1. Navigate to the position where you need to insert a word.

  2. Press I or i to switch to the insert mode.

  3. Type the required word.

Figure 5-1-5 shows the inserted word, three, in a file called abc in the vi editor window:

Click to collapse
Figure 5-1-5: The vi Editor Window with the Inserted Word in the abc File

Navigating in a File

In the vi editor, you can navigate horizontally and vertically in a file. Table 5-1-2 lists the commands used to navigate horizontally in a file:

Table 5-1-2: Commands for Navigating Horizontally in a File
Open table as spreadsheet

Command

Description

h or Ctrl + H

Moves the cursor input spaces before the current cursor position

l or

Moves the cursor input spaces after the current cursor position

Ctrl

Moves the cursor to the first character of the current line

|

Moves the cursor to the th space after the current cursor position

0

Moves the cursor to the beginning of the current line

)

Moves the cursor after sentences from the current cursor position

(

Moves the cursor before sentences from the current cursor position

f

Moves the cursor to the th occurrence of the specified character after the current cursor position

t

Moves the cursor one input space before the th occurrence of character after the current cursor position

F

Moves the cursor to the th occurrence of the specified character before the current cursor position

T

Moves the cursor to one input space after the th occurrence of the specified character from the current cursor position

w or W

Moves the cursor words after the current cursor position

b or B

Moves the cursor words before the current cursor position

e or E

Moves the cursor to the end of the th word after the current cursor position

Table 5-1-3 lists the commands for navigating vertically in a file:

Table 5-1-3: Commands for Navigating Vertically in a File
Open table as spreadsheet

Command

Description

j or Ctrl + N

Moves the cursor after the th line from the current line

k or Ctrl + P

Moves the cursor before the th line from the current line

$

Moves the cursor to the end of the th line from the current cursor position

_

Moves the cursor to the first character of - 1th line below the current line

+

Moves the cursor to the first character of the th line below the current cursor position

-

Moves the cursor to the first character of the nth line above the current cursor position

}

Moves the cursor after the th paragraph from the current cursor position

{

Moves the cursor before the th paragraph from the current cursor position

G

Moves the cursor to the th line from the beginning of the file

H

Moves the cursor to the th line from the first line of the window

M

Moves the cursor to the middle line of the window

:

Moves the cursor to the th line after the current cursor position

:+

Moves the cursor to the +th line after the current line

:-

Moves the cursor to the -th line before the current line

Ctrl + f

Scrolls to the next screen

Ctrl + b

Scroll to the previous screen

Ctrl + d

Scrolls to half a screen after the current cursor position

Ctrl + u

Scrolls to half a screen before the current cursor position

Editing a File

The vi editor provides commands to edit text in a file. It provides commands to undo changes, delete, copy, and paste the text of a file.

Undoing Changes

The vi editor provides commands to undo the changes made to a file. Table 5-1-4 lists the commands used to undo changes:

Table 5-1-4: Commands to Undo Changes
Open table as spreadsheet

Command

Description

U

Revokes the changes made to the file

:u

Revokes only the last change made to the file

:q!

Quits the editor without saving the file

Deleting Text

The vi editor provides commands for deleting text in a file. Using the commands in the vi editor, you can delete lines, words, and characters from a file. Table 5-1-5 lists the commands used for deleting text:

Table 5-1-5: Commands for Deleting Text
Open table as spreadsheet

Command

Description

x

Deletes characters after the current cursor position

X

Deletes characters before the current cursor position

dd

Deletes lines below the current line

D

Deletes the line after the current cursor position

:d

Deletes the lines from line to

Copying and Pasting Text

You can use the Y command to copy text. This process of copying text is also called yanking. The text is copied in a temporary location called a buffer. You can use the P command to paste text from the buffer at the required location. Table 5-1-6 lists the commands for copying and pasting text:

Table 5-1-6: Commands for Copying and Pasting Text
Open table as spreadsheet

Command

Description

yy

Copies n lines below the current line.

Y

Copies the current line.

:y

Copies lines from line to in the specified buffer. When you use upper case letters, the copied text is appended to the buffer.

P

Pastes the copied lines at the current cursor position.

y

Navigates to the position specified by > command and copies lines

For example, to copy and paste three lines from the current cursor position:

  1. Type 3yy to copy three lines from the current cursor position. Figure 5-1-6 shows the vi editor window after three lines are copied:

    Click to collapse
    Figure 5-1-6: The vi Editor Window After Copying Three Lines

  1. Place the cursor at the required position.

  2. Type P to paste the copied lines at the required position. Figure 5-1-7 shows the vi editor window after the text is pasted:

    Click to collapse
    Figure 5-1-7: The vi Editor Window with the Updated File

Searching and Replacing Text in a File

You can search and replace text in the vi editor. Table 5-1-7 lists the commands used to search text in the vi editor:

Table 5-1-7: Commands to Search Text in the vi Editor
Open table as spreadsheet

Command

Description

/

Moves the cursor to the next occurrence of the specified string from the current cursor position

??

Moves the cursor to the previous occurrence of the specified string from the current cursor position

//+

Moves the cursor to the th line below the next occurrence of the specified string from the current cursor position

//-

Moves the cursor to the th line above the previous occurrence of the specified string from the current cursor position

:g/

Searches from line to for the specified string and executes the specified command at each occurrence of the specified string

n

Searches for the string below the current occurrence of the string

N

Searches for the string above the current occurrence of the string

To search for a string called mode in the abc file:

  1. Open file abc in the vi editor, using the command:

    vi abc
  1. Search for the string in the file using the command:

    /mode

Figure 5-1-8 shows the vi editor window with the output of the command to search the string mode in the abc file:

Click to collapse
Figure 5-1-8: The vi Editor Window with the Search Command

The last line of the figure indicates the command used and the cursor position indicates the first occurrence of the string being searched. Table 5-1-8 lists the commands to replace text in the vi editor:

Table 5-1-8: Commands for Replacing Text
Open table as spreadsheet

Command

Description

R

Replaces the specified character at the current cursor position

R

Replaces text after the current cursor position

s

Replaces a character at the current cursor position with any number of characters

S

Replaces the current line

The syntax for replacing a text with a specified text within a file is:

:1,$s/string1/string2/g

When the above command is executed, string2 replaces string1. 1,$ specifies that all lines of the files will be searched.

The syntax for a command that limits the search area is:

:n1,n2s/string1/string2/g

In the above command, n1 and n2 specify the lines that are searched for string1. When found, string1 is replaced with string2.

The syntax for specifying a command to replace a string after asking for a confirmation from the end user is:

:n1,n2s/string1/string2/gc

In the above command, the character c at the end of command specifies that before replacing string1 with string2 a confirmation from the end user would be taken.

For example, the syntax for a command that searches for the string editor in a file and replaces it with the string window after taking a confirmation from the end user is:

:1,6s/editor/window/gc

Figure 5-1-9 shows the vi editor window when you execute the above command:

Click to collapse
Figure 5-1-9: The vi Editor Window with the Output of the Substitute Command

In the figure, the first occurrence of the string editor is highlighted. The last line shows the confirmation message.

Saving a File

You need to save a file after you have created it. You should be in the ex mode to save a file. Table 5-1-9 lists the commands for saving a file in the vi editor:

Table 5-1-9: The Commands for Saving a File
Open table as spreadsheet

Command

Description

:w

Saves the current file

:w

Saves the current file with the specified filename

:wq

Saves the current file and quits the vi editor

:.w

Saves the current line of the file to the specified file

To save the abc file:

  1. Press the Escape key to switch to the command mode.

  2. Type colon : to switch to the ex mode.

  3. Type w to save the file.

Figure 5-1-10 shows the vi editor window when you save a file using the :w option:

Click to collapse
Figure 5-1-10: The vi Editor Window with the Command for Saving a File

When you save a file, the Linux operating system saves the contents of the file to the hard disk and shows file information in the last line of the vi editor window. Figure 5-1-11 shows the status of the saved file in the vi editor window:

Click to collapse
Figure 5-1-11: The vi Editor Window with the Information of the Saved File

In the last line, "abc" specifies the file name, 6L specify the number of lines in the file, and 119C specifies the total number of characters in the file.

Advanced Commands

The vi editor provides commands to display file information, customize vi editor, shell commands, and work with multiple files.

Displaying File Information

You use the Ctrl+G command to display the file name, current line status, and cursor position. Figure 5-1-12 shows the vi editor window with the output of the Ctrl+G command:

Click to collapse
Figure 5-1-12: The vi Editor Window with the output of the Ctrl+G Command

In the last line, "abc" specifies the file name, line 1 of 6 specifies that the cursor is placed at the first line, 16% specifies the percentage of file used by the current line. col 1 specifies the current cursor position.

Customizing the vi Editor

You can customize the vi editor according to your requirements. The commands to customize the vi editor are:

  • The set command

  • The Mapping command

  • The Abbreviate command

The set Command

The vi editor provides you with various set options for working with a file, To view all the set options, use the command:

:set all

Table 5-1-10 lists the set options provided in the vi editor:

Table 5-1-10: The set Options in the vi Editor
Open table as spreadsheet

Option

Description

ai

Applies current indentation to the new line

ic

Ignores the case of letters while searching for a specific pattern of text

nu

Displays line numbers in the current file

sm

Matches ( with ), { with }, and [ with ]

ts

Sets a value for the tab key

ws

Search for specified text in the entire file

The syntax for using the nu, set command is:

:set nu

Figure 5-1-13 shows the result of the :set nu command in the vi editor window:

Click to collapse
Figure 5-1-13: The vi Editor Window with the Output of the :set nu Command

The Mapping Command

You need to use the map command in the ex mode to associate a function to a key. You can either associate a function with an undefined key or associate a new function with a defined key. A function assigned to a key is executed when you press a key. Table 5-1-11 lists the map commands:

Table 5-1-11: The Map Commands
Open table as spreadsheet

Command

Description

:map

Assigns the function to the specified string

:map!

Assigns the function to the specified string in the append mode

:map

Shows all the mappings in the vi editor

:map!

Shows all the append mode mappings in the vi editor

:unmap

Cancels the mapping assigned to string

:unmap!

Cancels the mapping assigned to string in the append mode

The Abbreviate Command

You can use the abbreviate command in the ex mode, to specify abbreviations for words. When you type an abbreviation in the insert mode, the vi editor expands it automatically. Table 5-1-12 lists the abbreviate commands:

Table 5-1-12: The Abbreviate Commands
Open table as spreadsheet

Command

Description

:ab <> <>

Abbreviates word as wr

:ab

Shows all the abbreviations in the vi editor

:unab

Disables ab as an abbreviation

Using Shell Commands in the vi Editor

The shell acts as a command interpreter. Table 5-1-13 lists the shell commands that you can use in the vi editor:

Table 5-1-13: Shell Commands in the vi Editor
Open table as spreadsheet

Command

Description

:sh

Executes a subshell

Ctrl + D

Switches to the vi editor from the shell

:!

Executes the shell command and passes lines to as input

:!!

Repeats the previous shell command and appends the option to the shell command

!
>

Executes the shell command with input as

!
>!

Appends to the previous shell command with the input specified by >

!!

Specifies lines from the current cursor position as the standard input to the shell command

!!!

Appends to the previous shell command and executes the shell again

:r!

Moves the output of the shell command to the line above the current line

:r

Reads the specified file into the buffer

Working with Multiple Files

You can work with multiple files in the vi editor when in the ex mode. The vi editor provides command to switch between files, split the vi editor window, and move text between files.

Commands to Switch Between Files

You can open multiple files in the vi editor. The syntax to invoke the vi editor with multiple files is:

vi filename1 filename2 filename3

In the above syntax, the vi editor opens files in the order in which they are listed.

The vi editor provides various commands to switch from one file to another without switching to the command prompt. Table 5-1-14 lists the commands to switch between files:

Table 5-1-14: Commands to Switch Between Files
Open table as spreadsheet

Command

Description

:n

Edits the next file if the vi editor is invoked with multiple file names

:rew

Edits all files that are open in the vi editor

:r

Shows the contents of the specified file below the current line of the current file

:w >> 

Appends buffer content to the specified file

:e

Switches from the current file to the specified file in the vi editor

:e!

Discards the changes made to the current file and switches to the specified file

:e!

Opens the last saved version of the file

e#

Switches to the recently edited file

Splitting the vi Editor Window

You can split the vi editor into multiple windows and view different files in these windows. To split the vi editor window into two windows:

  1. Invoke the vi editor using the command:

    vi abc
  1. To split the vi editor, use the command:

    :new filename
  1. Press the Enter key. The vi editor window splits into two windows. The first window shows file abc and the second window shows the file called filename.

    Figure 5-1-14 shows the vi editor window with multiple windows:

    Click to collapse
    Figure 5-1-14: The vi Editor Window with Multiple Windows

    In the above figure, the highlighted line with text, abc, is the demarcation between the two files.

Moving Text between Files

The vi editor enables you to store blocks of text in a named buffer. The text stored in this buffer can be pasted at the required location. There are twenty-six named buffers provided in the vi editor. The syntax for saving text in a buffer is:

"

In the command, refers to the buffer, and refers to the command used to copy or delete a line or text. Switch to the file, file1, using the command:

:e file1

Place the cursor at the required location and paste the text using the command:

"p

In the above command, ch refers to the buffer that contains the text to be pasted.

You need to use an upper case alphabet that specifies buffer name to append text to a buffer. The syntax to append text to a buffer is:

"

In the above command, is the buffer name.

For example, if you need to copy three lines of text from the def file and paste them in the abc file:

  1. In the def file, type:

    "a3yy
  1. To append the text in the buffer, type:

    "A2yy
  1. To switch to the abc file, type:

     :e abc
  1. Move the cursor to the required location.

  2. To paste the text, type:

    "ap