Use the Vi/Vim Editor: Basics
The VI editor is a command line-based, powerful text editor. It was first developed for Unix systems and subsequently transplanted to many Unix and Linux distributions.
On Linux, there is another advanced version of VI Editor-VIM (also known as VI IMproved ). VIM only adds more features to the strong functions of VI. These features include:
- Supports more Linux releases,
- Supports code block folding and syntax highlighting in multiple programming languages, including python, c ++, and perl,
- Supports editing files through multiple network protocols, including http and ssh,
- Supports editing and compressing files in an archive,
- Multiple files can be edited simultaneously on the split screen.
Next we will discuss the VI/VIM commands and options. For the purpose of teaching, we use VI for example, but all commands can be used in VIM. First, we will introduce the two modes of the VI editor.
Command mode
In command mode, we can execute tasks such as saving files, running commands in VI, copying, cutting, and pasting, and searching and replacing. When we are in insert mode, we can pressEscape
(Esc
) Key return command mode
Insert mode
In insert mode, you can type the file content. In command mode, pressi
Enter the insert mode.
Create a file
You can run the following command to create a file ):
$ vi filename
Once the file is created or opened, we first enter the command mode. We need to enter the input mode to enter the content in the file. We have learned about these two models through the previous article.
Exit Vi
To exit from the insert mode, pressEsc
Enter the command mode. Next, we can use two commands to exit Vi based on different needs.
- Do not save and exit-input in command mode
:q!
- Save and exit-input in command mode
:wq
Move cursor
Next we will discuss the commands and options for moving the cursor in command mode:
k
Move the cursor up a row
j
Move the cursor down one line
h
Move the cursor to the left of a letter
l
Move the cursor to the right of a letterNote: You can use4k
Or5l
The two commands move up four lines or five letters to the right.
0
Move the cursor to the beginning of the row
$
Move the cursor to the end of the row
nG
Move the cursor to line n
G
Move the cursor to the last line of the file
{
Move the cursor to the previous section
}
Move the cursor to the next segment
There are also some commands that can be used to control the movement of the cursor, but the commands listed above should be able to cope with daily work needs.
Edit text
This section lists some commands used in the command mode. You can enter the insert mode to edit the current file.
i
Insert content before the current cursor position
I
Insert content at the beginning of the row where the cursor is located
a
Insert content after the current cursor position
A
Insert content at the end of the row where the cursor is located
o
Add a row after the row where the current cursor is located
O
Add a row before the row where the current cursor is located
Delete text
The following commands can only be used in command mode.Esc
Enter command mode. If you are in insert mode:
dd
Delete the content of the entire line where the cursor is located.dd
Add a number before, for example2dd
You can delete two rows starting with the row where the cursor is located.
d$
Delete from cursor position till end of line
d^
Delete from the cursor position until the beginning of the line
dw
Delete all content starting from the cursor position until the next word
Copy and paste
yy
Copy the current row.yy
Multiple rows can be copied by adding numbers before
p
Paste the copied row after the cursor
P
Paste the copy row before the cursor
These are some basic commands that can be used in the VI/VIM editor. More advanced commands will be taught in future tutorials. If you have any questions or suggestions, please leave a message in the comment area below.
Vim Basics
Basic Vim tutorials
Make Vim an excellent C ++ IDE
Upgrade Ubuntu 14.04 to Vim7.4 to 8.0
Install the youcompleteme auto-completion plug-in Vim
Linux Vim Editor
Vim Text Editor
Install and configure the advanced version of Vim
Vim editor usage tutorial
Ubuntu 16.04 Vim YouCompleteMe Automatic Installation configuration and use
Linux text editor Vim basic tutorial