Skip to main content

vim

vim is a powerful, highly customizable text editor in the Linux environment.


Minimal Workflow

vim test # vim followed by FILE_NAME; opens the specified file.
# Press Esc twice to return to Normal mode. Switching between modes requires returning to Normal mode.
i # Press 'i' to enter Insert mode. Use arrow keys to move the cursor. Select the position to edit and modify the text content.
:wq! # ':' enters Command-line mode; w saves; q quits; wq saves and quits; wq! forces save and quit. Save the modified content.

Operating Modes

vim has four working modes. Normal mode: commonly used for operating file content via commands. Press Esc from any mode to enter this mode; no display at the bottom of the file.
Insert mode: used for modifying file content. Enter from Normal mode using 'i' or 'a'; '--INSERT--' is displayed at the bottom of the file.
Command-line mode: used for file settings. Enter from Normal mode using ':'; ':' is displayed at the bottom of the file.
Visual mode: used for multi-line batch selection. Enter from Normal mode using 'v' (character), 'V' (line), or 'Ctrl+v' (block); 'VISUAL...' is displayed at the bottom of the file.

Normal Mode

Movement
xG : 2G Move to the second line
x% : 50% Move to the 50% mark of the file
G Move cursor to the last line
gg Move to the first line
n spaces :3+ space key move to the third character
Delete
dd delete the line where the cursor is located
xdd : 5dd delete 5 lines down from the cursor
dgg: delete from the cursor up to the beginning of the text
dG: delete from the cursor to the end of the text
Copy and paste
yy copy the current line
xyy :4yy copy four lines including the current line downwards
p paste

Command-Line Mode

:U #Restore the original appearance of the entire line (text state when the file was opened)
:q #If the file has not been modified, exit directly
:q! #The file has been modified, discard changes and exit
:wq #The file has been modified, save changes and exit
:wq! #Force save and exit
:e! #Discard changes and return to the state when the file was opened
:set nu #Display line numbers
:set nonu # Cancel display of line numbers
:1,$s/word1/word2/g or :%s/word1/word2/g #Search for the word1 string from the first line to the last line and replace it with word2! (Commonly used)
:1,$s/word1/word2/gc or :%s/word1/word2/gc #Search for the word1 string from the first line to the last line and replace it with word2! And display a prompt character for user confirmation (confirm) before replacement! (Commonly used)

Advanced Usage

vim: Converting doc to ucx

:# “:” Enter command-line mode
:set ff # Check text format
:set ff=dos #Convert format to dos
:set ff=unix #Convert format to unix
/test #Search downwards
?test #Search upwards
n #Find the next one,
N #Find the previous one

Batch Processing

Batch comment:
Ctrl + v Enter block selection mode, then move the cursor to select the lines you want to comment, then press uppercase I to enter line-start insertion mode and input comment symbols such as // or #, after input is complete, press ESC twice, Vim will automatically add comments to the beginning of all selected lines, save and exit to complete commenting.
Remove comments:
Ctrl + v Enter block selection mode, select the comment symbols at the beginning of the lines you want to delete, note that // requires selecting two characters, after selection, press d to delete the comments, ESC to save and exit.
Method 2: Replace command
Batch comment.
Use the following command to add comments at the beginning of specified lines.
Command format: :start_line_number,end_line_numbers/^/comment_symbol/g (note the colon).
Remove comments:
Command format: :start_line_number,end_line_numbers/^comment_symbol//g (note the colon).