Linuxvi/vim operation learning Notes

Source: Internet
Author: User
Vi (vim) is a commonly used editor on Linux. by default, vi (vim) is installed in many Linux distributions ). There are many vi (vim) commands, but if they are used flexibly, the efficiency will be greatly improved. Vi is short for "visualinterface" and vim is viIMproved (enhanced version vi ). In general system management and maintenance, vi is enough. if you want to use code to highlight it, you can

Vi (vim) is a commonly used editor on Linux. by default, vi (vim) is installed in many Linux distributions ). There are many vi (vim) commands, but if they are used flexibly, the efficiency will be greatly improved. Vi is short for "visualinterface" and vim is viIMproved (enhanced version vi ). In general system management and maintenance, vi is enough. if you want to use code to highlight it, you can use vim. The following describes how to use vi: including the basic introduction of vi, usage mode, opening, closing, saving, inserting text or creating a row, moving the cursor, deleting, restoring characters or rows, searching, etc, this is a more suitable tutorial for beginners to learn vi.

Vi has three modes: insert mode, command mode, and low-line mode.

● Insert mode: you can enter characters in this mode. press ESC to return to the command mode.

● Command mode: you can move the cursor or delete characters.

● Low-line mode: you can save files, exit vi, set vi, and search functions (low-line mode can also be seen as in command mode ).

1. Open, save, and close a file (used in vi command mode)

Vi filename // open the filename File

: W // Save the file

: W vpser.net // save it to the vpser.net file

: Q // exit the editor. if the file has been modified, run the following command:

: Q! // Exit the editor and do not save

: Wq // exit the editor and save the file

2. Insert text or line (in vi command mode, run the following command to enter the insert mode. press ESC to exit the insert mode)

A // add text to the right of the current cursor position

I // add text to the left of the current cursor position

A // add text at the end of the current row

I // add text at the beginning of the current line (the beginning of a non-null character)

O // create a new row above the current row

O // create a new row under the current row

R // replace (overwrite) the current cursor position and subsequent text

J // merge the row where the cursor is located and the next row as one line (still in command mode)

3. move the cursor (used in vi command mode)

1. use the up, down, and left direction keys

2. in command mode, h goes to the left, j goes down, k goes up, and l goes to the right.

Space key to the right, Backspace to the left, Enter to the next line first,-to move to the previous line first.

4. delete or restore characters or lines (used in vi command mode)

X // delete the current character

Nx // delete n characters starting from the cursor

Dd // delete the current row

Ndd // delete n rows including the current row down

U // undo the previous operation

U // undo all operations on the current row

5. search (used in vi command mode)

/Vpser // search for a vpser string under the cursor

? Vpser // search for a vpser string from the cursor

N // search down the previous search action

N // search for the previous search action

6. jump to a specified line (used in vi command mode)

N + // jump n rows down

N-// jump n rows up

NG // jump to the row whose row number is n

G // jump to the bottom of the file

7. set the row number (used in vi command mode)

: Set nu // display the row number

: Set nonu // undisplay the row number

8. copy and paste (used in vi command mode)

Yy // copy the current row to the cache, or use "ayy copy," a as the buffer, and a can be replaced with any letter from a to z, you can complete multiple replication tasks.

Nyy // Copy the n rows of the current row to the buffer. you can also use "anyy replication," a is the buffer, and a can also be replaced with any letter from a to z, you can complete multiple replication tasks.

Yw // Copy the characters starting from the cursor to the end of the word.

Nyw // Copy the n words starting from the cursor.

Y ^ // copy the content from the cursor to the beginning of the line. VPS detective

Y $ // copy the content from the cursor to the end of the row.

P // paste the content in the clipboard after the cursor. if the preceding custom buffer is used, we recommend that you use "ap" for pasting.

P // paste the content in the clipboard before the cursor. if you use the custom buffer above, we recommend that you use "aP for pasting.

IX. replacement (used in vi command mode)

: S/old/new // replace the old

: S/old/new/g // replace all the old in the row with new

: N, m s/old/new/g // replace all the old values from n to m with new

: % S/old/new/g // replace all old files in the current file with new

10. edit other files

: E otherfilename // edit the file named otherfilename.

11. modify the file format

: Set fileformat = unix // modify the file to unix format. for example, ^ M will appear in the text file under windows in linux.

Summary: Although there are many commands in the vi (vim) tutorial, as long as you work diligently and practice, I believe you will be very familiar with it soon, at the same time, it will bring higher efficiency to your work or study. If you do not know what mode you are in, press the Esc key twice to return to the command mode. a last note will be given: pay attention to case sensitivity !!!

1. about exit


: Wq! ---- Force save and exit
: Wq ---- save and quit
: X ----- works the same as: wq
ZZ ---- has the same role as: wq (note that Z is capitalized and not in command mode)
: Q ---- exit
: Q! --- Force exit

========================================================== ======

2. about mobile


H: move a character from the current row to the left.
J: move to the next row
K: move to the previous row.
L: move one character to the right of the current row
Ctrl + f: scroll one page forward
Ctrl + B: scroll back one page
: N: Move the cursor to line n.
: $ Move the cursor to the last line
0. move the cursor to the beginning of the row.
$ Position the cursor at the end of the row
G positions the cursor in the last line of this article, which is the same as: $.
H. move the cursor to the top of the screen.
M positions the cursor in the middle of the screen
L move the cursor to the bottom of the screen



========================================================== ====
3. search



/: Search for the following information in the file.
? : Search for the things to be searched in the file.
N: repeated search forward
N: repeated search



========================================================== =====
4. about replication


Yy: copy the current row of the cursor
Nyy: Copy the n-1 line from the current row to the current row.
: 1,100 co 200 will 1 ~ 100 of the content is copied to 200th rows.
: 100,102 co $ change 100 ~ Copy the contents of the first row to the last row.


========================================================== ======
5. about pasting



P: paste it to the next row of the current row.
P (large): paste it to the previous row of the current row



========================================================== ======
6. About Delete. cut



Dd deletes the current row
Ndd is similar to nyy
Dw deletes a word
Similar to ndd, ndw
X delete a character
Nx delete n characters
DG deletes all content from the current cursor to the end of the file.
D0 deletes all content from the current cursor to the beginning of the line
D $ delete all content from the current cursor to the end of the row
: 1, 100d delete 1 ~ 100
: 100d delete 100th rows
: 1,100 mo $ Change 1 ~ Move the content of row 3 to the last row.

========================================================== =====
7. Insert


I: Enter the insert mode before the characters in the current position
I: insert at the beginning of the current row
A: Enter the insert mode after the characters at the current position
A: insert at the end of the current row.
O: insert a new row under the current row.
O: insert a new row on the current row.



========================================================== =====
8. about revocation


U: undo the last change


========================================================== =====
9. about replacement



Regexp: the pattern to be matched.
Replacement: string to be replaced



: S/regexp/replacement ------------------------- replace the first style of the current row
: S/regexp/replacement/g ------------------------- replace all the matches in the current row
: % S/regexp/replacement/g ----------------------- replace all matching styles in the file



========================================================== =====


PS: another important command is the "." command, which is used to repeat the previous command.

In vi, how does one: undo the last operation ?, Repeat a group of edit operations multiple times ?.......

A) undo the previous edit operation. ====> U
B) repeat the previous edit operation. ====>.
C) restore the unedited operation. ======> Ctrl + R
D) repeat a group of edits multiple times. ====> "." Command can repeat the last edit action.

 Handout vi of laruence's Linux private House dishThe image is relatively large and automatically scaled. we recommend that you download the image and view it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.