Fri. Aug 14th, 2020

How to use the vi editor in Linux

6 min read

The vi editor is a standard editor for all Unix and Linux systems. Its power is not inferior to any latest text editor. Here is a brief introduction to its usage and a small number of instructions. The vi editor is exactly the same for any version of Unix and Linux systems. Vi is also the most basic text editor in Linux. After learning it, you will be able to travel to the world of Linux.

The basic concepts of vi

Basically, vi can be divided into three states: command mode, insert mode, and last line mode. The functions of each mode are as follows:

  1. Command mode: Control the screen cursor movement, delete characters, words or lines, move and copy a certain section and enter Insert mode, or go to last line mode.
  2. Insert mode: Only in Insert mode, you can do text input. Press “ESC” to return to the command line mode.
  3. Last line mode: Save the file or exit vi. You can also set the editing environment, such as looking for strings, listing line numbers, etc.

However, we generally simplify vi into two modes when using it, that is, the last line mode is also included in the command mode.

The basic operation of vi

a) Run vi

Enter vi and the file name after the system prompts to enter the full-screen editing screen of vi:

$ vi myfile

However, it is important to note that after you enter vi, you are in “command mode” and you must switch to “insert mode” to enter text. Anyone who uses vi for the first time will want to use the up, down, left, and right keys to move the cursor first. As a result, the computer keeps beeping and suffocates himself, so after entering vi, do not disturb it first, switch to “insert mode”!

b) Switch to Insert mode to edit the file

In the “command mode”, click the letter “i” to enter “insert mode”, at which point you can start typing.

c) Insert switch

You are currently in “insert mode”, you can only enter text all the time if you find that you have made a typo! If you want to use the cursor keys to move back and delete the word, you must first press the “ESC” key to go to “command mode” and then delete the text.

d) Exit vi and save the file

In “command mode”, click the “:” colon key to enter “Last line mode”, for example:

:w filename (enter “w filename” to save the article with the specified filename)

:wq (enter “wq”, save and exit vi)

:q! (enter q ! force exit vi without saving)

Command mode function keys

1). Insert mode
Press “i” to switch to insert mode “insert mode”, press “i” to enter insert mode and start to input files from the current cursor position;

After entering the insert mode by pressing “a”, the text is entered from the position next to the current cursor position;

After pressing “o” to enter the insert mode, a new line is inserted, and the text is entered from the beginning of the line.

2). Switch from insert mode to command-line mode

Press the “ESC” key.

3). Move the cursor

Vi can directly use the cursor on the keyboard to move up, down, left and right, but regular vi uses lowercase English letters “h”, “j”, “k”, “l” to control the cursor left, down, up, and right, grid.

Press “ctrl” + “b”: the screen moves one page back.

Press “ctrl” + “f”: the screen moves forward one page.

Press “ctrl” + “u”: the screen moves half a page to the “back”.

Press “ctrl” + “d”: the screen moves half a page “forward”.

Press the number “0”: move to the beginning of the article.

Press “G”: Move to the end of the article.

Press “$”: Move to the “end of the line” where the cursor is located.

Press “^”: move to “beginning of line” where the cursor is

Press “w”: the cursor jumps to the beginning of the next word

Press “e”: the cursor jumps to the end of the next word

Press “b”: the cursor returns to the beginning of the previous word

Press “#l”: the cursor moves to the #th position of the line, such as 5l, 56l.

4). Delete text

“x”: Each time you press, the “behind” character at the cursor position is deleted.

“#x”: For example, “6x” means to delete 6 characters “behind” at the cursor position.

“x”: X in upper case. Each time you press it, the character “front” of the cursor position is deleted.

“#x”: For example, “20X” means delete the “front” 20 characters at the cursor position.

“dd”: Delete the line where the cursor is located.

“#dd”: delete line # starting with the cursor

5). Copy

“yw”: Copy the characters from the cursor position to the end of the word to the buffer.

“#yw”: Copy # words to the buffer

“yy”: Copy the line under the cursor to the buffer.

“#yy”: For example, “6yy” means to copy 6 lines of text “count down” from the line where the cursor is located.

“p”: Paste the characters in the buffer to the cursor position. Note: All copy commands related to “y” must cooperate with “p” to complete the copy and paste function.

6). Replace

“r”: Replaces the character at the cursor.

“R”: Replace the character where the cursor is until you press the “ESC” key.

7). Reply to the last operation

“u”: If you execute a command by mistake, you can press “u” immediately to return to the previous operation. Press “u” multiple times to perform multiple replies.

8). Change

“cw”: change the word at the cursor to the end

“c#w”: For example, “c3w” means change 3 words

9). Skip to the specified line

“ctrl” + “g” lists the line number of the line where the cursor is located.

“#g”: For example, “15G” means to move the cursor to the beginning of the 15th line of the article.

4.Introduction to commands in Last line mode

Before using “last line mode”, please remember to press “ESC” key to make sure you are in “command mode”, then press “:” colon to enter “last line mode”.

A) List line numbers

“set nu”: After entering “set nu”, the line number is listed before each line in the file.

B) Jump to a line in the file

“#”: “#” Signifies a number. Enter a number after the colon and press the Enter key to jump to that line. If you enter the number 15 and press Enter, you will jump to line 15 of the article.

C) Find characters

“/Keyword”: Press the “/” key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can always press “n” to find the key you want. So far.

“? Keyword”: first press the “?” Key, and then enter the character you want to find. If the keyword you are looking for is not what you want, you can always press “n” to find the key you want. So far.

D) Save the file

“w”: Enter the letter “w” in the colon to save the file.

E) leave vi

“q”: Press “q” to exit. If you cannot leave vi, you can forcibly leave vi after “q”.

“qw”: It is generally recommended to use with “w” when leaving so that you can save the file when you exit.

5, vi command list

1. The following table lists the functions of some keys in command mode:

h
Move cursor left one character

l
Move the cursor one character to the right

k
cursor up one line

j
cursor down one line

^
Cursor to the beginning of the line

0
number “0”, the cursor moves to the beginning of the article

G
cursor to the end of the article

$
Cursor to the end of the line

Ctrl + f
forward Fanning

Ctrl + b
backward scroll

Ctrl + d
half-screen forward

Ctrl + u
Half screen backward

i
insert character before cursor position

a
starts to increase after the cursor

o
Insert a new line, starting from the beginning of the line

ESC returns
from input state to command state

x
delete the character after the cursor

#x
delete the # characters after the cursor

X
(uppercase X), delete character before cursor

#X
delete the # characters in front of the cursor

dd
delete the line where the cursor is

#dd
delete the # line from the line where the cursor is

yw
copy a word at the cursor position

#yw
Copy the # characters at the cursor position

yy
copy the line where the cursor is

#yy
Copy # lines from the line where the cursor is

p
paste

u
Cancel operation

cw
change a word at the cursor position

#cwChange
the # characters at the cursor position

2. The following table lists some instructions in the command mode.
w filename
stores the file being edited as a filename.

wq filename
stores the file being edited as a filename and exits vi

q!
Discard all changes and exit vi

set nu
display line number