Linux Chapter Three-Enter VIM

Introduction to Linux

One, VIM text editor

1. Introduction to VI/VIM

  • 1. A text editing tool. The most classic and popular text editing tool in Linux is vi/vim. Of course, there are also some other text editing tools.

For example,
emacs
pico
nano
joe
jed and
so on, but we only need to master vi/vim

  • 2,1, Vi editor is the most basic text editor on linux and unix, working in character mode. Because it does not require a graphical interface, vi is an efficient text editor. Although there are many graphical interface editors available on Linux, the functions of vi in ​​system and server management are unmatched by those graphical editors.

  • 2.2. The vi editor can perform many text operations such as output, delete, search, replace, block operations, and users can customize it according to their needs, which is not available in other editing programs.

  • 2.3, vim is an enhanced version of vi, easier to use than vi. Almost all vi commands can be used on vim.


2. How to use VI/VIM

1. VI/VIM features and characteristics

  • VI/VIM is a plain text editor. Unlike word, it can typesetting, adjust font size, change fonts, etc., vi/vim is just a text editing tool, it can only operate on the content of the text, such as new Add content, modify content, delete content, etc.
  • vi is a full-screen text editor, it will occupy the entire screen when editing text

2. Three modes of VI/VIM

  • 2.1, vim editor has three editing modes, namely command mode (editing mode); input mode (insert mode); last line mode \

Three modes

Edit mode effect
Command mode The user executes commands, such as copy lines, paste lines and wait
Input mode Used to enter text, modify text, etc.
Last line mode Used to find text, save changes, etc.

How to switch between the three editing modes of vi/vim
Command mode -> input mode
i: insert, before the character at the current cursor, switch to input mode
I: capital i, at the beginning of the line where the current cursor is Input mode
a: append, switch to input mode
A: switch to input mode at the end of the line where the current cursor is located
o: open, create a new line below the line where the current cursor is located and switch to input mode
O : Capital o, not the number 0, create a new line above the line where the cursor is currently located and switch to input mode
Input mode -> command mode
ESC
command mode -> last line mode
last line mode -> command mode
ESC ESC


3. How to edit VI/VIM text files

  • Open text mode: After the file is opened, it is in command mode by default

vim filename //Open the file
vim +n filename //Open the file and locate the Nth line
vim +/pattern filename //Open the file and locate the first line of content matched by /pattern;

  • The way to close the text: enter the last line mode: enter q! Exit without saving, wq write and exit, x force save and exit.
  • Command mode to close the file: ZZ exit
  • Move the cursor

Insert picture description here

  • Character editing:
    Insert picture description here
  • Delete command: d

In command mode, the d command is often used in combination with jump commands, such as
d$: means to delete the content from the current cursor position to the end of the line
d^: means to delete the content from the current cursor position to the first non-blank character position of the first line
d0 :
Means to delete the content from the current cursor position to the beginning of the absolute line #d Jump sign: Delete # in the specified range of the jump sign, for example
------------------- ----#dw
-----------------------#de
------------------- ----#db
dd: delete the entire line where the current cursor is located
#dd: delete the # line including the line
where the current cursor is located D: delete the content of the line where the current cursor is located, keep the blank line
Use of the d command in the last line mode
Syntax::StartADD,EndADDd
where StartADD and EndADD refer to a range, such as

1,5d: to delete the first to fifth lines
*

  • Range representation
display method significance
The cursor is currently unknown
$ the last line
+# Line # of the line where the cursor is
$-# Last # line
% full text
  • Copy command: YY
  • Paste command: p

p: lowercase p;
-if the entire line is deleted or copied, paste it under the line where the cursor is
-if the deleted or copied is not the entire line, paste it after the character where the cursor is located
P: uppercase P
if you delete or The copied content is the entire line, then paste it to the top of the line where the cursor is.
If the deleted or copied line is not the entire line, paste it to the front of the character where the cursor is.

  • Replace: r

R: Replacement mode, replace multiple characters
r#: Replace the current cursor character with #, where # can be any character

  • Undo edit: u

u: undo the previous editing operation, connect u command to undo the previous n editing operations
#u: directly undo the previous # editing operations
ctrl+r: restore the last undo operation

  • Repeat the previous editing operation:.
  • Visualization mode:

In the visualization mode, all the editing operations mentioned above can be performed on the
selected content.
V: Select the content by character V: Select the content by rectangular block (line)

  • Text search:

/pattern: Find matching content
from top to bottom? pattern: Find matching content from bottom to top
n: Find the next item of matched content from top to bottom
N: Find the next item of matched content from bottom to top

  • Find and replace: s

The s command can only be used in the last line mode.
Syntax: ADDR1, ADDR2 s/pattern/string/gi
1,5 s/abc/def/g: means to replace all abc in the first to fifth lines with def

  • Display or cancel the line number in the last line mode:

set nu: display line number
set nonu: cancel display line number


4. A small exercise in vim

Create directory /tmp/test and copy /etc/man_db.conf to this directory.
Edit the man_db.conf file with vim to
Insert picture description here
set the display line number; Move to line 43, move 57 characters to the right, and see what is in the brackets content;
Insert picture description here

Move to the first line and search down for the string "gzip", which is on the first line;Insert picture description here

Change man between lines 50 to 100 to MAN;
Insert picture description here

After the modification, I suddenly regretted it and want to restore it all. What are the methods?
Use u to undo the previous operation.

Copy the contents of the 9 lines from 65 to 73 (containing MANDB_MAP) and paste it after the last line;
enter the V visualization view, select the 9 lines and copy the uppercase Y, and then jump to the last line and paste it with p

Uncomment the content between lines 21 and 42;
use the command in the last line mode: 21,42s/#/ /g
Insert picture description here

Save this file as man.test.config. In the
last line mode: :w man.test.config
to line 27, and delete 15 characters. What is the first character that appears?
Insert picture description here

Add a new line in the first line, and enter "I am a student" in this line

Save and exit
Insert picture description here

Guess you like

Origin blog.csdn.net/LBJ19224/article/details/109143927