Home  >  Article  >  php教程  >  Introduce the use of vi command under linux

Introduce the use of vi command under linux

高洛峰
高洛峰Original
2016-12-12 16:43:151128browse

One of the most powerful editors - vi
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 to be super difficult to use, but vi is the most basic editor, so I hope readers can learn it well and they will be able to use it smoothly in the UNIX world in the future. Unhindered and effortless, 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. The functions of each mode are as follows:
1. Comand mode: Control the movement of the screen cursor, deletion of characters or cursors, move and copy a section and enter Insert mode, or go to Last line mode.
2. Insert mode: Text data input can only be done in Insert mode. 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.
However, 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.
Basic operations of vi
? Enter vi
After entering vi and file name at the system prompt, you can enter the vi full-screen editing screen:
$ vi testfile
One thing to pay special attention to is that after you enter vi, you are in the "Command" mode", you need to switch to Insert mode to enter text. Users who are using vi for the first time will want to use the up, down, left and right keys to move the cursor first. As a result, the computer keeps beeping, which makes them angry to death, so after entering vi, don't move around first, and then switch to Insert.
?Switch to Insert mode to edit files
Press ‘i’, ‘a’ or ‘o’ in Command mode to enter Insert mode. At this point you can start typing text.
i: Insert, insert the entered text from the current cursor position.
a: Added, start inputting text from the next character where the cursor is currently located.
o: Insert a new line and enter text from the beginning of the line.
? Switch to Insert → Command mode, press Esc key
You are currently in Insert mode, you can only keep typing. If you find that you made a typo and want to use the cursor keys to move back and delete the word, you must press the ESC key to switch back to Command mode and then delete the text.
?Leave vi and save the file
In Command mode, you can press the colon ":" to type in Last line mode, for example:
:w filename (enter "w filename" to save the article to the specified file name filename)
: wq (Enter "wq", because the file name testfile has been specified when entering, so testfile will be written and leave vi)
:q! (Enter "q!" to force exit and abandon the edited file)

