Brief introduction
There are many kinds of text editors under Linux, VI is the most commonly used, and it is also standard for each version of Linux. Note thatVI is just a text editor that can color characters and can be auto-complete, but unlike word in Windows, there is a typography feature.
VI is a decade to grind a sword of the product, although the command is numerous, and most of the functions are based on keyboard input to complete, but once you are familiar with, you will find that the function and efficiency of the VI is not comparable to other graphical interface editor.
Vim is the abbreviation for Vi improved and is an improved version of VI. In Linux, vi is considered to be the de facto standard editor because:
- All versions of Linux have a VI editor;
- Occupy less resources;
- VI is more user-friendly than other editors such as Ed and ex.
You can use the VI editor to edit an existing file, create a new file, and open a text file in read-only mode .
Go to VI Editor
You can enter the VI editor in the following ways:
Command |
Description |
VI filename |
Open if filename exists, otherwise a new file is created and then opened. |
Vi-r filename |
Open an existing file in read-only mode (view cannot be edited only). |
View filename |
Opens an existing file in read-only mode. |
For example, use the VI Editor to create a new file and open it:
$vi testfile|~~~~~~~~~~~~"testfile" [New File]
Vertical bar (|) Represents the position of the cursor, and the tilde (~) indicates that the line does not have any content. If you don't have a ~, and you don't see anything, it means that the line must have a blank character (space, tab indent, line feed, and so on) or invisible characters .
Working mode
To learn more about VI before the first to understand the VI work mode, VI has three modes of operation:
1) Normal mode
When the shell enters the VI editor, it first enters normal mode. In normal mode, entering any character from the keyboard is interpreted as a command. There is no prompt in normal mode, executes immediately after the command is entered, does not require a carriage return, and the characters entered are not displayed on the screen.
In normal mode, you can execute commands, save files, move cursors, paste copies , and so on.
2) Edit mode
Edit mode is used primarily for text editing. Any characters entered by the user in this mode are saved as the contents of the file and displayed on the screen.
3) Command mode
Command mode, the user can perform some advanced processing of the file. Although commands in normal mode can do many things, it is necessary to perform some operations such as string lookups, substitutions, display line numbers, or command mode.
Note: There are several tutorials that say there are two modes of operation, which are merging command modes into normal mode.
Operating mode switching:
- Enter edit mode when I (insert), C (modify), O (another line) command is entered in normal mode, and press ESC to return to normal mode.
- Enter a colon in normal mode (:) can enter command mode. After entering the command press ENTER, the command will automatically return to normal mode after execution.
Tip: If you are not sure which mode you are currently in, pressing ESC two times will return to normal mode.
Exit VI Editor
Typically exits the VI Editor in command mode.
Exit Command |
Description |
Q |
If the file is not modified, it is returned directly to the shell, otherwise prompts to save the file. |
Q! |
Forcibly exits without saving the modified content. |
Wq |
The W command saves the file, and the Q command exits VI, which, together, saves and exits. |
Zz |
Save and exit, equivalent to WQ, but more convenient. |
Before exiting, you can also specify a filename after the W command to save the file as a new file, for example:
W filename2
Save the current file as filename2.
Note: When you edit a file, the user's actions are based on the copy in the buffer. If you do not save to disk when you exit, the contents of the buffer will be lost.
Move cursor
To not affect the contents of the file, you must move the cursor under normal mode (press two ESC). Use the commands in the following table to move one character at a time:
Command |
Description |
K |
Move cursor up (move one line) |
J |
Move cursor down (move one line) |
H |
Move the cursor to the left (move one character) |
L |
Move the cursor to the right (move one character) |
Two-point reminder:
- VI is case-sensitive, and be careful not to lock uppercase when entering commands.
- You can add a number to the front of the command as a prefix, for example, 2j moves the cursor down two lines.
Of course, there are many other commands to move the cursor, but remember, be sure to do it in normal mode (press two ESC).
commands to move the cursor
Command |
Description |
0 or | |
Position the cursor at the beginning of a line. |
$ |
Position the cursor at the end of a line. |
W |
Navigates to the next word. |
B |
Navigates to the previous word. |
( |
Position to the beginning of a sentence, the sentence is to!. ? Three kinds of symbols to define. |
) |
Navigate to the end of a sentence. |
{ |
Move to the beginning of the paragraph. &&&&&& |
} |
Move to the end of the paragraph. &&&&&&&&& |
[[ |
Return to the beginning of the paragraph. &&&&&&&&&& |
]] |
Move forward to the beginning of the next paragraph. &&&&&&&&&& |
n| |
Move to nth column (current row). |
1G |
Moves to the first line of the file. |
G |
Moves to the last line of the file. |
NG |
Moves to the nth row of the file. |
: N |
Moves to the nth row of the file. |
H |
Move to the top of the screen. |
Nh |
Move to the position of the nth row at the top of the screen. |
M |
Move to the middle of the screen. |
L |
Move to the bottom of the screen. |
NL |
Move to the position of line N on the bottom of the screen. |
: X |
X is a number that represents the line that moves to the line number x. |
Control commands
Some control commands can be combined with the Ctrl key, as follows:
Command |
Description |
Ctrl+d |
Scroll forward half screen |
Ctrl+f |
Scroll forward Full screen |
Ctrl+u |
Scroll Backward Half screen |
Ctrl+b |
Scroll backward through the entire screen |
Ctrl+e |
Scroll up one line |
Ctrl+y |
Scroll down one line |
Ctrl+i |
Refresh Screen |
Edit File
You can edit the file by switching to edit mode. There are a number of commands that can be switched from normal to edit mode, as shown below:
Command |
Description |
I |
Inserts text before the current cursor position |
I |
Inserts text at the beginning of the current line |
A |
Inserts text after the current cursor position |
A |
Inserts text at the end of the current line |
O |
Create a row below the current location |
O |
Create a row above the current position |
Delete character
The following command allows you to delete a character or line from a file:
Command |
Description |
X |
Delete the character under the current cursor |
X |
Delete the character in front of the cursor |
Dw |
Delete the character from the current cursor to the end of the word |
d^ |
Delete the character from the current cursor to the beginning of the line |
d$ |
Delete the character from the current cursor to the end of the line |
D |
Delete the character from the current cursor to the end of the line |
Dd |
Delete the line where the current cursor is located |
You can add a number prefix to the command to indicate the number of repetitions, for example, 2x to delete the characters under the cursor twice, and 2DD to delete the row where the cursor was deleted twice in succession.
You are advised to practice the above commands and further study further.
Modify text
If you want to modify a character, word, or line, you can use the following command:
Command |
Description |
Cc |
Deletes the current row and enters edit mode. |
cw |
Deletes the current word (word) and enters edit mode. |
R |
Replaces the character under the current cursor. |
R |
Replace the character from the current cursor and press the ESC key to exit. |
S |
Replaces the current character with the input character and enters edit mode. |
S |
Replaces the current line with the input text and enters edit mode. |
Paste Copy
Copy-paste command in VI:
Command |
Description |
Yy |
Copy when moving forward |
Nyy |
Copy n rows |
yw |
Copy a word (word) |
Nyw |
Copy n rows |
P |
Paste the copied text behind the cursor |
P |
Paste copied text to the front of the cursor |
Advanced Commands
Some of the following commands may seem odd, but will make your job more efficient, and if you're a VI heavy user, get to know it.
Command |
Description |
J |
Connect the current row and the next row as a row |
<< |
Moves the current line one unit to the left (one indent width) |
>> |
Move the current line one unit to the right (one indent width) |
~ |
Change the case of the current character |
^g |
Ctrl+g key combination to display the current file name and status |
U |
Undo changes made to the current row |
U |
Undo the last operation and press ' U ' again to restore the operation |
: F |
Displays the position of the current cursor in the file, the file name, and the total number of rows in the file as percent sign (%) |
: F filename |
Rename a file to filename |
: w filename |
Save changes to filename |
: E filename |
Open another file named filename |
: CD dirname |
Change the current working directory to DirName |
E |
Switch between two open files |
: N |
If you open more than one file with VI, you can use: N to switch to the next file |
:p |
If you have multiple files open with VI, you can use: N to switch to the previous file |
: N |
If you have multiple files open with VI, you can use: N to switch to the previous file |
: R file |
Reads the file and inserts it behind the current line |
: Nr File |
Read the file and insert it behind the nth line |
Text Lookup
If you want to do a full file search, you can enter/command in normal mode (press two ESC), then the status bar (the last line) appears "/" and prompts for the string to find, enter.
/command is to look down, and if you want to look up, can I use? Command.
At this point, the input n command can continue looking in the same direction, and the N command can continue in the opposite direction.
The searched string can contain characters with special meanings, and if you want to search for the characters themselves, you need to precede the backslash (\).
Partial list of special characters
character |
Description |
^ |
Match the beginning of a line |
. |
Match one character |
* |
Match 0 or more characters |
$ |
Matches the end of a line |
[ ] |
Match a set of characters |
If you want to search for a single character in a row, you can use the F or F command, F to search up, F to search down, and position the cursor to match the character.
You can also use the T or T command: the t command to search up and position the cursor in front of the matching character; the T command searches down and positions the cursor behind the matching character.
Set command
The SET command provides some settings for the VI editor. Use the SET command to enter command mode.
: Set command options
Command |
Description |
: Set IC |
Ignores case when searching. |
: Set AI |
Set auto indent (Auto-align). |
: Set Noai |
Cancels automatic indentation (auto-alignment). |
: Set Nu |
Displays the line number. |
: Set SW |
Sets the number of spaces to indent, for example, to set the number of indent spaces to 4::set sw=4. |
: Set WS |
Loop Search: If the specified character is not found until the end of the file, it will go back to the beginning to continue looking. |
: Set WM |
Set word wrapping, for example, to set a line break with 2 characters from the margin:: Set wm=2. |
: Set RO |
Change the file type to read-only. |
: Set term |
The output terminal type. |
: Set BF |
Ignores the input control characters, such as BEL (Bell), BS (BACKSPACE), CR (carriage return), and so on. |
Run command
Switch to command mode, and then enter! command to run the Linux command.
For example, before you save a file, if you want to see if the file exists, enter
:! Ls
You can list the files in the current directory.
Press any key to return to the VI editor.
Text substitution
Switch to command mode, and then enter the s/command to replace the text. The syntax is:
: s/search/replace/g
Search is the retrieved text, replace is the text to be replaced, and G represents the global substitution.
Some tips
The use of VI editor is explained, but please keep the following points in mind:
- Enter a colon (:) Enter command mode and press ESC two times to enter normal mode.
- The meaning of the case of the command is not the same.
- You must be in edit mode to enter content.
Thanks: Thank you for your patience and reading!
Linux VI Editor Operating manual