[Unix-Linux] The vi Editor Tutorial

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the Unix/Linux Tutorial!


The Introduction of the vi Editor

The vi editor enables us to edit lines in context with other lines in the file.

An improved version of the vi editor which is called the VIM has also been made available now. VIM stands for Vi IMproved.

vi is generally considered the de facto standard in Unix editors because:

  • It’s usually available on all the flavors of Unix system.
  • Its implementations are very similar across the board.
  • It requires very few resources.
  • It is more user-friendly than other editors such as the ed or the ex.


Starting the vi Editor

Command Description
vi filename Creates a new file, if it already does not exist. Otherwise, opens an existing file.
vi -R filename Opens an existing file in the read-only mode.
view filename Opens an existing file in the read-only mode.

Create a new file ‘testfile’ if it already does not exist in the current working directory:

$vi testfile
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]

A tilde (~) represents an unused line. If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some other non-viewable character present.


Operation Modes

While working with the vi editor, usually come across the following two modes:

  • Command mode
    This mode enables us to perform administrative tasks such as:
    • Saving the files
    • Executing the commands
    • Moving the cursor
    • Cutting(yanking) and pasting the lines or words
    • Finding and replacing
    In this mode, whatever we type is interpreted as a command.
  • Insert mode
    This mode enables us to insert text into the file.
    Everything that’s typed in this mode is interpreted as input and placed in the file.

vi always starts in the command mode. To enter text, we must be in the insert mode for which simply type i. To come out of the insert mode, press the Esc key, which will take us back to the command mode.


Getting Out of vi

Command Description
:q Quit out of vi
:q! Quit out of vi without saving
:w Save the contents of the editor
:wq Quit out of vi with saving the contents
ZZ The easiest way to save changes and exit vi, works same as :wq
:w filename2 Save the file we were working on as another filename called filename2


Moving within a File

To move around within a file without affecting our text, we must be in the command mode.

Commands we can use to move around one character at a time:

Command Description
k Moves the cursor up one line
j Moves the cursor down one line
h Moves the cursor to the left one character position
l Moves the cursor to the right one character position

The following information need to be considered to move within a file:

  • vi is case-sensitive. We need to pay attention to capitalization when using the commands.
  • Most commands in vi can be prefaced by the number of times we want the action to occur. → e.g. 2j moves the cursor two lines down the cursor location.

Commands to move around the file:

Command Description
0 or \| Positions the cursor at the beginning of a line
$ Positions the cursor at the end of a line
w Positions the cursor to the next word
b Positions the cursor to the previous word
( Positions the cursor to the beginning of the current sentence
) Positions the cursor to the beginning of the next sentence
E Moves to the end of the blank delimited word
{ Moves a paragraph back
} Moves a paragraph forward
[[ Moves a section back
]] Moves a section forward
n\| Moves to the column n in the current line
1G Moves to the first line of the file
G Moves to the last line of the file
nG Moves to the nth line of the file
:n Moves to the nth line of the file
fc Moves forward to c
Fc Moves back to c
H Moves to the top of the screen
nH Moves to the nth line from the top of the screen
M Moves to the middle of the screen
L Moves to the bottom of the screen
nL Moves to the nth line from the bottom of the screen
:x Colon followed by a number would position the cursor on the line number represented by x


Control Commands

The following commands can be used with the Control Key to performs functions as given in the table below:

Command Description
CTRL+d Move forward 1/2 screen
CTRL+f Move forward one full screen
CTRL+u Move backward 1/2 screen
CTRL+b Move backward one full screen
CTRL+e Move the screen up one line
CTRL+y Move the screen down one line
CTRL+l Redraw the screen


Editing Files

To edit the file, we need to be in the insert mode. There are many ways to enter the insert mode from the command mode:

Command Description
i Inserts text before the current cursor location
I Inserts text at the beginning of the current line
a Inserts text after the current cursor location
A Inserts text at the end of the current line
o Creates a new line for text entry below the cursor location
O Creates a new line for text entry above the cursor location


Deleting Characters

Commands, which can be used to delete characters and lines in an open file:

Command Description
x Deletes the character under the cursor location
X Deletes the character before the cursor location
dw Deletes from the current cursor location to the next word
d^ Deletes from the current cursor position to the beginning of the line
d$ Deletes from the current cursor position to the end of the line
D Deletes from the cursor position to the end of the current line
dd Deletes the line the cursor is on

Example: 2x deletes two characters under the cursor location and 2dd deletes two lines the cursor is on.


Change Commands

We can change characters, words, or lines in vi without deleting them. Here are the relevant commands:

Command Description
cc Removes the contents of the line, leaving you in insert mode.
cw Changes the word the cursor is on from the cursor to the lowercase w end of the word.
r Replaces the character under the cursor. vi returns to the command mode after the replacement is entered.
R Overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting.
s Replaces the current character with the character you type. Afterward, you are left in the insert mode.
S Deletes the line the cursor is on and replaces it with the new text. After the new text is entered, vi remains in the insert mode.


Copy and Paste Commands

Copy lines or words from one place and then paste them at another place using the following commands:

Command Description
yy Copies the current line.
yw Copies the current word from the character the lowercase w cursor is on, until the end of the word.
p Puts the copied text after the cursor.
P Puts the yanked text before the cursor.


Word and Character Searching

The vi editor has two kinds of searches: string and character.

For a string search, the / and ? commands are used. When we start these commands, the command will be shown on the last line of the screen, where we type the particular string to look for.

These two commands differ only in the direction where the search takes place:

  • The / command searches forwards (downwards) in the file.
  • The ? command searches backwards (upwards) in the file.

The n and N commands repeat the previous search command in the same or the opposite direction, respectively.

These characters must be preceded by a backslash (\) to be included as part of the search expression:

Character Description
^ Searches at the beginning of the line (Use at the beginning of a search expression).
. Matches a single character.
* Matches zero or more of the previous character.
$ End of the line (Use at the end of the search expression).
[ Starts a set of matching or non-matching expressions.
< This is put in an expression escaped with the backslash to find the ending or the beginning of a word.
> This helps see the ‘<’ character description above.

The character search searches within one line to find a character entered after the command.

  • The f and F commands search for a character on the current line only.
    f searches forwards and F searches backwards and the cursor moves to the position of the found character.
  • The t and T commands search for a character on the current line only
    for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character.


Set Commands

We can change the look and feel of your vi screen using the following :set commands. Once you are in the command mode, type :set followed by any of the following commands:

Command Description
:set ic Ignores the case when searching
:set ai Sets autoindent
:set noai Unsets autoindent
:set nu Displays lines with line numbers on the left side
:set sw Sets the width of a software tabstop. For example, you would set a shift width of 4 with this command — :set sw = 4
:set ws If wrapscan is set, and the word is not found at the bottom of the file, it will try searching for it at the beginning
:set wm If this option has a value greater than zero, the editor will automatically “word wrap”. For example, to set the wrap margin to two characters, you would type this: :set wm = 2
:set ro Changes file type to “read only”
:set term Prints terminal type
:set bf Discards control characters from input


Running Commands

The vi can run commands from within the editor. To run a command, we only need to go to the command mode and type :! command.

For example, if we want to check whether a file exists before we try to save our file with that filename, we can type :! ls and we will see the output of ls on the screen.

we can press any key (or the command’s escape sequence) to return to our vi session.


Replacing Text

The substitution command :s/ enables us to quickly replace words or groups of words within files.

Following is the syntax to replace text:

:s/search/replace/g

The g stands for globally. The result of this command is that all occurrences on the cursor’s line are changed.




Back to Top

See other articles in Category Unix-Linux

Leave a comment