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, pressiEnter 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, pressEscEnter 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:
kMove the cursor up a row
jMove the cursor down one line
hMove the cursor to the left of a letter
lMove the cursor to the right of a letterNote: You can use4kOr5lThe two commands move up four lines or five letters to the right.
0Move the cursor to the beginning of the row
$Move the cursor to the end of the row
nGMove the cursor to line n
GMove 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.
iInsert content before the current cursor position
IInsert content at the beginning of the row where the cursor is located
aInsert content after the current cursor position
AInsert content at the end of the row where the cursor is located
oAdd a row after the row where the current cursor is located
OAdd a row before the row where the current cursor is located
Delete text
The following commands can only be used in command mode.EscEnter command mode. If you are in insert mode:
ddDelete the content of the entire line where the cursor is located.ddAdd a number before, for example2ddYou 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
dwDelete all content starting from the cursor position until the next word
Copy and paste
yyCopy the current row.yyMultiple rows can be copied by adding numbers before
pPaste the copied row after the cursor
PPaste 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