Linux vi/vim

All Unix-like systems have the vi text editor built in; other text editors may not necessarily exist.

However, currently we use the vim editor more often.

vim has the ability for program editing; it can actively use font colors to distinguish the correctness of syntax, which is convenient for program design.

Related articles:The Most Complete Vim Shortcut Key Map — From Beginner to Advanced


What is vim?

Vim is a text editor developed from vi. It has particularly rich features that facilitate programming, such as code completion, compilation, and error jumping, and is widely used among programmers.

Simply put, vi is an old-fashioned word processor, but its functions are already quite complete; there is still room for improvement. vim 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 word processing software.

vim keyboard map

VI / VIM Keyboard Quick Reference Chart Click a key · Learn more
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 is divided into three modes:Command Mode, Insert Mode, and Command-Line Mode。

Command Mode

As soon as the user starts vi/vim, they enter Command Mode.

In this state, keyboard actions are recognized by Vim as commands rather than character input. For example, if we pressi, it will not input a character;iit is treated as a command.

The following are several commonly used commands in Normal Mode:

  • i-- Switch to Insert Mode and start entering text at the current cursor position.
  • x-- Delete the character at the current cursor.
  • :-- Switch to Command-Line Mode to enter a command on the bottom line.
  • a: Insert a new line below the current line and enter Insert Mode.
  • o-- Insert a new line above the current line and enter Insert Mode.
  • O-- Cut the current line.
  • dd-- Copy the current line.
  • yy(lowercase) -- Paste clipboard content below the cursor.
  • p(uppercase) -- Paste clipboard content above the cursor.
  • P-- Undo the last operation.
  • u-- Redo the last undone operation.
  • Ctrl + r-- Save the file.
  • :w-- Exit the Vim editor.
  • :q-- Forcefully exit the Vim editor without saving changes.
  • :q!If you want to edit text, simply start Vim, enter Command Mode, and press

to switch to Insert Mode.iCommand Mode only has some of the most basic commands, so you still need to rely on

the Command-Line Modeto enter more commands.Insert Mode

Insert Mode

to enter Insert Mode. Use theikey to return to Normal Mode.EscIn Insert Mode, you can use the following keys:

Character keys and Shift combinations

  • , input characters, Enter key, newline
  • ENTER, Backspace key, delete the character before the cursor
  • BACK SPACE, Delete key, delete the character after the cursor
  • DELArrow keys
  • , move the cursor in the text, move cursor to beginning/end of line
  • HOME/END, page up/down
  • Page Up/Page Down, toggle cursor between Insert/Replace mode; the cursor will become a vertical line/underline
  • Insert, exit Insert Mode and switch to Command Mode
  • ESCCommand-Line Mode

Command-Line Mode

(English colon) to enter Command-Line Mode.:In Command-Line Mode, you can enter single-character or multi-character commands; there are many available commands.

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

: Save the file.

  • :w: Exit the Vim editor.
  • :q: Save the file and exit the Vim editor.
  • :wq: Forcefully exit the Vim editor without saving changes.
  • :q!The key can exit Command-Line Mode at any time.

pressESCSimply put, we can think of these three modes as represented by the following diagram:

vi/vim Usage Examples


vi/vim Usage Examples

Use vi/vim to enter Normal Mode

Directly enter

$ vim example.txt

vi filenameand you will enter vi's Normal Mode. Please note that you must always add a filename after vi, regardless of whether the file exists or not!Press i to enter Insert Mode (also called Edit Mode) and start editing text.

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

In Edit Mode, you will find that the text –INSERT- appears in the status bar in the lower-left corner; that is the prompt indicating you can input any characters.

At this point, on the keyboard, except for

this key, all other keys can be treated as ordinary input buttons, so you can do any editing.EscPress the ESC button to return to Normal Mode.

Press the ESC button to return to Normal Mode.