Command mode function Key list
After introducing the command mode command, the function keys with the word "commonly used" are added after the command to indicate the more commonly used vi commands. Readers, you must learn and remember them.
(1) I, a, o switch to Insert mode. [Super commonly used]
(2) To move the cursor
vi can directly use the cursor keys on the keyboard to move up, down, left, and right, but regular vi uses lowercase English letters
h, j, k, l to control the cursor left, down, and left respectively. Move one space up and right.
Press Ctrl+B: Move the screen back one page. [Commonly used]
Press Ctrl+F: Move the screen forward one page. [Commonly used]
Press Ctrl+U: Move the screen back half a page.
Press Ctrl+D: Move the screen forward half a page.
Press 0 (number zero): Move to the beginning of the article. [Commonly used]
Press G: Move to the end of the article. [Commonly used]
Press w: the cursor jumps to the beginning of the next word. [Commonly used]
Press e: the cursor jumps to the end of the next word.
Press b: the cursor returns to the beginning of the previous word.
Press $: Move to the end of the line where the cursor is located. [Commonly used]
Press ^: Move to the first non-blank character in the line.
Press 0: Move to the beginning of the line. [Commonly used]
Press #: Move to the #th position of the line, for example: 51, 121. [Commonly used]
(3) Delete text
x: Delete one character after the cursor position each time you press it. [Very commonly used]
#x: For example, 6x means deleting the next 6 characters at the cursor position. [Commonly used]
X: X in large letters, each time you press it, delete the character in front of the cursor position.
#X: For example, 20X means delete the first 20 characters at the cursor position.
dd: Delete the line where the cursor is. [Very commonly used]
#dd: For example, 6dd means to delete the text 6 lines down from the line where the cursor is located. [Commonly used]
(4) Copy
yw: Copy the characters from the cursor to the end of the word into the buffer.
(Want to be the opposite of the functions of #x and #X)
p: Paste the characters in the buffer to the position of the cursor (commands 'yw' and 'p must be used together).
yy: Copy the line where the cursor is. [Very common]
p: Copy a single line to where you want to paste it. (Commands 'yy' and 'p' must be used together)
#yy: For example: 6yy means copying the text 6 lines down from the line where the cursor is located. [Commonly used]
p: Copy multiple lines to where you want to paste. (The instructions ‘#yy’ and ‘p’ must be used together)
“ayy: Put the copied lines into buffer a. vi provides the buffer function, which can store commonly used data in the buffer
"ap: Paste the data placed in buffer a.
"b3yy: Store three lines of data into buffer b.
"b3p: Paste the data in buffer b
(5) Replace
r: Replace the character at the cursor: [Commonly used]
R: Replace the character until Esc is pressed.
(6) Undo (undo) the previous command
u: If you mistakenly operate a command, you can press u immediately to revert to the previous operation. [Very common]
.: . You can repeat the last command.
(7) Change
cw: Change the location of the cursor. word to the end of the word $.
c#w: For example, c3w means changing 3 words.
(8) Jump to the specified line
Ctrl+G: List the line number of the line where the cursor is located.
#G: For example, 15G. , means moving the cursor to the beginning of line 15 of the article [Commonly used]
Introduction to commands in Last line mode
Readers, before you want to use Last line mode, please remember to press the Esc key to make sure you are in Command mode, and then press Colon ":" or "/" or "? "One of the three keys enters Last line mode.
1. List line numbers
set nu: After entering "set nu", the line number will be listed in front of each line of the article.
2. Jump to a certain part of the article One line
#: The pound sign represents a number. Enter the number before the Last line mode prompt symbol ":" and press Enter again to jump to the line. For example: 15 [Enter] will jump to the 15th line of the article. [Commonly used]
3. Search for string
/Keyword: Press / first, and then enter the word you want to find. If the keyword you are looking for for the first time is not what you want, you can keep pressing n to search further.
? Keyword: Press ? first, and then enter the word you want to find. If the keyword you are looking for for the first time is not what you want, you can press n to find the key you want.
4. Replace string
1, $s/string/replae/g: Entering "1, $s/string/replace/g" in last line mode will replace the full text string with replace string. , where 1,$s means that the search range is from the beginning to the end of the article, and g means that all replacements do not need to be confirmed.
%s/string/replace/c: It will also replace the full-text string string with the replace string. , the difference from the above command is that %s and 1,$s have the same function, and c means that you must confirm whether to replace again before replacing
1,20s/string/replace/g: Replace lines 1 to 20. Replace the string in the space with the relation string.
5. Save the file
w: Press w before the last line mode prompt ":" to save the file. [Very common]
#, # w filename: If you want to Extract a certain paragraph of the article and save it into another file. Use this command # to represent the line number, such as 30,50 w nice, and save the 30th to 50th lines of the article you are editing into the nice file.
6. q: Press q to leave. Sometimes if you cannot leave vi, you can use "! : Force to leave vi, such as "q!"
qw: It is generally recommended to use it with w when leaving, so that files can be saved when leaving. [Commonly used]

VI command collection:

Commands to enter vi

vi filename: Open or create a new 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 first line n at the beginning of the 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



Moving cursor type 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 word 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 j End of word

): The cursor moves to the end of the sentence

(: The cursor moves to the beginning of the sentence

}: The cursor moves 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 command

Ctrl+u: Scroll half a screen to the beginning of the file

Ctrl+d: Scroll half a screen to the end of the file

Ctrl+f: Scroll one screen to the end of the file

Ctrl+b; Scroll one screen to the beginning of the file

nz: Scroll the nth line to At 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 current time. End of 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: Starting from the current cursor position, 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 words

nCC: Modify the specified number of lines



Delete command

ndw or ndW: Delete the cursor position Beginning and n-1 characters after

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 one 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: Change Replace all p1 in the current row with p2

: n1,n2s/p1/p2/g: Replace all p1 in the n1 to n2 row with p2

: g/p1/s//p2/g: Replace All p1 in the file are replaced with p2



Option settings

all: List all option settings

term: Set the terminal type

ignorance: Ignore case in search

list: Show tab stops ( Ctrl+I) and the end-of-line mark ($)

number: Displays the line number

report: Displays the number modified by line-oriented commands

terse: Displays a brief warning message

warn: Before going to something else If the current file is not saved, the NO write message will be displayed.

nomagic: allows the use of special characters without "" in front of the search mode.

nowrapscan: prohibits vi from starting from the other end when the search reaches both ends of the file.

mesg: Allow vi to display information written by other users to their own terminals using write



The last line command

: n1, n2 co n3: Copy the content between lines n1 to n2 to line n3 Next

: n1, n2 m n3: Move the content between lines n1 to n2 to the bottom of line n3

: n1, n2 d: Delete the content between lines n1 to 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!: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 and n2 are not specified, it means that the entire file content will be used as the command. Input

: r!command: Put the output of command command into the current line.

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
Previous article:vi command listNext article:vi command list