Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux vi/vim

Linux vi/vim

All Unix-like systems come with the vi text editor built in, while other text editors may not necessarily be present.

But currently we use the vim editor more often.

vim has the ability to edit programs and can actively distinguish the correctness of syntax through font colors, making programming convenient.

Related articles:The Most Complete Vim Shortcut Key Map - From Basics to Advanced


What is vim?

Vim is a text editor developed from vi. It is especially rich in programming-friendly features such as code completion, compilation, and error jumping, and is widely used among programmers.

Simply put, vi is an old-style word processor, though already quite full-featured, but there is still room for improvement. vim, on the other hand, can be said to be a very useful tool for program developers.

Even vim's official website (https://www.vim.org/) itself says that vim is a program development tool rather than a word processing software.

vim Keyboard Map

VI / VIM Keyboard Quick Reference

Click a key to learn details

Cursor Movement

Editing Operations

Enter Insert Mode

Visual Mode

Search / Jump

Other Commands

Click any key to view detailed descriptions and example usage


Using vi/vim

Basically, vi/vim has three modes:Command Mode, Insert Mode, and Command-Line Mode.

Command Mode

When a user first starts vi/vim, they enter Command Mode.

In this state, keystrokes are recognized by Vim as commands rather than input characters. For example, pressing i at this point does not input a character; i is treated as a command.

The following are some commonly used commands in Normal Mode:

To edit text, simply start Vim, enter Command Mode, and press i to switch to Insert Mode.

Command Mode only has the most basic commands, so you still need to rely onCommand-Line Modeto enter more commands.

Insert Mode

Pressing i in Command Mode enters Insert Mode, and pressing Esc returns to Normal Mode.

In Insert Mode, the following keys can be used:

Command-Line Mode

Pressing : (the English colon) in Command Mode enters Command-Line Mode.

In Command-Line Mode, you can enter single- or multi-character commands, and there are many available commands.

In Command-Line Mode, the basic commands are (the colon has been omitted):

Press ESC at any time to exit Command-Line Mode.

Simply put, we can think of these three modes as shown in the diagram below:


vi/vim Usage Examples

Using vi/vim to Enter Normal Mode

If you want to use vi to create a file named runoob.txt, you can do this:



$ vim runoob.txt

Simply entering ** vi filename ** puts you into vi's Normal Mode. Note that you must always add a filename after vi, whether or not the file exists!

Press i to enter Insert Mode (also called Edit Mode) and start editing text

In Normal Mode, pressing i, o, a, and so on enters Insert Mode!

In Edit Mode, you will see -INSERT- in the status bar at the lower left, which indicates that you can enter any characters.

At this point, apart from the Esc key on the keyboard, all other keys can be treated as normal input keys, so you can perform any editing.

Press ESC to return to Normal Mode

Okay, suppose I have finished editing as shown above. How should I exit? Yes! That's right! Just press Esc this key! Immediately you will see - INSERT - disappear from the lower left of the screen!

In Normal Mode, press :wq to save and exit vi

OK, we want to save the file. The command to save and exit is simple: enter :wq to save and exit!

OK! We have now successfully created a file named runoob.txt.


vi/vim Key Descriptions

Besides the simple examples of i, Esc, and :wq above, vim actually has many more keys you can use.

Part 1: Cursor Movement, Copy/Paste, Search/Replace, etc. Available in Normal Mode

Methods for Moving the Cursor
h or the Left Arrow key
j or the Down Arrow key
k or the Up Arrow key
l or the Right Arrow key
If you place your right hand on the keyboard, you will find that hjkl are arranged together, so you can use these four keys to move the cursor. To move multiple times, for example 30 lines down, use the key combination "30j" or "30 Down Arrow"; that is, add the desired count (number) before pressing the action key!
[Ctrl] + [f]
[Ctrl] + [b]
[Ctrl] + [d]
[Ctrl] + [u]
+
-
n
0 or the function key [Home]
$ or the function key [End]
H
M
L
G
nG
gg
n
Search and Replace
/word
?word
n
N
Using /word together with n and N is very helpful! It lets you repeatedly find the keywords you are searching for!
:n1,n2s/word1/word2/g
":100,200s/vbird/VBIRD/g". (Commonly used)
:1,$s/word1/word2/g or :%s/word1/word2/g
:1,$s/word1/word2/gc or :%s/word1/word2/gc
Deleting, Copying, and Pasting
x, X
nx
dd
ndd
d1G
dG
d$
d0
yy
nyy
y1G
yG
y0
y$
p, P
J
c
u
[Ctrl]+r
The u and [Ctrl]+r commands are very commonly used! One undoes, the other redoes. With these two function keys, your editing becomes, hehe, very enjoyable!
.

Part 2: Descriptions of Keys Available for Switching from Normal Mode to Edit Mode

Entering Insert or Replace Edit Mode
i, I
i means "input at the current cursor position", and I means "start inputting at the first non-blank character of the current line". (Commonly used)
a, A
a means "start inputting at the character after the cursor", and A means "start inputting at the last character of the cursor's line". (Commonly used)
o, O
These are the uppercase and lowercase forms of the letter o. o inputs a new line below the current cursor line; O inputs a new line above the current cursor line! (Commonly used)
r, R
r replaces only the single character at the cursor once; R keeps replacing text at the cursor until ESC is pressed; (Commonly used)
With the keys above, the words --INSERT-- or --REPLACE-- appear in the lower left corner of the vi screen. You can tell the action from the name! Note in particular, as we mentioned above, when you want to input characters in the file, you must see INSERT or REPLACE in the lower left corner before you can type!
[Esc]

Part 3: Descriptions of Keys Available for Switching from Normal Mode to Command-Line Mode

Command-Line Commands for Saving, Exiting, etc.
:w
:w!
:q
:q!
Note that the exclamation mark (!) in vi often means "force".
:wq
ZZ
ZQ
:w [filename]
:r [filename]
:n1,n2 w [filename]
:! command
":! ls /home" lets you view, within vi, the file information output by ls under /home!
Changing the vim Environment
:set nu
:set nonu

Note especially that in vi/vim, numbers are very meaningful! A number usually means repeating an action several times! It can also mean going to the nth something.

For example, to delete 50 lines you use "50dd", right! The number is placed before the action. If I want to move 20 lines down? Just use "20j".