Vi Editor in Linux with Commands

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this document, you will learn about what a Vi editor is and why we use it. Understand the 3 modes of Vi editor and the significance of each. Learn the commands which are used in the 3 modes of Vi editor and how to use them.

What is Vi Editor?

Broadly speaking there are 2 ways to edit, read or create a file in Linux. They are: using the GUI (Graphic User Interface) or the terminal. For those people who prefer using the terminal, one of the best ways to edit, read or create a file is the Vi editor. Vi editor is the abbreviation for Visual Instrument.

Need of Vi Editor in Linux

It is preferred by many Linux users as it is available on all of the Linux systems by default, it is also user-friendly and feature-rich which makes the possibilities of editing, reading, or even creating a file from scratch using Vi editor endless.

Modes of Vi editor

There are 3 modes in the Vi editor

1. Command mode

2. Insert mode

3. Escape mode

Let us look at what each of these modes are for:

1. Command mode:

The command mode is active as soon as Vi starts up, which means that Vi itself opens in Command mode. In command mode, we can only edit the file, which means that we can only delete, paste and copy the text in the file. We cannot write in command mode.

2. Insert mode:

The insert mode is where we can write text into the file. Insert mode is activated upon typing ‘i’ on the command mode.

3. Escape mode:

This mode takes the cursor to the very last line of the screen and the editor will wait for a command. The escape mode is activated when you type the colon (‘:’). This mode is used to save or execute the file.

How to Start Vi editor?

Vi editor is started from the terminal in Linux. Just like pretty much everything has a command in Linux, so does Vi. These are the 3 ways you can start Vi editor.

1. Vi <filename>

The above command will open an already existing file, if and only if the name matches with what you have entered, else Vi will open a new file with the name you have entered.

2. Vi <file address>

The above command will open an already existing file in the directory you have specified if and only if the name matches what you have entered, else Vi will open a new file in the directory you have specified with the name you have entered.

3. Vi -R <file address>

The above command will open an already existing file in read-only mode, which means you will have no way to edit the file. You must note that this command will only open an existing file and will not create a new file.

Commands used in Vi Editor

There are 3 types of commands: Command mode commands, insert mode commands and escape mode commands. Let us see the different commands in detail:

1. Command mode commands:

Once you have started the Vi editor it opens up in command mode, where other than deleting, pasting and copying text, you can also navigate the cursor using a few commands. Before we look into the commands which helps us in deleting, pasting and copying text into the file, let us look into a few commands that will help us navigate through the file:

Note: Vi editor is case sensitive so please pay attention to the case of the command.

a. Cursor navigating commands:

i. k

This command moves the cursor up by one line. (you can also achieve the same by pressing the up arrow on the keyboard)

ii. j

This command moves the cursor down by one line. (you can also achieve the same by pressing the down arrow on the keyboard)

iii. h

This command moves the cursor left by one character. (you can also achieve the same by pressing the left arrow on the keyboard)

iv. l (lower case L)

This command moves the cursor right by one character. (you can also achieve the same by pressing the right arrow on the keyboard)

v. I (higher Case i)

This command moves the cursor to the beginning of the line you are currently in.

vi. $

This command moves the cursor to the end of the line you are currently in.

vii. H

This command moves the cursor to the very beginning of the screen.

viii. <n>H

This command moves the cursor to the nth line from the beginning of the screen. For example: The command “6H” will move the cursor to the 6th line from the beginning.

ix. L

This command moves the cursor to the bottom of the screen.

x. nL

This command moves the cursor to the nth line from the bottom of the screen. For example: The command “3H” will move the cursor to the 3rd line from the bottom.

xi. W

This command moves the cursor to the start of the next word.

xii. B

This command moves the cursor to the start of the previous word.

xiii. θ

This command brings the cursor to the very start of the current line. You can also write this command as : ^ .

b. Screen navigation commands

i. ^f

This command moves down one screen.

ii. ^b

This command moves up one screen.

iii. ^d

This command moves up by half a screen.

iv. ^u

This command moves down by half a screen.

v. ^l

This command redraws the entire screen.

vi. ^r

This command redraws the entire screen and removes the deleted lines.

Now that you know how to navigate the cursor and screen, let us see how to delete, paste and copy text in the file:

c. Deleting commands:

i. X

This command deletes the character which is present before the cursor. (It works exactly like the “backspace” key on the keyboard)

ii. x

This command deletes the character which is present under the cursor. (It works similar to the “delete” key on the keyboard)

iii. dd

This command deletes the entire line the cursor is present in.

iv. <n>dd

This command will delete n lines.

v. d^

This command deletes the characters from the beginning of the line to the position of the cursor. (in the same line)

vi. d$

This command deletes the characters from the position of the cursor to the end of the line.

vii. dθ

This command deletes the characters from the position of the cursor to the start of the line.

viii. D

This command will delete contents of the line after the cursor.

ix. dw

This command will delete a word.

x. <n>dw

This command will delete n word.

xi. dG

This command will delete content from the current line to the end of the file.

d. Copy and paste commands:

i. yy

This command will copy the entire line the cursor is currently present in.

ii. <n>yy

This command will copy the entire line the cursor is currently present in along with n lines after the line the cursor is in. For example, the command “3yy” will copy the line the cursor is on and also 3 lines after the line the cursor is in.

iii. p

This command will paste the copied text after the cursor.

iv. P

This command will paste the copied text before the cursor.

Set commands

