How to Use the vi (vim) editor on Linux

Source: Internet
Author: User
Tags vps
How to Use the VI (Vim) editor on Linux

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 "Visual Interface", and VIM is VI improved (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, search, and other 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 !!!

If you have any questions, please leave a message in this article or contact the VPs forum.

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.