Quick start of editor vim

Linux editor Vim

Insert picture description here


Preface

Learning computer will inevitably be exposed to the Linux operating system, and the important task of editing various files falls on the vim editor. Learning how to use vim can be regarded as an indispensable step for getting started with the Linux operating system. The learning of vim can let you Readers will appreciate the difference between Linux and Windows. So hurry up and start the next vim learning!

1. Introduction

vi is the first full-screen interactive editing program for the Linux system. It has been favored by users since its inception. After decades of unfailing, it will last forever, and its powerful editing functions can be combined with any Comparable to a newest editor. Various vi variants in later generations also basically inherited the powerful, concise, fast, and efficient features of vi. Among them, the vim editor is the most portable, powerful, and widely used.

Two, the basic mode of vim

The vim editor can be divided into 4 modes according to different usage modes, namely normal mode, insert mode, command-line mode and visual mode.

1. Standard mode

Usually after entering vim, it is in standard mode (or called command mode) by default . In this mode, any keyboard input is treated as a command. The input of the command is usually not echoed, only the result of the execution is displayed.
In this mode, the user can input commands to control the movement of the screen cursor, delete characters, words or lines, move and copy a certain section, and enter the other 3 modes. Press Esc in any other mode to return to this mode.

2. Insert mode

Only in insert mode can the user perform editing operations such as text input and modification, so this mode is also called edit mode. Enter the following command in standard mode to enter this mode:

command Features
a Enter a character after the cursor position
A Enter a character at the end of the line where the cursor is
i Enter a character before the cursor
I (large letter i) Enter a character at the beginning of a non-blank line in the line where the cursor is
The Add a new line under the line where the cursor is, and start typing at the beginning of the new line
O (capital letter o) Add a new line above the line where the cursor is, and start typing at the beginning of the new line

3. Command line mode

In this mode, the user can save the file or exit vim, or set the editing environment, such as searching for strings, listing line numbers, and so on. Enter ":" (general command), "/" (forward search) or "?" (reverse search) in the command mode to enter this mode. The commands entered by the user will be displayed on the bottom line, and these commands will be executed after they have been entered by pressing the Enter key. This mode is also called last-line mode.

4. Visual mode

Enter "v" (select by character), "V" (select by line), Ctrl+V (select by block) in the command mode to enter this mode, there will be "-VISUAL-" "-VISUAL LINE" at the bottom of the screen -" or "-VISUAL BLOCK-" and other prompts. In this mode, by moving the cursor to select text, the selected text will be displayed in reverse video, which provides efficient and intuitive editing functions.

Three, the basic operation of vim

1.Vim entry, save and exit

In the Linux operating system, hit the keyboard combination Ctrl+Alt+T to directly enter the terminal. Generally, the Linux built-in vim can directly enter "vim a certain file path", if the file is in the current directory, you can directly enter the file name.
Insert picture description here
As shown in the figure, after entering vi, there will be a wave match on the left of the screen. Anything with this symbol means that the line is currently empty. At this point, the command line mode has been entered.
To exit vim, enter the following command in command line mode:

Command type command Description
edit :e filename Edit the file named filename. If the file does not exist, create a new filename file
Save :w Save the file, the file should already have a name
- :w filename Save the file under filename
drop out :q Exit, you cannot exit if the file name has been modified
- :q! Forcibly exit without saving, regardless of whether the file has been modified
- :wq Exit after saving

2. Move the cursor

Command type command Description
Basic operation h,j,k,l (key L) Respectively equivalent to the left, down, up, and right arrow keys
Word manipulation w Move to the beginning of the next word
- e Move to the end of the next word
- b Move to the beginning of the previous word
Row operation Number 0 Move to the beginning of the line
- $ Move to the end of the line
- G Move to the end of the file
- gg Move to the beginning of the file
- H Move to the top of the current screen
- M Move to the beginning of the middle line of the current screen
- L Move to the beginning of the last line at the bottom of the current screen
- n- Move up n rows
- n+ Move down n rows
- nG Move to line n
Page operations Ctrl+F Turn the screen "up" by one page, which is equivalent to PageUp
- Ctrl+B Turn the screen "down" by one page, which is equivalent to PageDown
- Ctrl+U Turn the screen half a page "up"
- Ctrl+D Turn the screen half a page "down"

3. Text editing

Text editing includes input, modification, copy, paste, delete (you can use the Delete key and Backspace key), and restore operations. See the table below for details:

Command type command Description
modify r Modify the character where the cursor is, press "r" and directly type in the character to be modified
- R Enter the substitution state, modify the character at the position specified by the cursor, the substitution state does not end until you press Esc
copy yy Copy the line where the cursor is
- nyy Copy the n lines from the beginning of the line where the cursor is. For example, 3yy means copy three lines
- and ^ Copy the cursor to the beginning of the line
- and $ Copy the cursor to the end of the line
- is Copy a word (word)
- yG Copy the cursor to the end of the file
- y1G Copy the cursor to the beginning of the file
Paste p After pasting to the cursor
- P Paste before the cursor
delete x Delete a character at the cursor position
- X Delete the previous character at the cursor position
- s Delete the character under the cursor and enter the input mode
- S Delete the line where the cursor is located, and enter the input mode
- dd Delete the line where the cursor is
- ndd Delete the n lines down from the line where the cursor is
- D Delete to the end of the line, equivalent to d$
- dG Delete to the end of the file
- d1G Delete to the beginning of the file, equivalent to dgg
Reply (undo) u Undo the last operation, you can undo multiple times
- U Before the cursor leaves, all editing operations are resumed
- Ctrl+R Return to the state before the undo operation

4. Find and Replace

Both the search and replace functions of vim support regular expressions, which can match very complex keywords.

Command type command Description
Find /<Character to find> Find characters down
- ?<Character to find> Find characters up
- n Keep looking
- N Reverse lookup
replace :[range]s/pattern/string/[c,e,g,i] range: Specify the search range, for example 1, $ refers to the replacement range from the 0th line to the last line
- s: refers to switch to replacement mode
- pattern: refers to the string to be replaced, you can use regular expressions
- string: refers to the replacement string
- c: Ask before each replacement
- e: No error is displayed
- g: Force the entire line to replace
- i: Not case sensitive

to sum up

I believe that readers will have some experience when they see this. If they have not used Unix-like operating systems, they may be very uncomfortable. So many commands are nothing more than replacing the mouse click operation in the window. At first, you may feel that there is no mouse for this command. It is convenient to click, but when you are familiar with it, you will find the benefits. It can be operated without the mouse under the Linux operating system, and the computer can be operated more efficiently, quickly and accurately through the command line and shortcut keys.
In fact, most of the above commands are rarely used. You can easily use vim by mastering a few commands about saving and exiting, deleting, copying and restoring, and moving the cursor up, down, left, and right. Practice makes perfect, practice hard, and gradually adapt to using the Linux operating system for code development, which is also very helpful for future careers.

The content of this article is extracted from: "Embedded Linux C Language Application Development Tutorial Mobile Learning Edition | Second Edition"
jointly published by China Industry and Information Publishing Group and People's Posts and Telecommunications Publishing House

Guess you like

Origin blog.csdn.net/weixin_43594279/article/details/113825051