Introduction to VIM operation mode

Time:2021-8-11

What is VIM?

VIM is a text editor developed from VI. Code completion, compilation, error jump and other convenient programming functions are particularly rich, which are widely used by programmers. Together with Emacs, it has become the favorite text editor for UNIX like system users.

VIM’s design philosophy is a combination of commands. Users have learned a variety of commands to move / jump between texts and other editing commands in common mode. If they can flexibly combine them, they can edit text more efficiently than those editors without mode.

At the same time, VIM is similar to many shortcut key settings and regular expressions, which can assist memory. And VIM is optimized for programmers.

Three operating modes of vim

VIM has three operation modes: command mode, insert mode and last line mode.

Three mode switching shortcut keys:

pattern Shortcut key
Command mode ESC
Input mode I or a
last line node :

Command mode

In the command mode, you can control cursor movement and enter commands to copy, paste, delete and find text.

Use commandvim filenameAfter entering the editor view, the default mode is command mode. At this time, tapping the keyboard letter will be recognized as a command. For example, tapping D twice on the keyboard will delete the line where the cursor is located.

The following are shortcuts commonly used in command mode:

operation Shortcut key
Move cursor left h
Move cursor right L (lowercase L)
Move cursor up k
Cursor down j
Move the cursor to the next word w
Move the cursor to the previous word b
Move cursor to line n nG
Move cursor to first row gg
Move cursor to last line G
Quickly return to the last cursor position Ctrl+o
Delete current character x
Delete previous character X
Delete entire row dd
Delete a word DW or daw
Delete to end of line D $or D
Delete to beginning of line d^
Delete to end of document dG
Delete to document header d1G
Delete n rows ndd
Delete n consecutive characters nx
Changes the cursor position to uppercase or lowercase ~
Copy the entire row where the cursor is located YY (3yy means copy 3 rows)
Paste after cursor (down) p
Paste in front of (above) the cursor P
shear dd
Switching uplink and downlink ddp
Replace the whole row, that is, delete the row where the cursor is located and enter the insert mode cc
Undo one or n operations u{n}
Undo all modifications of the current row U
Resume undo operation Ctrl+r
The entire line is indented to the right >>
The whole line will go back to the left <<
If the file has not been changed, it will not be saved and left. If the file has been changed, it will be saved and left ZZ

Input mode

In the command mode, press the I or a key to enter the input mode. In the input mode, you can normally use the keyboard keys to insert and delete text.

The following are common shortcut key operations for input mode:

operation command
Line feed Enter
Deletes the previous character of the cursor Space
Delete the next character after the cursor Delete
Move cursor in text Direction key
Move cursor to beginning / end of line Home/End
Switch cursor to input / replace mode Insert
Exit the input mode and switch to the command mode Esc

last line node

Press in command mode:Key to enter the bottom line command mode, in which you can enter a single or multiple character command.

The following are common shortcuts in the bobbin thread command mode:

operation command
preservation :w
sign out :q
Save and exit :wq(:wq!Indicates forced save (exit)
Save the file as a different file name :w new_filename
set number :set nu
suppress line number :set nonu
Center this line :ce
Keep this line of text to the right :ri
Keep this line to the left :le
Look under the cursor for a string named word :/word
Look over the cursor for a string named word :?word
Repeat the action of the previous search :n
Look for the word1 string from the first line to the last line and replace it with word2 :1,$s/word1/word2/gor :%s/word1/word2/g

We can refer to the following illustration to understand these three modes:

Introduction to VIM operation mode

Use example

In this example, you will use VIM to write a Tang poem in a text file.

  1. Create a new file and enter VIM command mode.
VIM thinking at night.txt

Introduction to VIM operation mode

Direct inputVIM file nameYou can enter the general mode of vi. Please note that you must add a file name after VI, whether the file exists or not!

  1. PressiEnter the input mode and input the poem name of “silent night thoughts”.

Introduction to VIM operation mode

In general mode, just press I, O, a and other characters to enter the input mode!
In the edit mode, you can find the word – Insert – in the status bar in the lower left corner, that is, the prompt that you can enter any character.
At this time, exceptEscIn addition to this key, other keys can be regarded as general input buttons, so you can make any editing.

  1. Press the ECS key to return to the command mode and enter the lower thread command:ce, center the title of the poem.

Introduction to VIM operation mode

  1. PressoKey wrap and enter the input mode to enter the first line of poetry.

Introduction to VIM operation mode

  1. Press the ECS key to return to the command mode and enter the lower thread command:ce, center the first line.

Introduction to VIM operation mode

  1. PressoKey wrap and enter the input mode to enter the second line of poetry.

Introduction to VIM operation mode

  1. Press the ECS key to return to the command mode and enter the lower thread command:ce, center the second line.

Introduction to VIM operation mode

  1. Execute the baseline command in command mode:wqLeave vim.

Learn interesting knowledge, make interesting friends and shape interesting souls!

Hello, everyone, I am the author of the programming of samadhi, hermit king. My official account is programming Samadhi. I hope you can give us more advice.