1.1 introduction to VI and vim
In Linux, we often edit and modify text files, that is, plain text files encoded by ASCII, Unicode or other codes. We introduced nano before. We will use more professional and powerful tools in actual work
Type of text editing:
- Full screen editor: nano (character tool), GEDIT (graphical tool), vi,vim
- Line editor: sed
vi
Visual editor, text editor, is one of the necessary tools for Linux. It has powerful functions, steep learning curve and great learning difficulty
vim
VIsual editor iMproved is used in the same way as vi, but it has more powerful functions. It is not necessary to install software during installation, and you need to install it yourself
Official website: www.vim.org
Other related editors: gvim a graphical version of the Vim editor
vim notes
1.2 preliminary of using vim
1.2.1 vim command format
vim [OPTION]... FILE...
Common options
+# #After opening the file, place the cursor in the second position#Line beginning, + default line end +/PATTERN #Place the cursor at the beginning of the first line matched by PATTERN -b file #Open file in binary mode -d file1 file2... #Compare multiple files, equivalent to vimdiff -m file #Read only open file -e file #Entering the ex mode directly is equivalent to executing ex file -y file #Easy mode (like "evim", modeless), you can directly operate the file, ctrl+o:wq|q! Save and exit without saving
explain:
- If the file exists, the file is opened and the contents are displayed
- If the file does not exist, it is created when it is saved for the first time after editing
example:
#centos does not have the vim tool installed by default and needs to be installed manually [root@rocky8 ~]# yum -y install vim
1.2.2 three main modes and conversion
vim is a schema editor, and keystroke behavior depends on vim's "schema"
Three common modes:
- Command or normal mode: the default mode, which can move the cursor and cut / paste text
- Insert or edit mode: used to modify text
- extended command or command line mode: save, exit, etc
Mode conversion
- Command mode -- > insert mode
i insert, Enter at the cursor I Enter at the beginning of the line where the current cursor is located a append, Enter after the cursor A Enter at the end of the line where the current cursor is located o Opens a new line below the line where the current cursor is located O Opens a new row above the row of the current cursor
- Insert mode - ESC ----- > command mode
- Command mode: --------- > extended command mode
- Extended command mode ----- ESC, enter ----- > command mode
1.3 extended command mode
Press the colon ":" to enter Ex mode and create a command prompt: on the left side of the screen at the bottom
1.3.1 extended command mode basic commands
w Write (save) disk file wq Write and exit x Write and exit X encryption q sign out q! Exit without saving, even the changes will be lost r filename Read the contents of the file into the current file w filename Writes the contents of the current file to another file !command Execute command r!command Read in command output
example:
:r /etc/issue #Read file into text :! pwd #Execute command :r!ls /boot #Read in command output :w a.txt #Save the contents of the current file to another file
1.3.2 address delimitation
Format:
:start_pos,end_pos CMD1.3.2.1 address delimitation format
# #Specific section#Line, for example, 2 indicates line 2 #,# #From left#Represents the starting line to the right#Represents the end line #,+# #From left#Represents the starting line of the, plus the right#Indicates the number of rows. Example: 2, + 3 indicates 2 to 5 rows . #Current row $ #Last line .,$-1 #Current row to penultimate row % #Full text, equivalent to 1$ /pattern/ #Look down from the current line until it matches the first line of pattern, that is, regular expression /pat1/,/pat2/ #It starts from the row to which pat1 pattern matches for the first time and ends at the row to which pat2 matches for the first time #,/pat/ #Start from the specified line and end when the first line matching pattern is found /pat/,$ #Drill down to find all lines from the first line that matches pattern to the end of the entire file
example:
:.,$d #Delete current row to last row1.3.2.2 address delimitation followed by an edit command
d #delete y #copy w file #Save the rows in the range to the specified file r file #Inserts all the contents of the specified file in the specified location
1.3.3 find and replace
format
s/What to find/Replace with/Modifier
explain:
What to find: you can use the basic regular expression pattern Replace with: you cannot use the pattern, but you can use it\1, \2, ...Backward reference symbols; You can also use“&"Reference the entire content found in the previous lookup
Modifier:
i #ignore case g #Global replace. By default, each line only replaces the first occurrence gc #Global replacement, ask before each replacement
Find separator in replace / replace with other characters, such as: #@
example:
:60,65y #Copy lines 60-65 P #Stick :%s/nologin/false/ #Replace all nologin s with false :%s#abc#123# #This will only replace the first abc in a row :%s#abc#123#g #Adding g will replace everything on a line
example:
s@/etc@/var@g s#/boot#/#i
1.3.4 working characteristics of customized vim
The configuration of the extended command mode is only valid for the current vim process. The configuration can be stored in a file for persistence
Profile:
/etc/vimrc #overall situation ~/.vimrc #personal1.3.4.1 line number
Display: set number,Abbreviation set nu Cancel display: set nonumber, Abbreviation set nonu
example:
:set nu #set number :set nonu #Cancel line number display1.3.4.2 ignore the case of characters
Enable: set ignorecase,Abbreviation set ic Do not ignore: set noic1.3.4.3 auto indent
Enable: set autoindent,Abbreviation set ai Disable: set noai
example:
:set ai #Automatic line alignment :set noai #cancel1.3.4.4 copy retention format
Enable: set paste Disable: set nopaste
example:
:set paste #Copy retention format1.3.4.5 Display Tab and line breaks ^ I and $display
Enable: set list Disable: set nolist
example:
:set list #Show Tab ^I and line breaks$ :set nolist #cancel1.3.4.6 highlight search
Enable: set hlsearch, Abbreviation: set hls Disable: set nohlsearch Abbreviation: nohl
example:
:set hls #Show highlight :nohl #Unhighlight1.3.4.7 syntax highlighting
Enable: syntax on Abbreviation: syn on Disable: syntax off Abbreviation: syn off
example:
:syn on #Syntax highlighting :syn off #Turn off syntax highlighting1.3.4.8 file format
Enable windows Format: set fileformat=dos Enable unix Format: set fileformat=unix Abbreviation set ff=dos|unix
example:
:set ff=unix #Convert to unix format :set ff=dos #Convert to windows format1.3.4.9 replace tab with space
Enable: set expandtab The default is 8 spaces instead Tab Disable: set noexpandtab Abbreviation: set et1.3.4.10 Tab is replaced by the number of specified spaces
Enable: set tabstop=# appoint#Replace Tab with a space Abbreviation: set ts=4
example:
:set et #Enable Tab to replace with spaces :set ts=8 #Specifies to replace the Tab with 8 spaces1.3.4.11 set indent width
#Indent right command mode > > #Indent left command mode<< #Set indent to 4 characters set shiftwidth=4
example:
:set cul #Sets the identification line of the line where the cursor is located1.3.4.12 help
set help
:help option-list :set or :set all
1.4 command mode
Command mode, also known as Normal mode, is powerful, but this mode inputs commands and displays them on the screen, so you need to remember a large number of shortcut keys to use them better
1.4.1 exit VIM
ZZ Save exit ZQ Exit without saving
example:
ZZ #Save exit ZQ #Exit Without Saving
1.4.2 cursor jump
Jump between characters:
h: Left L: right j: lower k: upper #COMMAND: Jump by#The specified number of characters
Jump between words:
w: The first word of the next word e: The ending of the current or next word b: The first word of the current or previous word #COMMAND: from#Specifies the number of words to jump at a time
Jump to current page:
H: Top of page M: Middle row of page L: Bottom of page zt: Move the current line of the cursor to the top of the screen zz: Move the current line of the cursor to the middle of the screen zb: Move the current line of the cursor to the bottom of the screen
Line start and line end jump:
^ Jump to the first non white space character at the beginning of the line 0 Jump to the beginning of the line $ Jump to end of line
Move between lines:
#G Jump to from#Line, equivalent to in extended command mode :# G Last line 1G, gg first line
example:
G #Last line 1G,gg #Jump to the first line 10G #Jump to line 10
Move between sentences:
) Next sentence ( Last sentence
Move between paragraphs:
} Next paragraph { the preceding paragraph
Command mode flip operation
Ctrl+f Flip to the end of the file Ctrl+b Flip to the top of the file Ctrl+d Flip half the screen to the end of the file Ctrl+u Flip half the screen to the head of the file
1.4.3 character editing
x Delete character at cursor #x Deletes the start at the cursor#Characters xp Swap the position of the character where the cursor is located and the character after it ~ Convert case J Deletes the line break after the current line
1.4.4 replace command
r Replace only one character at the cursor R Switch to REPLACE Mode (appears on the last line)-- REPLACE -- (prompt),Press ESC Return to command mode
1.4.5 delete command
d Delete command can be combined with cursor jump character to realize range deletion d$ Delete to end of line d^ Delete to non blank line header d0 Delete to beginning of line dw de db #COMMAND dd: Cut the line where the cursor is located #dd multi line deletion D: Delete from the current cursor position to the end of the line, which is equivalent to d$
example:
dd #Delete entire row dG #Delete current row to last row dgg #Delete current row to first row 4dd #Delete 4 lines
1.4.6 copy command (yank)
y Copy, behavior similar to d command y$ y0 y^ ye yw yb #COMMAND yy: Copy line #yy copy multiple rows Y Copy the entire line, equivalent to yy
example:
yy #Copy entire row 3yy #Copy 3 rows
1.4.7 paste command
p If the buffer is a whole line, paste it below the line where the current cursor is located; Otherwise, paste it behind the current cursor P If the buffer is a whole line, paste it above the line where the current cursor is located; Otherwise, paste to the front of the current cursor
example:
p #Paste to the back of the current row P #Paste to front of current row
1.4.8 change command
Switch to insert mode after command c is deleted
c$ c^ c0 cb ce cw #COMMAND cc #Delete the current line and enter new content, which is equivalent to S #cc C #Delete the current cursor to the end of the line and switch to the insertion mode, which is equivalent to c$
example:
cc #Delete the current line and switch to insert mode at the same time
Command mode operation text summary
1.4.9 search
/PATTERN: Look at the end of the file from the current cursor ?PATTERN: Find the file header from the current cursor n: Same direction as command N: Opposite to command
example:
/root #Search up ?root #Search down n #Same direction as command N #Reverse to command
1.4.10 undo changes
u Undo recent changes, equivalent to windows in ctrl+z #u undo previous changes U All changes to this line after the extinction mark falls on this line Ctrl - r Redoing the last undo change is equivalent to windows in ctrl+y . Repeat the previous operation #. Repeat the previous operation#second
example:
u #revoke Ctrl+r #Cancel cancel
1.4.10 advanced usage
<start position><command><end position>
Common commands: y copy, d delete, Gu uppercase, Gu lowercase
example:
0y$ command 0 → First come first y → Copy from here $ → Copy to the last character of the line
Example: paste "raymond" 100 times
100iraymond [ESC]
Advanced Usage
di" If the cursor is between, the content between will be deleted yi( Cursor in()Between, copy()Content between vi[ Cursor in[]Between, select[]Content between dtx Delete the character until it meets the first character after the cursor x character ytx Copy the character until it meets the first character after the cursor x character
example:
^gU$ #Capitalize the current line 100iraymond,Esc #Copy raymond 100 times di" #Delete the contents between double quotation marks di( di[ di' di{
1.5 visualization mode
At the end of the line, there is "- VISUAL –" indication, indicating that it is in VISUAL mode
Allow selected text blocks
v Character oriented, the following will be displayed at the end of the line:-- VISUAL -- V Facing the whole line, the following will be displayed in the last line:-- VISUAL LINE -- ctrl-v Block oriented (also known as column mode), the following is displayed in the last row:-- VISUAL BLOCK --
The visualization key can be used in conjunction with the move key
w)} arrows, etc
Highlighted text can be deleted, copied, changed, filtered, searched, replaced, etc
example:
v Select d #Delete selected character V Select the entire row d #Delete selected row ctrl+v Select the rectangle and delete or copy it
Example: insert at the beginning of a file line#
input ctrl+v Enter visualization mode input G Skip to the last line and select the first character of each line input I Switch to insert mode input # Press ESC key gg The first line, ctrl+v ,G ,I ,#,Esc
Example: insert the same content in the specified column
1.Position the cursor where you want to operate. 2.CTRL+v Enter the "visual block" mode and select how many rows to operate in this column 3.SHIFT+i(I) 4.Enter what to insert 5.Press ESC key
1.6 multi file mode
vim FILE1 FILE2 FILE3 ... :next next :prev previous :first first :last the last one :wall Save all :qall Exit all without saving :wqall Save exit all
1.7 multi window mode
1.7.1 multi file segmentation
vim -o|-O FILE1 FILE2 ... -o: Split horizontally or up and down -O: Vertical or left-right split( vim only) Switch between windows: Ctrl+w, Arrow
1.7.2 single file window segmentation
Ctrl+w,s: split, Split horizontally, split up and down Ctrl+w,v: vertical, Vertical split, left and right split screen ctrl+w,q: Cancel adjacent windows ctrl+w,o: Cancel all windows :wqall sign out
example:
Ctrl+w,s #Up and down split screen Ctrl+w,v #Left and right split screen Ctrl+w,q #Cancel adjacent windows Ctrl+w,o #Cancel all windows Ctr+w, → #Move to right window Ctrl+w, ↓ #Move to lower window
1.8 practice
1. Set tab indent to 4 characters in vim
2. Copy the / etc/rc.d/init.d/functions file to the / tmp directory and replace / etc/sysconfig/init in the / tmp/functions file with / var/log
3. Delete the # number at the beginning of all lines starting with # and followed by at least one white space character in the / tmp/functions file
1.9 help
:help :help topic Use :q to exit help #vimtutor
example:
[root@rocky8 ~]# vimtutor #Help manual
1.10 vim summary chart
Learn VIM using reference documents: https://www.runoob.com/w3cnote/all-vim-cheatsheat.html