LINUX vi editor command and LINUXvi editor command
Note: This article source https://www.cnblogs.com/jiayongji/p/5771444.html
(1) What is vi/vim?
In Linux, almost all configuration files exist in plain text, and the vi editor is available in all Linux distributions, therefore, using simple text editing software, you can easily modify various system configurations. Vi is a powerful text editor, while vim is an advanced version of vi, which not only displays text content in different colors, it can also be used as a program editor to edit shell scripts and C language programs.
(2) why learning vi/vim?
First, all Linux distributions have built-in vi editors instead of other text editors, which are very common. Second, many software editing interfaces call vi by default. Third, vi has the ability to edit programs. Finally, the vi Program is simple and the editing speed is quite fast.
(3) the three modes of vi and the Conversion Relationship between each mode
(4) common operations in general mode
[H (or left arrow key)] move the cursor one character to the left
[J (or downward arrow key)] move the cursor down a character
[K (or up arrow key)] move the cursor up to a character
[L (or right arrow key)] move the cursor one character to the right
[[Ctrl] + f] Move a Page Down (equivalent to the Page Down key)
[[Ctrl] + B] Move a Page Up (equivalent to the Page Up key)
[[0] or [Home]: move the cursor to the front of the current row
[[$] Or [End]: move the cursor to the End of the current row
[G] move the cursor to the last line of the file (at the first character)
[NG] n is a number (the same below) and is moved to the nth row in the current file.
[Gg] moves to the first line of the file, equivalent to "1G"
[N [Enter] move the cursor down n rows
[/Word] searches for the string with the word content in the file (downward query)
【? Word: search for a string whose content is word in the file (search up)
[N] indicates a duplicate search action, that is, to find the next
[[N] Reverse lookup next
[: N1, n2s/word1/word2/g] n1 and n2 are numbers. Search for the word1 string from line n1 to line n2 and replace it with word2.
[: 1, s/word1/word2/g] searches for word1 registration from the first line (line n is the same) to the last line, and replaces it with word2 [: 1, s/word1/word2/g] searches for word1 registration from the first line (line n) to the last line, and replaces it with word2 [: 1, s/word1/word2/gc functions are the same as above, but each replacement requires confirmation from the user.
[X, X] x deletes a character backward, which is equivalent to [Delete]. X deletes a character forward, which is equivalent to [Backspace].
[Dd] deletes the entire line of the cursor.
[Ndd] Delete n rows down where the cursor is located
[Yy] copy the row where the cursor is located
[Nyy] copy the n rows down the cursor
[P, P] p: paste the copied data under the cursor line; P: paste the copied data on the cursor line
[U] Undo the previous operation
[[Ctrl] + r] Undo multiple times
[.] This is the decimal point key. Repeat the previous operation.
(5) switch from general mode to edit mode
1. Enter the insert mode (6 commands)
[I] Insert from the current cursor
[I] from the current cursor
[A] Insert the cursor from the next character
[A] Insert from the last character in the row where the cursor is located
[O] lowercase letter o, Insert a new row at the next line of the current row where the cursor is located and start to insert
[O] an uppercase letter "O" is used to Insert a new row at the top of the row where the cursor is currently located and start inserting.
2. Enter the replacement mode (2 commands)
[R] Only replaces the character of the cursor once.
[R] replaces the character of the cursor until you press the [ESC] key.
[[ESC] exit the editing mode and return to the normal mode.
(6) switch from general mode to Command Line Mode
[: W] Save the file
[: W !] If the file is read-only, force save the file
[: Q] leaving vi
[: Q !] Force exit vi without saving
[: Wq] Save and exit
[: Wq !] Force save and exit
【:! Command: displays the result after you temporarily exit vi and execute a command in the command line.
[: Set nu] displays the row number.
[: Set nonu] undisplays the row number.
[: W newfile] Save
[: Set fileencoding] view the current file encoding format
[: Set fileencoding = UTF-8] sets the current file encoding format to UTF-8, or other encoding formats.
[: Set fileformat] view the broken line format of the current file (dos \ windows, unix or macintosh)
[: Set fileformat = unix] sets the disconnection format of the current file to unix.
(7) file recovery mode
[[O] pen Read-Only] open a file in Read-Only mode
[[E] dit anyway] Open the file in normal mode and the content of the saved file will not be loaded.
[[R] ecover] loads the content of the temporary file
[[D] elete it] open a file normally and delete the temporary file
[[Q] uit] Press q to exit vi. No other operations are performed.
[A] bort] is similar to quit.
(8) block selection (used in Normal Mode)
[V, V] v: select the reversed area of the cursor. V: select the reversed line of the cursor.
[[Ctrl] + v] block selection. Select text in a rectangle.
[Y] copy the reversed area to the clipboard.
[D] Delete the reversed content.
(9) Multi-file editing
[Vim file1 file2] Open two files at the same time
[: N] edit the next file
[: N] edit the previous file
[: Files] lists all files currently opened with vim.
(10) Multi-Window Function
[: Sp [filename] open a new window to display the new file. If you only enter: sp, the two windows will display the same file.
[[Ctrl] + w + j] move the cursor to the bottom window
[[Ctrl] + w + k] move the cursor over the window
[[Ctrl] + w + q] exit the current window
(11) vim configuration file
The vim configuration file is/etc/vimrc, but it is generally not recommended to directly modify this file, but to create a new hidden file under the user root directory:
Vim ~ /. Vimrc
Edit the file. Common configurations are as follows:
Bash
"The content after double quotation marks is a comment
Set nu "display row number
Set hlsearch "reverse display of searched strings
Set backspace = 2 "can be deleted at any time with the backspace key
Set autoindent "automatic contraction
Set ruler "displays the status in the bottom row
Set showmode "display mode in the lower left corner
Set bg = dark "displays different background colors and can also be light
Syntax on "syntax check, Color Display
(12) Dos and Linux line breaks (file conversion)
Dos2unix [-kn] file [newfile]
Unix2dos [-kn] file [newfile]
-K: Retain the original mtime format of the file.
-N: Retain the original file and output the converted content to the new file.
(13) Transformation of the Language Family Code
Iconv -- list languages supported by iconv
Iconv-f original encoding-t new encoding filename [-o new file]
-F: from, followed by the original encoding format
-T: to, followed by the new encoding format
-O file: an optional parameter to create a new file
For example, convert/tmp/a.txt from big5 encoding format to utf8 encoding format:
Iconv-f big5-t utf8/tmp/a.txt-o new.txt