1. Vim Common Operation
1.1 Vim Introduction
(1) Vim is a powerful full- screen text editor , which is the most commonly used text editor on Linux/unix, and its function is to create, edit and display text files.
(2) Vim has no menu, only commands.
(3) Installation of Vim editor
① 4 Mounting packages required Vim-filesystem, Vim-common, vim-enhanced, Vim-minimal
② View the existing packages for this machine: #rpm –qa | grep vim
③ Installing the VIM Editor: Yum–y install vim*
1.2 Vim settings for the editor
(1) Modify the configuration file/ETC/VIMRC or #~/.vimrc
① Auto Indent by C syntax: Set cindent, such as press {, then change line Auto Indent
② Auto Indent: set Autoindent
③ Add line number :set Nu
④tab indent 4 characters :set tabstop=4
(2) Multi-file concurrent operation ( mainly used for copying and pasting in different files )
① open multiple files simultaneously: #vim test1.c test2.c test3.c
② in the last line mode, view open file:: args, then display below, [test1.c] test2.c test3.c, where add [] file represents the file currently being edited
③ switch to another file:: First,: Next,:p rev,: Last
1.3 Vim Mode of Operation
(1) 3 modes of operation and switching
(2) Basic editing
① Insert Command
Command |
Role |
A |
Insert in the word specifier the cursor |
A |
Insert at the end of the line where the cursor is |
I |
Insert in the word match either the cursor |
I |
Insert at the beginning of the cursor |
O |
Insert a new line under the cursor |
O |
Insert a new line on the cursor |
S |
Remove the light-setting characters and enter insert mode |
S |
Clears (contents) the line where the cursor is located and enters insert mode |
② Positioning commands
: Set nu
command |
effect |
Set line number |
Cancel line number |
GG |
to first line |
G |
to the last line |
NG (n Go) |
to Nth line |
: n |
to Nth line |
$ |
Move to end of line |
0 (number 0) |
Move beginning |
③ Delete command
Command |
Role |
X |
Delete the character at the cursor location |
Nx |
Delete the first n characters at the cursor location |
Dd |
Delete Row of cursor, NDD delete n rows |
Dg |
Delete the line from the cursor to the end of the file |
D |
Delete the cursor at the end of the line |
: n1,n2d |
Delete a specified range of rows |
④ copy and Cut commands
Command |
Role |
Yy |
Copy when moving forward |
Nyy |
Copy the following n rows of the current row |
Dd |
Cut when moving forward |
Ndd |
Cut the following n rows of the current row |
D |
Delete the cursor at the end of the line |
P, p |
Paste at the current cursor row or line |
⑤ Replace and Undo commands
Command |
Role |
R |
Replaces the character at which the cursor is located |
R |
Start replacing characters from the cursor until you press ESC to end |
U |
Cancel the previous action |
Ctrl+r |
Redo Operation |
⑥ Search and search Replace command
Command |
Role |
/string ? string |
Search forward (or backward) the specified string,/forward,? Represents a backward lookup. Ignore case: Set/ic |
N |
Searches for the next occurrence of the specified string |
:%s/old/new/g |
Full text replaces the specified string,s preceded by%, representing the full-text range, and g for global, if not plus% for the current row. |
: n1,n2s/old/new/g |
s preceded by a number indicating that the specified string is substituted within the specified range of rows |
⑦ Save and Exit commands
Command |
Role |
: W |
Save only, do not exit |
: w New_filename |
Save as specified file |
: Wq |
Save changes and exit |
Zz |
Shortcut keys (shift+z+z), save changes and exit |
: q! |
Do not save changes to exit |
: wq! |
forcibly save the changes and exit (the file owner and root can be used, for the file only read-only permission !) ) |
2. Vim Usage Tips
(1) execute command in last line mode : Format,! Command
For example,:!ls-l/etc,:!date
(2) import command execution result
① Import Command Execution result: #r! order, eg. #r!ll/tmp
② import file to Cursor location: #r file name, eg. #r/etc/issue
(3) Defining shortcut keys:map shortcut Trigger command
① Comment Line:: Map ^p i#<esc>//definition ctrl+p for I means the cursor goes back to the beginning of the row and into insert mode, then enter #, and finally press ESC to return to the command mode (note that the ^p in the input should be pressed ctrl+v+p).
② Delete Note:: Map ^b 0x//ctrl+b,0 indicates that the cursor is positioned to the beginning of the line, but still in command mode, x indicates the deletion of the character under the cursor.
③ Insert Email Address:: Map ^m [email protected]<ESC>//ctrl+m, where I means go into insert mode,<esc> return to command mode.
(4) Continuous line annotation
① comment Consecutive lines:: n1,n2s/^/#/g//where ^ denotes a null character at the beginning of the line, replaced by #
② Delete consecutive lines comment:: n1,n2s/^#//g//Replace # with empty
③ to//To NOTE:: n1,n2s/^/\/\//g//where \ means escape , that is, to//. Originally, the empty character at the beginning of the line is converted to//, which acts as a comment.
(5) Replacement: : AB mymail [email protected] //ab command, replace MyMail with [email protected]
4th Chapter Text Editor vim