Home  >  Article  >  php教程  >  Compilation of how to use vi command to modify files and save them under Linux

Compilation of how to use vi command to modify files and save them under Linux

高洛峰
高洛峰Original
2016-12-12 16:27:511163browse

vi is a screen editor provided by all UNIX systems. It provides a window device through which files can be edited. Of course, people who know a little bit about UNIX systems will more or less find vi super difficult to use, but vi is the most basic editor, so I hope readers can learn it well and will definitely use it in the UNIX world in the future. Smooth travel,
 With ease, because several other text processors are not standard equipment in UNIX. Maybe someone else's Linux machine does not have joe or pico installed. If you don't know vi, then you may not be able to do it.

The basic concept of vi

Basically, vi can be divided into three operating states, namely Command mode, Insert mode and Last line mode.
Functional distinction of each mode As follows:

 1. Comand mode: Control the movement of the screen cursor, the deletion of characters or cursors, move and copy a section and enter Insert mode, or go to Last line mode.
 2. Insert mode: Only in Insert mode, text data can be input. Press Esc to return to Comand mode.
 3. Last line mode: Save the file or leave the editor. You can also set the editing environment, such as searching for strings, listing line numbers, etc.
 But vi can be simplified into two modes, that is, Last line mode is also included in Command mode, and vi is divided into Command and Insert modes.

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. Restore 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: Move the cursor one character to the left
k or Ctrl+p: Move the cursor up one line
j or Ctrl+n: Move the cursor down one line
Enter: Move the cursor down one line
w or W: Move the cursor one character to the right to the beginning
b or B: Move the cursor one word to the left to the beginning of the word
e or E: Move the cursor one word to the right to the end of the word
): 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 the nth line
n+: The cursor moves down n lines
n-: The cursor moves up n lines
n$: The cursor moves to the end of the nth line
H: The cursor moves To the 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 to the beginning of the file
Ctrl+d: Scroll half a screen toward the end of the file
Ctrl+f: Scroll one screen toward the end of the file
Ctrl+b; Scroll one screen toward the beginning of the file
nz: Move the nth Scroll the line to the top of the screen. If n is not specified, the current line will be scrolled 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 subsequent characters until the ESC key is pressed
s: From the current cursor position Starting at , replace 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 : Delete a character, x deletes the one after the cursor, and ?pattern: Search for 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: Replace all p1 in the current line Replace all 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 Replacement

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: If the current file is not saved when switching to another file, the NO write message will be displayed.
nomagic: Allow the use of special characters without "" in front of the search mode.
nowrapscan: Prohibit vi from searching at both ends of the file. Start from the other end again
mesg: Allow vi to display information written by other users to their own terminals using write

Last line command
: n1, n2 co n3: Copy the content between lines n1 to n2 to n3 Next line
: n1, n2 m n3: Move the content between line n1 to line n2 to the bottom of line n3
: n1, n2 d: Delete the content between line n1 to n2
: w: Save Current file
: e filename: Open the file filename for editing
: x: Save the current file and exit
: q: Exit vi
: q!: Do not save the file and exit vi
: !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 the command and execute it. If n1, n2 is not specified, it means that the entire file content is used as the input of the command
: r!command: Use the command command The output result is put into the current line

Register operation
"?nyy: Save the contents of the current line and the next n lines into the register?, where ? is a letter and n is a number
"?nyw: Save the current line and the next n lines into the register? And save 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 to the register?, where ? is a letter, 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. Replace text
┌─────┬──────┐
│Command│Description│
├─────┼──────┤
│$   │To the end of the line│
├─────┼──────┤
│(   │To the end of the sentence Beginning│
├─────┼──────┤
│)   │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 one character│
├─── ┼────────────┤
│c  │Modify the text until you press Esc │
├───┼────────────┤
│cw │Modify next Word│
├───┼───────────┤
│cnw  │Modify the next n words│
└───┴────────────┘
5. Text editing
┌──┬───────────────────┐
│Mingsi│Description│
├──┼───── ────────────────┤
│yy │Move a line of text into the default buffer│
├──┼──────────── ──────────┤
│yn │Move the next word into the default buffer│
├──┼────────────────── ────┤
│ynw │Move the following n words to the default buffer│
├──┼───────────────────── ┤
│p  │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 it is missing The provincial 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 skills

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 command

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 after the username in etc/passwd starting from the colon and ending at the end of the line.
---- 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 separator.

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