Vi (Vim) keyboard chart and its basic commands

Source: Internet
Author: User

Vi (Vim) keyboard chart and its basic commands

Vi (Vim) keyboard chart and its basic commands:

 

Go to vi

Vi filename open or create a file and place the cursor at the beginning of the first line

Vi + n filename open the file and place the cursor at the beginning of line n

Vi + filename open the file and place the cursor at the beginning of the last line

Vi-r filename: a system crash occurred when vi was being edited last time.

Vi file1. .. filen open multiple files and edit them in sequence

Vi Working Mode

Command mode: controls the movement of the screen cursor, the deletion of characters, words, or lines, and moves to a certain segment and enters the Insert mode, or to the last line mode.

Insert mode: You can enter text only in Insert mode. Press ESC to return to the command line mode.

Last line mode: Save or exit the vi file, or set the editing environment, such as searching strings and listing row numbers.

 

Move the cursor in Command Line Mode

K, j, h, and l functions are equivalent to the upper, lower, left, and right arrow keys.

Ctrl + B Move up a page in the file (equivalent to the PageUp key)

Ctrl + f move down a page in the file (equivalent to the PageDown key)

Ctrl + u move back half page

Ctrl + d move the screen half page forward

H move the cursor to the top of the screen (Highest)

NH move the cursor to line n of the screen (for example, 2 H: move the cursor to line 2nd of the screen)

M move the cursor to the Middle of the screen (Middle)

L move the cursor down the screen (Lowest)

NL moves the cursor to the last n rows of the screen (for example, 3L: move the cursor to the last 3rd rows of the screen)

W shifts the cursor right in the specified row to the beginning of the next word.

E move the cursor right in the specified row to the end of a word

B. move the cursor left in the specified row to the beginning of the previous word.

0: 0. move the cursor left to the beginning of the line.

Move G cursor to the end of the article

NG cursor moves to the nth line of the article (for example, 8G: moves to the nth line of the article)

$ Move the cursor right to the end of the row

^ Move the cursor to the first non-empty character in the row

Replacement and Deletion

Rc uses c to replace the current character pointed to by the cursor

Nrc replaces the first n characters pointed to by the cursor with c (for example, 5rc: replaces the first 5 Characters pointed to by the cursor with c)

X deletes a character behind the cursor position.

Nx deletes n characters after the cursor position (for example, 3x: 3 characters after the cursor position)

X in uppercase. delete a character before the cursor position.

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

Dd deletes the row where the cursor is located and removes gaps

Ndd deletes n rows of content from the row where the cursor is located and removes gaps (for example, 3dd: deletes 3 rows of content and removes gaps)

Copy and paste

The content (such as characters, words, or rows) deleted from the body is not really lost, but is cut and copied to a memory buffer. You can paste it to a specified position in the body.

P: lowercase letter p. paste the content of the buffer to the end of the cursor.

P capital letter P, paste the content of the buffer to the front of the cursor

If the buffer content is a character or word, it is directly pasted before or after the cursor; if the buffer content is the entire line of body, it is pasted on the previous line or next line of the row where the current cursor is located.

Sometimes you need to copy a piece of text to a new location, while retaining the content of the original location. In this case, the specified content should be copied (instead of cut) to the memory buffer. The command to complete this operation is:

Yy copies the current row to the memory buffer

Nyy copies n rows of content to the memory buffer (for example, 5yy: Copies 5 rows of content to the memory buffer)

 

 

SEARCH strings

/Str1 forward search string str1

N continue searching to find the position where the str1 string will appear next time

? Str2 reverse search string str2

Undo and repeat

U undo the result of the previous command

. Repeat the last command to modify the body.

Text Selection

V character selection command

Command selected by line V

Insert mode to insert mode

I enter the body on the left of the cursor

A. Enter the text on the right of the cursor.

O Add a new line to the next row of the cursor

O Add a new line to the row where the cursor is located

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

A. Enter the body at the end of the row where the cursor is located.

Exit insert mode

ESC key or combination key Ctrl + [

Baseline Mode

Complex commands can be used in the bottom-line mode of vi.

Exit command

In command mode, you can use the ZZ command to exit the vi editing program. This command saves the changes made to the body and overwrites the original file. To exit the editing program without saving the edited content, run the following command:

: Q exit without modification

: Q! Discard all modifications and exit the editing program

Row number and file storage

: N move the cursor to line n

: Set nu display row number

: Set nonu cancel row number display

 

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

:. + 5 move the cursor to the second row after the current row

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

 

In baseline mode, you can read or write the body from a file. For example:

: W writes the edited content to the original file to save the intermediate editing 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 content of the original file unchanged.

: A, bw file writes the content from row a to row B to the file (for example, 1,. w file writes row 1st to the current row to the file)

: R file reads the content of the file and inserts the content behind the row where the current cursor is located.

: F file: Rename the current file to file

String search

:/Str/forward search, move the cursor to the next row containing the string str

:? Str? Reverse search: move the cursor to the previous row containing the string str

Body replacement

: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line

: S/str1/str2/g replace all the str1 strings in the row with str2

:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2

: 1, $ s/str1/str2/g replace str1 with string str2

: G/str1/s // str2/g functions are the same as above

From the above replacement command, we can see that g is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only Replace the first appearance of the search string; g is placed at the beginning of the command to replace all rows that contain search strings in the body.

Delete body

: D. Delete the row where the cursor is located.

: 3d Delete row 3rd

:., $ D Delete the current row to the end of the body

:/Str1/,/str2/d delete all rows from string str1 to str2

Restore Files

Vi will generate another temporary file when editing a file. The file name usually starts with. And ends with. swp. When vi Exits normally, the file is deleted. If it unexpectedly exits without saving the latest changes to the file, you can use the restore command, you can also use the-r option when starting vi.

: Recover recovery file

Option settings

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

: Set option

Common features include:

Autoindent: sets this option, and the body is automatically indented.

If this option is set for ignorecase, the differences between upper and lower case letters in the Rule expression are ignored.

Number.

Ruler sets this option to display the position of the row and column of the cursor at the bottom of the screen.

Tabstop sets the number of spaces skipped by pressing the Tab key. For example, set tabstop = n. The default value of n is 8.

Mk saves the options in the. exrc file of the current directory.

Shell Switch

When editing the body, use the shell switch Command provided in the bottom-line vi Mode to execute the Linux Command without exiting vi.

:! After executing the shell command, return to vi

In command mode, Enter K to run vi to find the manual page of the word where the cursor is located, which is equivalent to running man command.

-------------------------------------- Split line --------------------------------------

Build VIM into a simple and practical IDE

Vim Learning Guide

Quick learn Vi Editor

Powerful Vim Editor

Build a Vim Development Environment on CentOS 6.2

Install the highlighted Vim editing tool in CentOS 5.4

Vim tips: C language settings

Set the Vim row number in Ubuntu

Vim editor basic tutorial

-------------------------------------- Split line --------------------------------------

 

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.