this button! You will immediately see that the – INSERT – in the lower-left corner of the screen is gone!EscIn Normal Mode, press

In Normal Mode, press:wqSave and exit vi

and you can save and exit!:wqOK! In this way, we have successfully created a example.txt file.

vi/vim Key Guide


vi/vim Key Guide

Part 1: Cursor movement, copy and paste, search and replace, etc. available in Normal Mode

Part 1: Cursor movement, copy and paste, search and replace, etc. available in Normal Mode

h or left arrow key (←)
Move the cursor left by one character j or down arrow key (↓)
Move the cursor down by one character k or up arrow key (↑)
Move the cursor up by one character l or right arrow key (→)
Move the cursor right by one character If you place your right hand on the keyboard, you will find that hjkl are arranged together, so you can use these four buttons to move the cursor.
If you want to move multiple times, for example, move down 30 lines, you can use the key combination "30j" or "30↓". That is, after adding the desired number (number), press the action!
[Ctrl] + [f] Move the screen 'down' by one page, equivalent to the [Page Down] key (common)
[Ctrl] + [b] Move the screen 'up' by one page, equivalent to the [Page Up] key (common)
[Ctrl] + [d] Move the screen 'down' by half a page
[Ctrl] + [u] Move the screen 'up' by half a page
+ Move the cursor to the first non-blank character of the next line
- Move the cursor to the first non-blank character of the previous line
n<space> That n represents a "number", such as 20. After pressing the number, press the spacebar and the cursor will move n characters to the right on this line. For example, 20<space> moves the cursor 20 characters forward.
0 or the [Home] function key This is the number "0": move to the very first character of this line (common)
$ or the [End] function key Move to the very last character of this line (common)
H Move the cursor to the first character of the top line on this screen
M Move the cursor to the first character of the middle line on this screen
L Move the cursor to the first character of the bottom line on this screen
G Move to the last line of this file (common)
nG n is a number. Move to line n of this file. For example, 20G moves to line 20 of this file (can be used with :set nu)
gg Move to the first line of this file, equivalent to 1G! (common)
n<Enter> n is a number. Move the cursor down n lines (common)
Search and replace
/word Search downward from the cursor for a string named word. For example, to search for the string vbird in the file, just type /vbird! (common)
?word Search upward from the cursor for a string named word.
n This n is an English key. It means repeating the previous search action. For example, if we just executed /vbird to search downward for the string vbird, then after pressing n, it will continue searching downward for the next string named vbird. If we executed ?vbird, then pressing n will continue searching upward for the string vbird!
N This N is an English key. It is exactly the opposite of n, performing the previous search action in the "reverse" direction. For example, after /vbird, pressing N means searching "upward" for vbird.
Using /word together with n and N is very helpful! It allows you to repeatedly find the keywords you are searching for!
:n1,n2s/word1/word2/g n1 and n2 are numbers. Between lines n1 and n2, find the string word1 and replace it with word2! For example, to search for vbird between lines 100 and 200 and replace it with VBIRD:
':100,200s/vbird/VBIRD/g'. (common)
:1,$s/word1/word2/gor:%s/word1/word2/g From the first line to the last line, find the string word1 and replace it with word2! (common)
:1,$s/word1/word2/gcor:%s/word1/word2/gc From the first line to the last line, find the string word1 and replace it with word2! Also, before replacing, show a prompt for the user to confirm whether to replace! (common)
Delete, copy, and paste
x, X In a line, x deletes one character to the right (equivalent to the [del] key), and X deletes one character to the left (equivalent to the [backspace] key, i.e., the backspace key) (common)
nx n is a number. Delete n characters consecutively to the right. For example, to delete 10 characters consecutively, use '10x'.
dd Cut the entire line where the cursor is (common); use p/P to paste.
ndd n is a number. Cut n lines downward from the cursor. For example, 20dd cuts 20 lines (common); use p/P to paste.
d1G Delete all data from the cursor to the first line
dG Delete all data from the cursor to the last line
d$ Delete from the cursor position to the last character of the line
d0 That is the number 0. Delete from the cursor position to the first character of the line
yy Copy the line where the cursor is (common)
nyy n is a number. Copy n lines downward from the cursor. For example, 20yy copies 20 lines (common)
y1G Copy all data from the cursor line to the first line
yG Copy all data from the cursor line to the last line
y0 Copy all data from the character at the cursor to the beginning of the line
y$ Copy all data from the character at the cursor to the end of the line
p, P p pastes the copied data on the line below the cursor, and P pastes it on the line above the cursor! For example, if the cursor is currently on line 20 and 10 lines of data have been copied, then after pressing p, those 10 lines will be pasted after the original line 20, that is, starting from line 21. But what if you press P? Then the original line 20 will be pushed down to become line 30. (common)
J Join the line where the cursor is with the next line's data into the same line
c Repeat deleting multiple data, for example, delete 10 lines downward, [10cj]
u Undo the previous action. (common)
[Ctrl]+r Redo the previous action. (common)
The u and [Ctrl]+r keys are very common commands! One is undo, the other is redo. With these two function keys, your editing, hehe! Will be quite enjoyable!
. Don't doubt it! This is the decimal point! It means repeating the previous action. If you want to repeat deletion, pasting, etc., just press the decimal point '.'! (common)

