Vi and Vim for Linux

1. Basic introduction

The Linux system will have a built-in Vi text editor. Vim has the ability of program editing, which can be regarded as an enhanced version of VI. It can actively use font colors to identify the correctness of grammar, which is convenient for program design. Code completion, compilation, and errors are particularly rich in programming-friendly functions, and are widely used among programmers.

Two, common mode

2.1 Normal Mode

Opening a file with Vim directly enters the normal mode (this is the default mode). In this mode, you can use the [Up, Down, Left, and Right] keys to move the cursor, you can use [Delete Character] or [Delete Entire Line] to process the file content, and you can also use [Copy and Paste] to process your file data.

2.2 Insert mode

Press and i,I,o,O,a,A,r,Rwait for any letter to enter the editing mode, generally speaking, ijust press.

2.3 Command line mode

Based on insert mode, enter Esc, then enter :to enter command line mode . In this mode, you can provide relevant instructions. The actions of completing reading, saving, replacing, leaving Vim, displaying line numbers, etc. are achieved in this mode.

Three, practical operation

(1) Establish a remote connection to operate the Linux system on Xshell. For details, please refer to Linux remote operation .
(2) Enter in Xshell vim hello.cppto enter the normal mode of Vim.
(3) Enter ithe insert mode and write the code.
insert image description here
(4) Enter Esc, and then enter to :enter the command line mode. Enter again wqto complete saving the file.

4. Mode conversion and shortcut keys

4.1 Mode conversion

insert image description here

4.2 Shortcut keys

Normal mode :

  1. Copy the current line (yy), copy the 5 lines below the current line (5yy), and paste (enter p)
  2. Delete the current line (dd), delete 5 lines down from the current line (5dd)
  3. Use shortcuts to the last line (G) and first line (gg) of the document
  4. Undo an action in insert mode (u)
  5. Move the cursor to a line (line number shift+g)

Command line mode :

  1. Find a word in the file (/keyword, press Enter to search, enter n to find the next one)
  2. Set the line number of the file, cancel the line number of the file (:set nu and :set nonu)

Reference materials: 2021 Han Shunping learns Linux in a week

Guess you like

Origin blog.csdn.net/professor_tao/article/details/125580604