In command mode, there is another type of command called the set command. Using set commands you can change the look of the Vi editor screen. Let us look at how to set various parameters using the set commands:

i. :set ic

This command ignores the case while searching for a specific word or character.

ii. :set ai

This command sets auto indent.

iii. :set noai

This command unsets auto indent.

iv. :set nu

This command displays the line numbers of the left side of each line.

v. :set sw

This command sets the width of a software tab space. For example if you used the command: ’:set sw = 4’ the tab space will have 4 spaces width.

vi. :set ws

This command searches a word at the top if wrapscan is set and the word is not found at the bottom.

vii. :set ro

This command changes the file type to “read only”.

viii. :set bf

This command discards control characters from input.

ix. :set term

This command prints the terminal type

x. :set wm

This command automatically word wraps, if it has a greater value than 0.

Miscellaneous commands:

i. u

This command will undo the command which has been previously used.

ii. .

This command will repeat the command which has been last used.

iii. J

This command will join 2 lines

iv. <n>G

This command will take the cursor to the nth line number. The ‘G’ command without any number prefixing it, will take the cursor to the last line of the file.

v. U

This command will undo changes to the entire line.

vi. G

This command directs you to the last line of the file.

vii. “

This command directs you to your previous position in the file.

viii. xp

This command switches 2 characters.

ix. yyp

This command repeats the current line.

x. ddp

This command swaps 2 lines.

xi. :=

This command prints the total number of lines in the file at the bottom of the file.

xii. :.=

This command prints the line number of the line you are currently in.

These are the commands which are used in the command mode of the Vi editor.

xiii. “add

This command deletes the current line and puts text in buffer a.

xvi. “ap

This command pastes the line from buffer a.

Now let us look into the commands of insert mode of the Vi editor.

2. Insert mode Commands in Vi

First of all let us see how to toggle between the 3 modes of Vi Editor:

  • Command mode to insert mode: “i”
  • Insert mode to command mode: “Esc”
  • Command mode to escape mode: “:”
  • Insert mode to command Escape mode: “Esc” and then“:”

Now let us see a few commands with respect to Insert mode:

Note: ‘i’ is not the only command to enter insert mode from command mode, you can use any of the following too:

i. i

This command will insert text before the current position of the cursor.

ii. I

This command will insert text at the beginning of the line in which the cursor is in.

iii. a

This command will insert text after the current position of the cursor.

iv. A

This command will insert text at the end of the line in which the cursor is in.

v. o

This command will make the cursor go to a new line so that text can be entered in the new line.

vi. O

This command will make a new line above the line the cursor was in so that text can be entered in the new line.

vii. R

This command will replace the text that is there from the cursor’s current position to the right of the line.

viii. S

This command will replace the entire line the cursor was in, so that text can be inserted into the line freshly.

ix. cw

This command will change word with the new text

x. c<N>w

This command will change ‘N’ characters, starting with the character under the cursor.

xi. C

This command replaces the characters in the current line.

xii. cc

This command replaces the entire current line

xiii. <N>CC

This command replaces the next ‘N’ lines, starting with the current line

The above commands are the commands to not only insert text in the file efficiently, but also enter insert mode from the command mode of the Vi editor.

After writing the text, you may want to save it, that is when the escape mode comes into play. Here are a few commands that are used in the escape mode:

Note: These commands work only in the escape mode if you are there in either command mode or insert mode, switch to escape mode if you want to run the commands below.

3. Escape mode commands in Vi:

i. :q

This command will just quit from the Vi editor.

ii. :q!

This command will quit from the Vi editor without saving the changes or work you have done.

iii. :wq

This command will save the work and exit from the Vi editor.

iv. :w<filename>

This command will write the work to the file name you enter. This command works exactly like save as.

v. ZZ

This command saves and quotes the Vi editor.

vi. :w

This command simply saves the file (does not close)

vii. :w!

This command saves the file in the Vi editor and also writes it to a non-writable file.

viii. replace

This command will let you replace selected text with the new desired text

Syntax:

:s/<old string>/<new string>

E.g:

:s/<Ferrari>/<Lamborghini>

The above command will replace all of the matches it finds with “Ferrari” with the new word “lamborghini”.

ix. ab

This command abbreviates a word using the syntax:

:ab <abbreviation><abbreviated word>

In escape mode, you can also search a specific pattern of string (just like grep but less powerful). Let us see the various commands to search:

Character and word searching in Vi

i. /

This command performs a forward search for the given string

ii. ?

This command performs a backward search for the given string

iii. ^

This command searches at the beginning of the line, it is used as:

/^<word>

iv. .

This command matches a single character.

v. $

This command searches at the end of the line, it is used as:

/<word>$

vi. n

This command goes to the next occurrence of the searched word

vii. /wo[characters]rd

Let us see this command with an example:

The command “pal[abc]ce” will search for the words, palace, palbce and palcce.

viii. /\<word\>

This command is used to force search the word. For example, the command “/\<he\>” will search only for ‘he’ and not for ‘hereby’ or ‘here’.

These are the commands that are applied in the escape mode. There are other commands which help in searching, force save as, etc which help in better productivity.

Summary

The Vi Editor has so many more efficient yet simple commands which help in searching, selecting, sorting, etc.

The Vi Editor is an extremely productive and powerful editor in Linux that can help in the simple process of editing a text file by using the many commands from all the 3 modes, which makes workflow so much easier as it saves a lot of time while editing really large files in a huge project.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *