Linux vi editor command Daquan

  
        

The most powerful editor ——vi

vi is a screen editor for all UNIX systems that provides a window device through which files can be edited. Of course, people who know a little about UNIX systems,

think more or less vi is super difficult, but vi is the most basic editor, so I hope readers can learn it well, and later The UNIX world will be unimpeded,

easy, because several other text processors are not standard on UNIX. Maybe someone else's Linux machine doesn't have joe or pico installed, if you don't vi, you probably won't be embarrassed.

Basic Concepts of vi

Basically, vi can be divided into three operational states: Command mode, Insert mode, and Last line mode. ),

The functions of each mode are as follows:

1. Comand mode: Controls the movement of the screen cursor, the deletion of characters or cursors, moves a certain section and enters the Insert mode, or Go to the Last line mode.

2. Insert mode: Only in the Insert mode, you can do text data input, press Esc, etc. to return to the Comand mode.

3. Last line mode: will save the file or leave the editor, you can also set the editing environment, such as looking for strings, listing line numbers, and so on.

However, you can simplify vi into two modes, that is, calculate the Last line mode into Command mode and divide vi into Command and Insert mode.

Basic operation of vi

Enter vi

After entering vi and file name in the system prompt symbol, you can enter vi full screen editing screen:

$ vi testfile

One thing to note is that after you enter vi, you are in "Command mode", you have to switch to Insert mode to enter text.

Users who use 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 calling, and mad at yourself, so after entering vi, don't tamper with it,

After the Insert, say it. Switch to the Insert mode to edit the file. In the Command mode, press ‘i’,‘a’ or ‘o’ three keys to enter the Insert mode.

At this point you can start typing text.

i: Insert, insert the entered text from where the cursor is currently located.

a: Increase, the current word where the cursor is located begins to input text.

o: Insert a new line and enter text from the beginning of the line.

?Insert switch →Command mode, press Esc key

You are currently in Insert mode, you can only type. If you find that you have made a typo, you want to use the cursor keys to move back, delete the word,

press ESC to convert back to Command mode, and then delete the text.

?Leave vi and save files

In Command mode, type "Last line mode" by colon "

:w filename (input) “w filename”, save the article to the specified file name filename)

:wq (enter “wq”, because the file name testfile has been specified at the time of entry, so it will be written to testfile and leave vi)

:q! (Enter “q!”, force to leave and discard the edited file)

Command mode function key list

When introducing the command mode command After the instruction, add the function key of the “usual” word to indicate the more commonly used vi command,

Please read and remember.

(1) I, a, o switch into Insert mode. [Super Common]

(2) Move the cursor

vi can use the cursor keys on the keyboard to move up and down, left and right, but the regular vi is in lowercase English letters

h, j, k, l, respectively control the cursor left, down, up, right shift a grid.

Press Ctrl+B: the screen moves back one page. [Common]

Press Ctrl+F: the screen moves forward one page. [Common]

Press Ctrl+U: the screen moves back half a page.

Press Ctrl+D: the screen moves forward by half a page.

Press 0 (number zero): to move the beginning of the article. [Common]

Press G: to move to the end of the article. [Common]

Press w: to jump to the beginning of the next word. [Common]

Press e: to jump to the end of the next word.

Press b: the cursor returns to the beginning of the previous word.

Press $: to move to the end of the line where the cursor is located. [Common]

Press ^: to move to the first non-blank character on the line.

Press 0: to move to the beginning of the line. [Common]

Press #: to move to the #th position of the line, for example: 51, 121. [Common]

(3) Delete text

x: Each time you press to delete the next character at the cursor position. [Ultra-Frequency]

#x: For example, the 6x table deletes the last 6 characters of the cursor position. [Common]

X: X of a large character, each time you press to delete the previous character at the position of the cursor.

#X: For example, the 20X table deletes the first 20 characters of the cursor position.

dd: Delete the line where the cursor is. [Ultra-Frequency]

#dd: For example, the 6dd table deletes the text from the line where the cursor is located, down 6 lines. [Common]

(4) Copy

yw: Copy the character from the cursor to the end of the character into the buffer.

(Thinking about the opposite of #x, #X)

p: Paste the characters in the buffer to the cursor position (instructions ‘yw’ and ‘p must match use).

yy: Copy the line where the cursor is. [Ultra-Frequency]

p: Copy a single line to where you want to paste. (Instructions & lsquo;yy’ and ‘p’must be used together)

#yy: For example: 6yy means copying the text from the line where the cursor is located to the next 6 lines. [Common]

p: Copy multiple lines to where you want to paste. (Instructions & lsquo; #yy & rsquo; & & lsquo; p & rsquo; must be used together)

“ ayy: put the copy line into the buffer a, vi provides the buffer function, the commonly used data can exist in the buffer

“ ap: will be placed in the buffer a data paste.

“b3yy: Store three rows of data in buffer b.

“b3p: Paste the data of buffer b

(5) Replace

r: Replace the character where the cursor is: [Common]

R: Replace characters until you press Esc.

(6) Restore (undo) previous instruction

u: If you mishandle an instruction, you can press u immediately to return to the previous operation. [Ultra-Frequency]

.: . The last instruction can be executed repeatedly.

(7) Change

cw: Change the word where the cursor is located to the end of $.

c#w: For example, c3w represents a change of 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 the 15th line of the article. [Common]

Introduction to instructions in Last line mode

Before you use the Last line mode, please remember to press the Esc key to confirm that you are in Command mode, then press the colon “ :&&quoquo; or“/” or “?”

One of the three keys enters the Last line mode.

1. List the line number

set nu: After entering “set nu”, the line number will be listed before each line of the article.

2. Jump to a line of the article

#:The pound sign represents a number, enter the number before the Last line mode prompt “":”, then press Enter to jump to The line,

For example: 15[Enter] will jump to the 15th line of the article. [Common]

3. Find the string

/Keyword: Press /, then enter the word you want to find, if the first time you find the keyword is not as much as you can , you can always press n to find the keyword you want.

?Keyword: Press ?, then enter the word you want to find. If the first time you find the keyword is not what you want, you can press n to find the keyword you want. .

4. Replace the string

1, $s/string/replae/g: Enter "1", $s/string/replace/g” in the last line mode The string string is replaced by the replace string.

Copyright © Windows knowledge All Rights Reserved