Linux commands (5): vi

Entering the command vi 
vi filename: Open or create a new file, the first line and the first cursor 
vi + n filename: open the file, and the n-th row first cursor 
vi + filename: open the file, and the cursor the last line of the first 
vi + / pattern filename: open the file, and a first cursor pattern matches the string 
vi -r filename: a system crash occurs when the last editing timing VI, restore filename 
VI filename ... .filename: open multiple files, edit sequentially 

moving a cursor commands of
h: cursor one character 
l: the cursor one character 
space: the cursor one character 
Backspace: cursor one character 
k or Ctrl + p: cursor move row 
j or Ctrl + n: cursor down one line 
Enter: cursor down one line 
w or w: cursor right to a prefix word 
b or B: cursor one character to the prefix 
e or E: cursor right suffix to a word 
): cursor to end of the sentence 
(: move the cursor to the first sentence 
}: move the cursor to the beginning of paragraph 
{: move the cursor to the end of the paragraph 
nG: first cursor to the n-th line 
n +: cursor down n rows 
n -: move cursor row n 
n $: move the cursor to the end of the n-th row 
H : Move the cursor to the top line 
M: middle of the screen cursor to the line 
L: a screen cursor to the last row 
0 :( note the number zero) the cursor to the current line 
$: Move the cursor to the end of the current line 

of the screen roll commands of 
Ctrl + u: first turned to the document half screen 
Ctrl + d: turned to the end of the file half screen 
Ctrl + f: turning a screen to the end of the file 
Ctrl + b; the file header down one screen 
nz: the roller n-th line to the top of the screen, when n is not specified current row rolling to top of the screen. 

Insert text type command 
i: before the cursor 
I: In the current line 
a: after the cursor 
A: A current line 
o: open a new row below the current line 
O: open new line above the current line 
r: Replace the current character 
R: replace the current character and the subsequent characters, press the ESC key until 
s: starting from the current cursor position to input text alternative specified number of characters 
S: delete the specified number of rows, and instead of the input text 
ncw or nCW: modify the specified number of words 
nCC: modify the specified number of rows 

delete command 
ndw or ndW: start and at the cursor is deleted word n-1 
do: to delete the first row 
d $: delete to end of line 
ndd: delete the current after row n-1 and line 
x or X: after deleting a character, deleting the cursor x, and before the cursor is deleted X 
Ctrl + u: erasing input mode at the input text 

search and replace commands 
/ pattern: from the cursor at the beginning of the search pattern to the end of the file 
pattern:? from the cursor at the beginning of the first search pattern to the file 
n: repeat the last search command in the same direction 
N: in the opposite direction to repeat the last search command 
: s / p1 / p2 / g: all the current line p1 p2 are used alternatively 
: n1, n2s / p1 / p2 / g: the first row n1 to n2 all p1 p2 are used alternatively 
: g / p1 / s // p2 / g: the all p1 p2 were replaced with the file 

option settings 
all: lists all option settings circumstances 
term: set terminal type 
ignorance: ignore case in the search 
list: display tab stops (Ctrl + I) and the end of the line mark ($) 
Number the : display the line number 
report: modify the display line-oriented command over the number of 
terse: a brief warning message displayed 
warn: when to file if the other did not save the current file is displayed NO write information 
nomagic: allow the search mode, use special characters without leading "\" in 
nowrapscan: ban vi in the search reaches both ends of the file, and from the other end of the beginning 
mesg: allow other users to use vi show write write the information on your own terminal 

last line mode command 
: n1, n2 co n3: n2 to n1 line to the line of Copy the contents of the next line n3 
: n1, n2 m n3: n1 rows between the content to move to the next row of n2 n3 line 
: n1, n2 d: delete the contents of the rows n1 to n2 between rows 
: W: save the current file 
: e filename: Open the file filename for editing 
: x: save the current file and exit 
: q: quit vi 
: q !: do not save the file and exit vi 
:! The Command: execute shell commands the Command 
: N1, n2 w command:! the file n1 n2 row to row as the content of a command input and executed, if not specified n1, n2, it indicates that the contents of the entire file as a command input 
:! r command: the commands of command output into the current line 

register operation 
"nyy:??? save the contents of the current line and the next n rows into a register, wherein a letter, n is a number 
" nyw:? save the current line and the next n characters to register? In which a letter, n is a number? 
"NYL:?? Save the current line and the next n characters into a register, wherein a letter, n is a number? 
" P:? Remove the register? The contents and puts it at the cursor position. Here? It may be a letter, a number may be 
ndd: deletes the current line and the next line of text co-n, and delete the content into the register No. 1 deleted.

VI use
----------------------------------------------- ---------------------------------

First, insert text 
┌──┬───────── ───┐ 
│ │ command described │ 
├──┼────────────┤ 
│i │ insert text before the current character │ 
├──┼────────────┤ 
| I │ beginning of the line to insert text │ 
├──┼────────── ──┤ 
│a │ │ add text after the current character 
├──┼────────────┤ 
│A end of the line to add text │ │ 
├──┼──────── ────┤ 
│o │ │ insert a blank line after the current line 
├──┼────────────┤ 
│O │ inserted in front of a blank line of the current row │ 
├──┼── ──────────┤ 
│R manner │ rewritten input text in │ 
└──┴────────────┘ 
two, move the cursor 
┌─────┬── ─────────┐ 
│ │ command described │ 
├─────┼───────────┤ 
│j or the down arrow moves down one line │ │ 
├──── ─┼───────────┤ 
│k │ moved upward arrow or line │ 
├─────┼─────────── 
│h or left arrow left one character │ │ 
├─────┼───────────┤ 
│l or right arrow right one character │ │ 
├─────┼── ─────────┤ 
│w │ right word │ 
├─────┼───────────┤ 
│W │ right one space-separated word │ 
├─────┼──── ───────┤ 
│b left a word │ │ 
├─────┼───────────┤ 
│B │ left a space-separated words │ 
├── ───┼───────────┤ 
│0 moved to the first line │ │ 
│Ctrl page forward-F │ │ 
├─────┼───────── ──┤ 
│Ctrl page backward-B │ │ 
├─────┼───────────┤ 
│nG to the n-th row │ │ 
├─────┼─── ────────┤ 
│G to the last line │ │ 
└─────┴───────────┘ 
Third, replace text 
┌─────┬──── ──┐ 
│ │ command described │ 
├─────┼──────┤ 
│ $ │ end of the line │ 
├─────┼──────┤ 
beginning │ (│ to sentences │ 
────┼──────┤ 
│) │ sentence to the end of │ 
├─────┼──────┤ 
{│ │ beginning to paragraph │ 
├─────┼──────┤ 
│ │} to the end of the paragraph │ 
└─────┴──────┘ 

four, delete text 
┌─ ──┬───────────┐ 
│ │ command described │ 
├───┼───────────┤ 
│r replace a character │ │ 
├───┼─ ──────────┤ 
│c │ modify the text until you press the Esc health │ 
├───┼───────────┤ 
under │cw │ modify a word │ 
├── ─┼───────────┤ 
│cnw │ modify the next n words │ 
└───┴───────────┘ 
five text editor 
┌──┬ ──────────────────────┐ 
│ │ life Temple described │ 
├──┼───────────────── ─────┤ 
│yy │ line of text will be moved to the default buffer │ 
├──┼──────────────────────┤ 
│yn │ will Moved to a default word buffer │ 
├──┼──────────────────────┤ 
│ynw │ n words will move behind the default buffer │ 
├──┼──────────────────────┤ 
│p │ If the default buffer contains a line of text, if the current │ 
inserted behind a row │ │ the acoustic well default empty line buffer │ 
│ │ attached to this container row; if the default buffer contains │ 
│ │ plurality of words, these words are pasted to the right of the cursor. │ 
├──┼──────────────────────┤ 
│P │ If the default buffer contains a line of text, a positive current │ 
│ │ inserted before the row a blank line the inner wells default buffer │ 
│ │ attached to this container row; if the default buffer contains │ 
│ │ plurality of words, these words are pasted to the left of the cursor    

  │ 
└──┴─ ─────────────────────┘ 
six, save and exit 
┌───────────┬─────────── ────┐ 
│ │ command described │ 
├───────────┼───────────────┤ 
│zz save and exit │ │ 
├─── ────────┼───────────────┤ 
│: w filename write the file │ │ 
├───────────┼─── ────────────┤ 
│: W │ │ write file 
├───────────┼───────────────┤ 
│: │ the X-saving (current file if modified) and exit │ 
├───── ──────┼───────────────┤ 
│:! q │ not save the file, exit │ 
├───────────┼── ─────────────┤ 
│: q │ │ quit vi 

VI common technique

VI commands can be said to command Unix / Linux world's most commonly used to edit the file, but because of its numerous command set many people are not accustomed to using it, in fact, you only need to know basic commands, and then be flexibly applied, will find its advantage and will gradually prefer to use this method. This article aims to introduce VI of some of the most commonly used commands and advanced application skills. 

I. Basic command 

---- 1 Cursor commands 

k, j, h, l - the upper, lower, left and right cursor movement commands. Although you can use the four cursor keys to the right of the keyboard in Linux, but keep in mind that four command is very useful. It is these four keys placed his right hand on the keyboard basic position. 
nG-- jump command. n is the number of rows, so that the cursor jumps to the command immediately specified line. 
Ctrl + number row position of the cursor and the number of columns G-- report. 
w, b-- cursor skip forward or backward one word. 
---- 2. Editing commands 
i, a, r-- before the cursor is located at the rear and insert the character command (i = insert, a = append  , r = replace).
cw, dw-- change (replacement) / delete command word where the cursor at the (c = change, d = delete  ).
x, d $, dd-- delete a character, the cursor is located to remove all the end of the line and delete the entire command line. 
---- 3 Find command 
---- / string,? String-- back or find the appropriate command string from under the cursor forward. 
---- 4 Copy Copy command 
---- yy, p-- line copy to the clipboard or remove the contents of the clipboard command. 

Second, FAQs and application skills 

---- 1 In reading the contents of a new file / etc / passwd are removed username portion. 
VI file ---- 
----: R & lt / etc / passwd file in the opened file at the cursor reading / etc / passwd 
----:% G S /:.*// delete / etc / passwd a user name from the latter until all portions of the colon start end of the line. 
---- You can also read the contents of the file after the specified line number, for example, use the command ": 3r / etc / passwd" file from the new line 3 to start reading / etc all content / passwd is. 
---- We can also delete all the files in the blank lines and comment lines starting with # using the following method. 
#Cat squid.conf.default ---- | grep -v ^ $ | grep -v ^ # 

---- 2. Open a file to edit it after the user logged on to the file does not have write permissions, can not save, you need to do to modify into a temporary file. 
File VI ---- 
----: W / tmp / all save the changes. 1, which may be modified to save a part to a temporary file, for example, only the contents of between 20 and 59 rows to save file / tmp / 1, we can type the following command. 
Vi File ---- 
----: 20,59w / tmp / 1 

---- 3. Use VI to edit a file, but need to delete the contents of a large segment. 
---- First, use the Edit command "vi file" to open the file, and then move the cursor to the line to be deleted at the press Ctrl + G to display the line number, then press Ctrl + G at the end of the display line number at the end of the document. 
----: 23,1045d assume line number 2 and get 23 1045, during the full content of this deletion, can also be ma, mb command to be deleted is marked with the beginning and ending lines, and then use ": a, bd" command to delete. 

---- 4 In each line of the whole file or the beginning or end of the line a few lines plus some string. 
Vi File ---- 
----: 3, $ S / ^ / some String / First insert "some string" line of the file in the first line to the last line. 
----:% s / $ / some string / g Add "some string" end of the line in the entire file each line. 
----:% s / string1 / string2 / g throughout this document replace "string1" into "string2". 
----: Line 3 3,7s / string1 / string2 / replace only the file to line 7 in the "string1" into "string2". 
---- Note: wherein s is a substitute,% means that all rows, g denotes global. 

---- 5 2 edit files, a copy of a text file and paste it into another file. 
VI file1 file2 ---- 
---- YY file copy cursor row 1 
----: n switch to File 2 (n = next) 
line ---- P 2 file at the cursor paste the copied 
----: n switch back Document 1 

---- 6 Replace the path file. 
---- use the command ":% s # / usr / bin # / bin # g" can put all files in the path / usr / bin into / bin. Using a command ":% s // usr / bin // bin / g" to achieve, where "" is the escape character, indicating that the subsequent "character /" character is meaningful, not delimiters.

Taken http://www.cnblogs.com/88999660/articles/1581524.html

Reproduced in: https: //www.cnblogs.com/miaokm/p/3897788.html

Guess you like

Origin blog.csdn.net/weixin_34252090/article/details/94145185