Vim basic operations

Preface: The author is also new to Linux and may not have summarized it well

Linux training methods: Basic exercises

Today’s radio wave: Mermaid-JJ Lin

0:21━━━━━━?──────── 4:14
? ?

FollowLikeCollect Every encouragement from you is a great support for me

Table of Contents

1. Overview of Vim

2. Vim’s four command modes

3. Basic operations of insert mode

4. Basic operations of last line mode

5. Basic operations in command mode

6. Basic operations in visual mode

1. Overview of Vim

Vim is a text editor that is derived from vi (an old Unix editor) but is more powerful and flexible than vi. It has a simple interface and powerful editing functions, which can help users edit text files efficiently. Vim has multiple modes, including command mode, insert mode and visual mode. In command mode, users can perform commands such as moving the cursor, deleting text, or searching. In insert mode, the user can enter text. In visual mode, users can select text for processing. Vim also supports plug-ins, which can help users expand their editing capabilities and customize their editing environment. Due to its powerful features and efficient editing methods, Vim is widely used for programming and system management tasks.

2. Vim’s four command modes

Vim has four command modes, which are:

1. Command Mode (Normal Mode): In this mode, users can use most of Vim’s commands to move the cursor, delete, copy, paste, search, replace, etc.

2. Insert Mode (Insert Mode): In this mode, users can enter text just like in other text editors. Insert mode can be entered by pressing i, a, o and other commands.

3. Visual Mode (Visual Mode): In this mode, the user can select a text area by moving the cursor, and then perform certain commands on the area. Visual mode can be entered by pressing commands such as v, V, Ctrl + v, etc.

4. Last line mode (Command Mode): In this mode, users can execute some advanced commands, such as opening files, saving files, exiting Vim, etc. You can enter command line mode by pressing the colon (:) command.

Usually we use the following command to open the Vim editor

vim file name (if it exists, open it directly, if not, create it)

After we use the vim command to open the file, the default opening is the command mode, as follows:

Before that, you need to remember one sentence:Switching any mode is done in command mode! ! !

A picture to help you understand the above~

3. Basic operations of insertion mode

We are in command mode. Press any of the i/a/o keys to enter insert mode. If you are not sure which mode you are in. Like the author, note more the ESC key before switching.After entering insert mode, there will be a corresponding mark in the lower left corner, as follows:

Operation command:

Command mode + i: Indicates inserting content before the cursor (most commonly used)

Command mode + a: Indicates inserting content after the cursor

Command mode + o: Insert a new line below the line where the cursor is located

Command mode + s: Delete the character where the cursor is and start inserting

Command mode + I: Insert starting from the beginning of the line where the cursor is located. If there is a space at the beginning of the line, insert it after the space

Command mode + A: Start inserting at the end of your line where the cursor is located

Command mode + O: Start inserting on a new line above the line where the cursor is located

Command mode + S: Delete the line where the cursor is and start inserting

Switch back to command mode via ESC!

4. Basic operations of last line mode

Usually after we finish editing in insert mode, we return to our command mode. At this time, we usually think about exiting Vim or saving the code or data we just wrote, etc. First introduce the command operations of saving and exit!

In this regard, after returning to the command mode, pass shift +: enter the last line mode!

For common save and exit commands:

: w (save current file)

: w! (force to save the current file)
: q (Enter q to force quit vim without saving)
: q! (Enter q! to force quit vim without saving)

: wq! (Enter wq!, force save and exit vim) (most commonly used)

Some basic instructions:

List line numbers
“set nu”: After entering “set nu”, the line number will be listed in front of each line in the file.

Jump to a line in the file
“#”: The “#” sign represents a number. Enter a number after the colon and press the Enter key to jump to that line. For example, enter the number 15,
Press Enter again and it will jump to line 15 of the article.
Find characters
“/Keyword”: Press the “/” key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can keep pressing “n” to find the key you want later. So far.

“?Keyword”: Press the “?” key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can keep pressing “n” to search forward to find the key you want. So far.

Vim for multi-file editing

: vs file (open if it exists, create if not)

Open an additional file in the same window, of course you can also open multiple files!

Use Ctrl + w w to switch files

5. Basic operations in command mode

In addition to the “transfer station” that can switch to other modes, the command mode also has many instructions. The following are the main and commonly used instructions!

Move cursor
Vim can directly use the cursor on the keyboard to move up, down, left, and right, but regular vim uses lowercase English letters “h”, “j”, “k”,
“l” controls the cursor to move one space to the left, down, up, and right respectively.
Press “G”: move to the end of the article
Press ” $ “: Move to the “end” of the line where the cursor is located
Press “^”: move to the “beginning” of the line where the cursor is located
Press “w”: the cursor jumps to the beginning of the next word
Press “e”: the cursor jumps to the end of the next word
Press “b”: the cursor returns to the beginning of the previous word
Press “#l”: the cursor moves to the #th position of the line, such as: 5l,56l
Press [gg]: enter the beginning of the text
Press [shift+g]: enter the end of the text
Press “ctrl” + “b”: the screen moves “back” one page
Press “ctrl” + “f”: the screen moves “forward” one page
Press “ctrl” + “u”: the screen moves half a page “back”
Press “ctrl” + “d”: the screen moves half a page “forward”

Delete text
“x”: Each time you press it, delete a character at the cursor position.
“#x”: For example, “6x” means to delete the 6 characters “behind (including yourself)” where the cursor is located
“X”: Capital X, each time you press it, delete the character “before” the cursor position.
“#X”: For example, “20X” means to delete the 20 characters “before” the cursor position.
“dd”: delete the line where the cursor is located
“#dd”: Delete # lines starting from the line where the cursor is located

Copy
“yw”: Copy the characters from the cursor to the end of the word into the buffer.
“#yw”: Copy # words to the buffer
“yy”: Copy the line where the cursor is to the buffer.
“#yy”: For example, “6yy” means copying 6 lines of text “counting down” from the line where the cursor is located.
“p”: Paste the characters in the buffer to the cursor position. Note: All copy commands related to “y” must be combined with “p” to complete the copy and paste functions.

Replace
“r”: Replace the character where the cursor is.
“R”: Replace the character where the cursor is until the “ESC” key is pressed.
Undo last action
“u”: If you execute a command by mistake, you can press “u” immediately to return to the previous operation. Press “u” multiple times to perform multiple replies.
“ctrl + r”: Undo recovery
Change
“cw”: Change the word where the cursor is to the end of the word
“c#w”: For example, “c3w” means changing 3 characters
Jump to specified line
“ctrl” + “g” lists the line number of the line where the cursor is located.
“#G”: For example, “15G” means moving the cursor to the beginning of line 15 of the article

6. Basic operations in visual mode

When we write code, there are usually some places that need comments, so how to comment inVim? At this time, you need to use visual mode, in which commenting and uncommenting are more common operations.

Ctrl + v enter visual mode

Batch comment operations

After entering visual mode, use the arrow keys h j k l to select the area to be annotated. Note: Be sure to select all! ! !

Then Shift + i or I and then // then ESC to comment successfully

Batch delete operations

After entering visual mode, use the arrow keys h j k l to select the area to be annotated, and d to delete the specified area

Thank you for your patience in reading this? ( ′?` ). If there are any mistakes, please kick the author o(╥﹏╥)o!

Give me three rounds before leaving~