VI (VIM) keyboard diagram and its basic commands

Source: Internet
Author: User

Enter VI

VI filename Opens or creates a new file and places the cursor at the beginning of the first

VI +n filename Opens the file and places the cursor at the beginning of nth

VI + filename Opens the file and places the cursor at the beginning of the last line

Vi-r filename The last time you were editing with VI, a system crash occurred, restoring the file

VI File1....filen open multiple files and edit them in sequence

VI Mode of operation

Command mode: Controls the movement of the screen cursor, the deletion of characters, words, or lines, moving and copying a section and entering insert mode, or to last line mode.

Insert mode: Only under Insert mode can you make text input and press "esc" to return to command line mode.

Bottom line mode: Save or Exit VI, or you can set the editing environment, such as searching for strings, listing line numbers, and so on.

Command line mode move cursor

K, J, H, L functions are equal to the upper, lower, left and RIGHT arrow keys respectively

Ctrl+b Move up one page in the file (equivalent to the PAGEUP key)

Ctrl+f Move down one page in the file (equivalent to the PageDown key)

Ctrl+u screen move back half page

Ctrl+d screen move forward half page

H moves the cursor to the highest upstream of the screen (highest)

NH moves the cursor to the nth row of the screen (e.g. 2H: Moves the cursor to line 2nd of the screen)

M moves the cursor to the middle of the screen (middle)

L move the cursor to the most downstream of the screen (Lowest)

NL moves the cursor to the penultimate line of the screen (for example, 3L: Moves the cursor to the bottom 3rd line of the screen)

W Move the cursor right within the specified line to the beginning of the next word

E Move the cursor right within the specified line to the end of a word

b Move the cursor left within the specified line to the beginning of the previous word

0 number 0, move left cursor to the beginning of the bank

G cursor moves to the end of the article

NG cursor moves to the nth line of the article (e.g. 8G: Moves to line 8th of the article)

$ right Move cursor to the end of the bank

^ Move the cursor to the first non-null character of the bank

Replace and delete

RC replaces the current character pointed to by the cursor with C

The NRC replaces the first n characters pointed to by the cursor with C (for example, 5RC: Replace the first 5 characters that the cursor points with C)

X Delete a character after the cursor position

NX removes the n characters after the cursor position (for example, 3x: Delete 3 characters after the cursor position)

x uppercase X, deleting one character before the cursor position

NX removes the n characters preceding the cursor position (for example, 3X: Delete the 3 characters before the cursor position)

DD deletes the line of the cursor and removes the gap

NDD removes n-line content from the line where the cursor is located and removes voids (such as 3DD: Delete 3 rows and remove voids)

Copy and paste

Content that is removed from the body (such as characters, words, or lines) is not really lost, but is cut and copied into a memory buffer. The user can paste it into the body at the specified location.

P lowercase Letter p, paste the contents of the buffer behind the cursor

P Capital Letter p, paste the contents of the buffer to the front of the cursor

If the contents of the buffer are characters or words, paste directly before or after the cursor, or if the contents of the buffer are the entire line body, paste on the previous line or row of the current cursor.

Sometimes you need to copy a piece of text to a new location while preserving the contents of the original location. In this case, you should first copy (rather than cut) the specified content to the memory buffer. The command to complete this operation is:

YY copy current line to memory buffer

NYY copy n rows of content into memory buffers (e.g. 5yy: Copy 5 rows of content into memory buffers)

Search string

/STR1 Forward Search string str1

N continue searching to find out where the STR1 string will appear next

? str2 Reverse Search string str2

Undo and Redo

U undo the result of the previous command

. Repeat the last command to modify the body

Text selection

V Character check Command

V-Line Check command

Insert mode enters insert mode

I enter the body on the left of the cursor

A enter the text to the right of the cursor

o Add a new row to the next line in the row where the cursor is located

O add a new row to the previous line on the row where the cursor is located

I enter body at the beginning of the line where the cursor is located

A enter body at the end of the line where the cursor is located

Exit Insert Mode

ESC key or key combination ctrl+[

Bottom row mode

In the bottom-row mode of VI, complex commands can be used.

Exit command

In command mode, you can use the ZZ command to exit the VI editing program, which saves changes to the body and overwrites the original file. If you only need to exit the editing program and not save the edited content, you can use the following command:

: Q Quit without modification

: q! Discard all changes and exit the editing program

Line number and File save

: N Moves the cursor to the nth row

: Set NU Displays line number

: Set Nonu suppress line number display

In the bottom row mode, you can specify the line number range for the command operation. The value is used to specify the absolute line number; Represents the line number of the row where the cursor is located; the character "$" represents the line number of the last line of the body; a simple expression, such as ". +5", represents the 5th row down the current line. For example:

:. +5 move the cursor to line 5th after the current line

: $ moves the cursor to the last line of the body

In the bottom row mode, the body is allowed to be read from the file, or the body is written to the file. For example:

: W writes the edited content to the original file to save the edited intermediate result

: Wq writes the edited content to the original file and exits the editing program (equivalent to the ZZ command)

: W file writes the edited content to the file, keeping the contents of the original file intact

: A,BW file writes the contents of line A to line B to the file (e.g., 1,.W file writes the 1th line to the current line to the file)

: R file reads the contents of the file and inserts the following line of the current cursor

: F file renames the current file

String search

:/str/forward Search, moves the cursor to the next line containing the string str

:? str? Reverse search, moves the cursor to the previous row containing the string str

Body replacement

: s/str1/str2/replaces the first occurrence of a string in a line with a string str2 str1

: S/str1/str2/g replaces all occurrences of strings in a line with a string str2 str1

:., $ s/str1/str2/g Replace all occurrences of the current line to the end of the string with the string str2 str1

: 1,$ s/str1/str2/g replaces all occurrences of strings in the body with a string str2 str1

: g/str1/s//str2/g function Ibid.

As you can see from the Replace command above, G is placed at the end of the command to replace each occurrence of the search string, without G, to replace only the first occurrence of the search string, and g at the beginning of the command to replace all rows in the body that contain the search string.

Delete Body

:D Delete the cursor in the row

: 3d Delete Line 3rd

:., $d Delete the current line to the end of the body

:/str1/,/str2/d Delete all rows from string str1 to str2

Recover files

VI when editing a file, a separate temporary file is generated, the name of the file is usually. Start with a. SWP end. VI in the normal exit, the file is deleted, if you unexpectedly quit, and did not save the latest changes to the file, you can use the recovery command, you can also start VI with the-r option.

: Recover Recover files

Option settings

To control different editing functions, VI offers many internal options. Use the: Set command to set options. The basic syntax is:

: Set option Settings options

Common feature options include the following:

Autoindent Set this option, the body is automatically indented

ignorecase Set this option, the difference between uppercase and lowercase letters in the rule expression is ignored

Number sets this option to display the body line numbers

Ruler Set this option, the location of the row and column of the cursor is displayed at the bottom of the screen

TabStop sets the number of spaces to skip by pressing the Tab key. Example: Set tabstop=n,n default value is 8

Mk Saves the option in the. exrc file in the current directory

Shell switch

When editing the body, use the shell Toggle command provided in the VI bottom row mode to execute the Linux command without exiting VI

:! Command executes the shell command and then returns to VI

Also, in command mode, type K to command VI to find the manual page of the word where the cursor is located, equivalent to running the man command.

VI (VIM) keyboard diagram and its basic commands

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.