Part 2: Description of buttons available for switching from Normal Mode to Edit Mode

Enter the input or replace editing mode
i, I Enter insert mode:
i means "input from the current cursor position", I means "start input from the first non-space character of the current line". (common))
a, A Enter insert mode:
a means "start input from the next character after the current cursor", A means "start input from the last character of the current line". (common))
o, O Enter insert mode:
These are the uppercase and lowercase forms of the English letter o. o opens a new line below the current cursor line; O opens a new line above the current cursor line! (common))
r, R Enter replace mode:
r only replaces the single character at the cursor once; R keeps replacing text at the cursor until ESC is pressed; (common))
Among the above keys, the text "--INSERT--" or "--REPLACE--" will appear in the lower-left corner of the vi screen. You can tell what action it is from the name!! Note especially, as we mentioned above, when you want to input characters into the file, you must see INSERT or REPLACE in the lower-left corner before you can input!
[Esc] Exit edit mode and return to normal mode (common))

Part 3: Description of buttons available for switching from Normal Mode to Command-Line Mode

Commands for saving, exiting, etc. in command-line mode
:w Write the edited data to a file on the hard disk (common))
:w! If the file attribute is "read-only", force writing to that file. However, whether you can actually write still depends on your file permissions for that file!
:q Exit vi (common))
:q! If you have modified the file but do not want to save it, use ! to force quit without saving the file.
Note that the exclamation mark (!) in vi often means "force".
:wq Save and then exit; if it is :wq!, it forces saving and then exits (common))
ZZ This is uppercase Z! If modified, save the current file, then exit! It is equivalent to (save and exit).
ZQ Do not save, force quit. Equivalent to:q!。
:w [filename] Save the edited data as another file (similar to Save As).
:r [filename] While editing, read in the data of another file. That is, append the contents of the file 'filename' after the line where the cursor is.
:n1,n2 w [filename] Save the contents from n1 to n2 as the file filename.
:! command Temporarily leave vi and execute the display result of command in command-line mode! For example
':! ls /home' lets you view the file information output by ls under /home within vi!
Changing the vim environment
:set nu Display line numbers. After setting, the line number will be shown as a prefix on every line.
:set nonu The opposite of set nu, it cancels line numbers!

Pay special attention: in vi/vim, numbers are very meaningful! A number usually means to repeat something that many times! It can also mean going to some "nth" something.

For example, to delete 50 lines, you use '50dd', right! The number is placed before the action. For instance, if I want to move down 20 lines, then '20j' or '20↓' will do.

Other extensions