Home  >  Article  >  php教程  >  A complete list of vi commands under linux

A complete list of vi commands under linux

高洛峰
高洛峰Original
2016-12-12 16:24:361166browse

Command to enter vi
vi filename: Open or create a file, and place the cursor at the beginning of the first line
vi +n filename: Open the file, and place the cursor at the beginning of the nth line
vi + filename: Open the file, and Place the cursor at the beginning of the last line
vi +/pattern filename: Open the file and place the cursor at the first string matching pattern
vi -r filename: The system crashed when editing with vi last time, recover filename
vi filename....filename: Open multiple files and edit them in sequence

Move cursor command
h: Move the cursor one character to the left
l: Move the cursor one character to the right
space: Move the cursor one character to the right
Backspace : The cursor moves one character to the left
k or Ctrl+p: The cursor moves one line up
j or Ctrl+n: The cursor moves one line down
Enter: The cursor moves one line down
w or W: The cursor moves one character to the right to the beginning
b or B: move the cursor one word to the left to the beginning
e or E: move the cursor one word to the right to the end
): move the cursor to the end of the sentence
(: move the cursor to the beginning of the sentence
}: move the cursor to the beginning of the paragraph
{ : The cursor moves to the end of the paragraph
nG: The cursor moves to the beginning of line n
n+: The cursor moves down n lines
n-: The cursor moves up n lines
n$: The cursor moves to the end of line n
H: The cursor moves to Top line of the screen
M: The cursor moves to the middle line of the screen
L: The cursor moves to the last line of the screen
0: (Note the number zero) The cursor moves to the beginning of the current line
$: The cursor moves to the end of the current line

Screen scrolling class Command
Ctrl+u: Scroll half a screen towards the beginning of the file
Ctrl+d: Scroll half a screen towards the end of the file
Ctrl+f: Scroll one screen towards the end of the file
Ctrl+b; Scroll one screen towards the beginning of the file
nz: Move the nth line Scroll to the top of the screen.

Insert text command
i: before the cursor
I: at the beginning of the current line
a: after the cursor
A: at the end of the current line
o : Open a new line below the current line
O: Open a new line above the current line
r: Replace the current character
R: Replace the current character and the characters after it until the ESC key is pressed
s: From the current cursor position Start by replacing the specified number of characters with the entered text
S: Delete the specified number of lines and replace them with the entered text
ncw or nCW: Modify the specified number of characters
nCC: Modify the specified number of lines

Delete command
ndw or ndW: Delete the n-1 characters starting at the cursor and following it
do: Delete to the beginning of the line
d$: Delete to the end of the line
ndd: Delete the current line and n-1 lines after it
x or X: To delete a character, x deletes what is after the cursor, and pattern: Search pattern from the beginning of the cursor to the beginning of the file
n: Repeat the last search command in the same direction
N: Repeat the last search command in the opposite direction
: s/p1/p2/g: All p1 in the current line Replace with p2
: n1,n2s/p1/p2/g: Replace all p1 in the n1 to n2 lines with p2
: g/p1/s//p2/g: Replace all p1 in the file with p2

Option settings
all: List all option settings
term: Set the terminal type
ignorance: Ignore case in search
list: Display tab stops (Ctrl+I) and end-of-line flags ($)
number: Display line numbers
report: Display the number modified by line-oriented commands
terse: Display a brief warning message
warn: Display NO write message if the current file is not saved when switching to other files
nomagic: Allow searching mode, use special characters without "" in front of them
nowrapscan: prohibit vi from searching from both ends of the file and start from the other end
mesg: allow vi to display information written by other users to their own terminals using write

Finally Line mode command
: n1, n2 co n3: Copy the content between lines n1 to n2 to line n3
: n1, n2 m n3: Move the content between lines n1 to n2 to line n3 Next line
: n1, n2 d: Delete the content between lines n1 and n2
: w: Save the current file
: e filename: Open the file filename for editing
: x: Save the current file and exit
: q: Exit vi
: q!: Exit vi without saving the file
: !command: Execute the shell command command
: n1,n2 w!command: Use the contents of lines n1 to n2 in the file as the input of command and execute it. If If n1 and n2 are not specified, it means that the entire file content is used as the input of the command
: r!command: Put the output result of the command command into the current line

Register operation
"?nyy: Put the current line and the n lines below it Save contents to register? , where ? is a letter, n is a number
"?nyw: Save the current line and the next n words to the register?, where ? is a letter, n is a number
"?nyl: Save the current line and the next n characters into the register?, where? is a letter and n is a number
"?p: Take out the register? and place it at the cursor position. here? It can be a letter or a number
ndd: Delete the current line and n lines of text below it, and put the deleted content into the deletion register No. 1.

Usage of VI
-------------------------------------------------- ------------------------------------

1. Insert text
┌──┬─── ─────────┐
│Command│Description│
├──┼───────────┤
│i │Insert text before the current character│
├──┼ ────────────┤
│I  │Insert text at the beginning of the line │
├──┼───────────┤
│a  │Add after the current character Text│
├──┼────────────┤
│A │Add text at the end of the line│
├──┼───────────────┤
│o │Insert a blank line after the current line│
├──┼───────────┤
│O │Insert a blank line before the current line│
├──┼────── ──────┤
│R  │Enter text in rewrite mode │
└──┴───────────┘
2. Move the cursor
┌─────┬── ─────────┐
│Command│Description│
├─────┼───────────┤
│j or down arrow│Move down one line│
├ ─────┼───────────┤
│k or up arrow│Move up one line│
├─────┼───────────┤
│h or left arrow│Move one character to the left│
├─────┼──────────┤
│l or right arrow│Move one character to the right│
├───── ┼────────────┤
│w   │Move right one word│
├─────┼───────────┤
│W   │Move right one word Space separated words │
├─────┼──────────┤
│b   │Move one word to the left│
├─────┼──────── ───┤
│B   │Move one space-separated word to the left│
├─────┼──────────┤
│0   │Move to the beginning of the line│
│Ctrl- F │Page forward│
├─────┼──────────┤
│Ctrl-B │Page backward│
├─────┼──── ───────┤
│nG  │To the nth line│
├─────┼──────────┤
│G  │To the last line│
└─── ──┴───────────┘
3. Replacement text
┌─────┬──────┐
│Command│Description│
├─────┼─ ─────┤
│$   │To the end of the line│
├─────┼──────┤
│(   │To the beginning of the sentence│
├──────┼──── ──┤
│)   │To the end of the sentence│
├──────┼─────┤
│{  │To the beginning of the paragraph│
├─────┼────── ┤
│}  │To the end of the paragraph│
└─────┴─────┘

4. Delete text
┌───┬────────────┐
│Command│Description│
├───┼──────────┤
│r  │Replace a character│
├───┼────────────┤
│c  │Modify the text until the Esc key is pressed│
├───┼──────────┤
│cw │Modify the next word│
├───┼────── ─────┤
│cnw  │Modify the next n words│
└───┴───────────┘
5. Text editing
┌──┬────────────────────┐
│Mingji│Description│
├──┼─────────── ───────────┤
│yy │Move a line of text into the default buffer│
├──┼────────────────── ─────┤ n │YN │ Shift the next word to the default buffer area │
├ ─┼───────────────────┤






│ynw │Move the following n words to the default buffer│
├──┼───────────────────────────┤
│p │If If the default buffer contains a line of text, insert an empty line after the current line and paste the sound content in the default buffer into this line; if the default buffer contains │
│ │Multiple words, paste these words to the right of the cursor. │
├──┼───────────────────────┤
│P │If the default buffer contains a line of text, the current one │
│ │ Insert an empty line before the line and paste the contents of the default buffer into this line; if the default buffer contains multiple words, paste these words to the left of the cursor

  │
└──┴─────────────────────┘
6. Save and exit
┌────────────┬─ ─────────────┐
│Command│Description│
├────────────┼─────────────── ┤
│zz    │Save and exit│
├───────────┼────────────────────┤
│:w filename  │Write file│
├───────────┼───────────────┤
│:W    │Write file│
├────────── ─┼───────────────┤
│:x    │Save (if the current file has been modified) and exit│
├─────────────┼── ─────────────┤
│:q!    │Exit directly without saving the file│
├────────────┼───────── ──────┤
│:q    │Exit vi      │

VI Common Tips

VI command can be said to be the most commonly used command to edit files in the Unix/Linux world, but because of its numerous command sets, many People are not accustomed to using it. In fact, you only need to master the basic commands and then use them flexibly. You will discover its advantages and gradually like to use this method. This article aims to introduce some of the most commonly used commands and advanced application techniques of VI.

1. Introduction to basic commands

---- 1. Cursor commands

k, j, h, l - up, down, left and right cursor movement commands. Although you can use the 4 cursor keys on the right side of the keyboard in Linux, it is still very useful to remember these 4 commands. These 4 keys are the basic position of your right hand on the keyboard.
nG——Jump command. n is the number of lines. This command immediately jumps the cursor to the specified line.
Ctrl+G - Report the number of rows and columns at the cursor position.
w, b - Make the cursor skip a word forward or backward.
---- 2. Editing commands
i, a, r - Insert character commands before, after and at the cursor (i=insert, a=append, r=replace).
cw, dw - commands to change (replace)/delete the word at the cursor (c=change, d=delete).
x, d$, dd - commands to delete one character, delete all characters from the cursor to the end of the line, and delete the entire line.
---- 3. Search command
----/string, ?string——Command to search the corresponding string backward or forward from the cursor position.
---- 4. Copy command
---- yy, p - command to copy a line to the clipboard or remove the contents of the clipboard.

2. Frequently Asked Questions and Application Tips

---- 1. Read the contents of /etc/passwd in a new file and extract the username part.
---- vi file
---- :r /etc/passwd Read /etc/passwd at the cursor position in the open file file 🎜---- :%s/:.*//g Delete all parts from the colon to the end of the line after the user name in /etc/passwd.
---- You can also read the file content after the specified line number, for example, use the command ":3r /etc/passwd" to read all the contents of /etc/passwd starting from the 3rd line of the new file.
---- We can also use the following method to delete all blank lines and comment lines starting with # in the file.
---- #cat squid.conf.default | grep -v ^$ | grep -v ^#

---- 2. After opening a file for editing, I learned that the logged-in user did not have write permissions for the file and could not save it. The changes needed to be saved to a temporary file.
---- vi file
---- :w /tmp/1 Save all the changes made, you can also save some of the changes to a temporary file, for example, just the content between lines 20 to 59 Save to file /tmp/1, we can type the following command.
---- vi file
---- :20,59w /tmp/1

---- 3. Use VI to edit a file, but a large section of content needs to be deleted.
---- First use the editing command "vi file" to open the file, then move the cursor to the line that needs to be deleted and press Ctrl+G to display the line number. Then press Ctrl+G at the end to display the line number at the end of the file. .
---- :23,1045d Assume that the line numbers obtained twice are 23 and 1045, then delete all the contents during this period. You can also use ma and mb commands to mark the start line and end line to be deleted, and then Use the ":a,bd" command to delete.

---- 4. Add some strings to the beginning or end of each line of the entire file or some lines.
---- vi file
---- :3,$s/^/some string / Insert "some string" at the beginning of the first line to the last line of the file.
---- :%s/$/some string/g Add "some string" at the end of each line in the entire file.
---- :%s/string1/string2/g Replace "string1" with "string2" in the entire file.
---- :3,7s/string1/string2/ Only replace "string1" in the 3rd to 7th lines of the file with "string2".
---- Note: s stands for substitute, % stands for all rows, and g stands for global.

---- 5. Edit 2 files at the same time, copy text from one file and paste it into the other.
---- vi file1 file2
---- yy Copy the line at the cursor of file 1
---- :n Switch to file 2 (n=next)
---- p The cursor of file 2 Paste the copied line at the location
---- :n Switch back to file 1

---- 6. Replace the path in the file.
---- Use the command ":%s#/usr/bin#/bin#g" to replace all paths /usr/bin in the file with /bin. You can also use the command ":%s//usr/bin//bin/g", where "" is an escape character, indicating that the following "/" character is a character with actual meaning, not a delimiter.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn