(a) What is Vi/vim?
Almost all the configuration files in the Linux world exist in plain text, and the VI editor is available on all Linux distribution systems, so it is convenient to easily modify the various configurations of the system with simple text editing software. VI is a powerful text editor, and Vim is the advanced version of VI, not only can display text content in different colors, but also can do such as Shell script, C language program editing and other functions, can be used as a program editor.
(b) Why study Vi/vim?
First of all Linux distribution system will default built-in VI editor, not necessarily with other text editor, very general, and secondly, many of the software's editing interface will be called by default VI; Third, VI has the ability of program editing; Finally, vi program is simple, the editing speed is quite fast.
(iii) The three modes of VI and the conversion relationship between the various modes
(iv) General mode common operation
"H (or left ARROW key)" Cursor moves left one character
"J (or DOWN arrow)" Cursor moves down one character
"K (or Up ARROW key)" Moves the cursor up one character
"L (or right ARROW key)" Cursor moves right one character
"[Ctrl] + F" screen moves down one page (equivalent to page DOWN key)
"[Ctrl] + B" screen moves up one page (equivalent to page UP key)
"[0] or [Home]" cursor moves to the front of the current line
"[$] or [end]" cursor moves to the end of the current line
The "G" cursor moves to the last line of the file (at the first character)
"NG" N is a number (hereinafter), moving to the nth row in the current file
"GG" moves to the first line of the file, equivalent to "1G"
"N[enter]" cursor moves down n rows
"/word" finds a string in a file with Word (look down)
"? Word" finds a string in a file that contains word (look up)
"[n]" means a duplicate lookup action, which is to find the next
"[N]" reverse lookup next
": n1,n2s/word1/word2/g" N1, N2 as numbers, find N2 string between line N1 and Word1 line and replace it with WORD2
": 1, $s/word1/word2/g" find word1 registration from the first line (same as Nth line) to the last line and replace it with WORD2
": 1, $s/word1/word2/gc" function as above, but each time the replacement will let the user confirm
"X,x" x deletes a character backwards, equivalent to [Delete],x deletes a character forward, equivalent to [Backspace]
"DD" deletes the entire line where the cursor is located
"NDD" Removes the downward n-line of the cursor
"YY" the line where the cursor is copied
"Nyy" Copy the down-n line where the cursor is located
"P,p" p to paste the copied data on the next line of the cursor; p to paste the data that has already been copied on one line of the cursor
"U" undoes the previous action
"[Ctrl] + R" undo multiple times
"." This is the decimal key, repeat the previous action
(v) operation of the general mode switch to edit mode
1. Enter insert mode (6 commands)
"I" is inserted from the current cursor location
"I" from the current cursor
"A" inserts from the next character where the current cursor is located
"A" starts inserting at the last character of the line where the cursor is located
"O" English lowercase o, insert a new line at the next line where the cursor is currently located and start inserting
"O" English capital letter O, insert a new line at the top of the line where the cursor is currently located and start inserting
2. Enter replacement mode (2 commands)
"R" replaces only the one character where the cursor is located once
"R" will always replace the character of the cursor until the [ESC] key is pressed
"[ESC]" exit edit mode back to normal mode
(vi) General mode switch to command line mode
": W" Save file
": w!" If the file is read-only, force the file to be saved
": Q" Leave VI
": q!" does not save forced departure VI
": Wq" left after saving
": wq!" forced to leave after saving
【:! Command "Temporarily leaves VI to the command line after executing a command to display the result
": Set nu" Displays line numbers
": Set Nonu" to suppress line numbers
": W newfile" Save As
(vii) File recovery model
"[O]pen read-only" opens the file as read-only
"[E]dit Anyway" opens the file in normal mode and does not load the contents of the staging file
"[R]ecover] loading staging file contents
[D]elete It opens the file in normal mode and deletes the staging file
"[Q]uit" press Q to leave VI, no other operation
"[A]bort" is similar to the Quit function
(eight) block selection (for general mode)
"V,v" V: Anti-white selection of the place where the cursor is passing, V: White selection of the line through which the cursor passes
"[Ctrl] + V" Block selection, you can select text in a rectangular way
"Y" copies the anti-white places to the Clipboard
"D" Removes the anti-white content
(ix) Multi-file editing
"Vim file1 file2" Open two files at a time
": N" edits the next file
": N" Edit Previous file
": Files" lists all files that are currently open with vim
(10) Multi-window function
": SP [filename]" opens a new window that displays a new file, and if you enter only: SP, the two Windows display the same file
"[Ctrl] + W + j" Cursor moves to the lower window
"[Ctrl] + W + k" cursor moves to the upper window
"[Ctrl] + W + q" leaves the current window
(11) Vim configuration file
The configuration file for Vim is/ETC/VIMRC, but it is generally not recommended to modify this file directly, but to create a new hidden file in the user root directory:
Vim ~/.VIMRC
Then edit this file, the usual configuration is as follows:
"The content behind the double quotation marks is a comment
Set Nu "Show line number
Set Hlsearch "lookup string anti-white display
Set backspace=2 "Can be deleted at any time with backspace bar
Set autoindent "Auto Indent
Set ruler "show status on the bottom row
Set Showmode "display mode in the lower left corner
Set Bg=dark "displays a different background color and can also be light
Syntax on "Grammar check, color display
(12) DOS and Linux break characters (file conversion)
Dos2unix [-kn] file [NewFile]
Unix2dos [-kn] file [NewFile]
-K: Preserves the original mtime time format of the file
-N: Preserves the original old file and outputs the converted content to the new file
(13) Language encoding conversion
Iconv--list lists iconv supported language codes
Iconv-f Original code-t new code filename [-o new file]
-f:from, followed by the original encoding format
-t:to, followed by new encoding format
-o file: Optional parameter to create a new file
Example: Converting/tmp/a.txt from BIG5 encoded format to UTF8 encoding format:
Iconv-f big5-t Utf8/tmp/a.txt-o New.txt
Linux Vi/vim Editor Common commands and Usage summary