[Reprint] vi usage tips

Source: Internet
Author: User

Access the VI command

 

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 +/pattern filename: open the file and place the cursor at the first string matching pattern

VI-r filename: a system crash occurred when VI was being edited last time, restoring filename

VI filename .... Filename: open multiple files and edit them in sequence.

Move cursor command

H: move the cursor one character to the left

L: move the cursor one character to the right

Space: move the cursor one character to the right

Backspace: move the cursor one character to the left

K or Ctrl + P: move the cursor up a row

J or Ctrl + N: move the cursor down a row

Enter: move the cursor down a row

W or W: move one word to the beginning of the word to the right of the cursor.

B or B: move one word left to the beginning of the word

E or E: move one word to the end of the word to the right of the cursor.

): Move the cursor to the end of the sentence.

(: Move the cursor to the beginning of the sentence.

}: Move the cursor to the beginning of the paragraph

{: Move the cursor to the end of the paragraph

NG: move the cursor to the beginning of line N

N +: move the cursor down n rows

N-: Move n rows above the cursor

N $: move the cursor to the end of line N

H: move the cursor to the top line of the screen

M: move the cursor to the middle line of the screen

L: move the cursor to the last line of the screen

0: (Note the number is zero) move the cursor to the beginning of the current row

$: Move the cursor to the end of the current line.

CTRL + u: half screen at the beginning of the file

CTRL + D: Flip the half screen to the end of the file

CTRL + F: Flip the screen to the end of the file

CTRL + B; open a screen at the beginning of the file

NZ: Rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.

Insert text commands

I: Before the cursor

I: at the beginning of the current row

A: After the cursor

A: At the end of the current row

O: open a new row under the current row

O: open a new row above the current row.

R: Replace the current character

R: Replace the current and subsequent characters until you Press ESC.

S: Starting from the current cursor position, replace the specified number of characters with the input text

S: delete a specified number of rows and replace them with the input text

NCW or NCW: modify a specified number of characters

NCC: specifies the number of lines to delete.

Ndw or ndw: n-1 characters starting at and following the cursor

Do: Delete to the beginning of a row

D $: Delete to the end of the row

NDD: Delete the current row and the next n-1 row

X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.

CTRL + u: Delete the text search and replacement commands entered in the input mode

/Pattern: Search for pattern from the beginning of the cursor to the end of the file

? Pattern: searches for the first object from the cursor

Pattern N: Repeat the previous search command in the Same Direction

N: Repeat the previous search command in the reverse direction.

: S/P1/P2/g replace all p1 in the current row with p2

: N1, N2s/P1/P2/g replace all P1 from line N1 to line N2 with p2

: G/P1/S // P2/g replace all p1 in the file with p2

ALL: lists all options.

Term: Set the terminal type

Ignorance: Case Insensitive in search

List: displays the stop table (CTRL + I) and end mark ($)

Number: displays the row number.

Report: displays the number of changes made by line-oriented commands.

Terse: displays brief warning information

Warn: No write information is displayed if the current file is not saved when it is transferred to another file.

Nomagic: allows the use of the special character nowrapscan without "/" before in search mode: Prohibit VI from searching to the two ends of the file and start from the other end

Mesg: Allows VI to display the last line of information that other users write to their terminal using write.

: N1, N2 Co N3 copy content from line N1 to line N2 to line N3

: N1, N2 M N3 move the content from line N1 to line N2 to line N3

: N1, N2 D Delete content between N1 row and N2 row

: W: Save the current file

: E filename open file filename for editing

: X Save the current file and exit

: Q: Exit vi

: Q! Do not save the file and exit vi

:! Command to execute shell command

: N1, N2 W! Command: The content from line N1 to line N2 in the file is used as the command input and executed. If N1 and N2 are not specified, the entire file content is used as the command input.

: R! Command: place the output result of the command to the current line.

Register operation

"? Nyy: Save the content of the current row and Its n rows to the register? , Where? It is a letter, and N is a number.

"? Nyw: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.

"? Nyl: Save the current row and Its n characters to the register? , Where? It is a letter, and N is a number.

"? P: retrieve the register? And place it at the cursor position. Here? It can be a letter or a number NDD: delete n lines of text in the current row and its bottom, and put the deleted content in the delete register 1.

SET command

Show row number: set number

Set the file to read-only: Set readonly

Make VI in ": N" and ":!" The file is automatically saved before the command: Set autowrite

Display the mode in which the user is in: Set showmode

: Set noshowmode: disable this display.

There are many other VI environment variables. Understanding them will make it easier for users to use VI. You can use the online help of VI to find the detailed functions of corresponding environment variables.

VI skills

1) Use the specified shortcut key, such as Ctrl + J input # annotator
At the ":" prompt, run the map command to define the shortcut key, press Ctrl + V at the same time, then press Ctrl + J, enter a space, and then enter the command to be defined, press the "I" key and enter "#", and then enter the "ESC" key.: Map ^ B I # <ESC>

2) define input shortcuts, such as input Max will automatically display as max@zhangjianfeng.com
At the ":" prompt, use the command AB,: AB Max max@zhangjianfeng.com

VI ~ /. Vimrc # The file corresponding to the shortcut key. If you copy the file to another host, you can also use the defined shortcut key.

3). encrypt the file
In the command line status: Run Command X and enter the password. PS: Generally, the script cannot be automatically executed after the password is added.

++ Search for a word in VI to highlight it. It looks uncomfortable. How can I remove it?

In the command mode of VI, enter nohlsearch. You can also ~ /. The above statements written in vimrc will be highlighted:
Set hlsearch
The following statement is not highlighted:
Set nohlsearch

 

Other VI skills:

1. Exchange two characters in XP;
Change DDP between the upper and lower lines;
Merge the upper and lower rows J;
Copy from current position to end of line y $
If you want to paste it to another place, you can just
Delete all rows of DG
Delete from current position to end of line D $

2. x
Encrypted after saving
: X
Then the system will prompt you to enter the password
Enter the password and save the disk and exit.

Use
VI-x filename
The system will prompt you to enter the password.
Example: Taking HP-UX as an Example
A. Encryption
VI File
: X
(Are you sure? (Y/n) [N] :) Answer Y
Enter the password, press enter, and save it. The encryption is successful.
B. decryption
VI-C file or VI-X file
Enter the password after key: To enter the file
: X
(Are you sure? (Y/n) [N] :) Answer Y
Press enter and use WQ or WQ again! Save, and the file is successfully decrypted.
By the way, the encrypted script cannot be interpreted and executed!
C.: X (uppcase X)
Key :......
Use WQ later! Storage disk, cannot use: X, otherwise the encryption fails
When opening:
VI-X file
Key :.....

3. In the command state, nyy indicates copying the content of the next n lines starting from the cursor line, P indicates paste, and pasted to the cursor.

4. Replace a single character with R;
Overwrite multiple characters with R;
Replace one character with S;
Replace the whole line with S;
: % S/old_word/new_word/g;
This command replaces a specific string in the entire file;

5. Jump H to the first line.
M jumps to the middle row.
L jump to the last line
Move the cursor to line N and press mk
Move the cursor to Row M and press "ay 'K
Store row n to m in register a, and so on. B, c ........ Registers, etc.
To paste it somewhere, move the cursor directly to somewhere and press 'ap

6. delete commands
D l Delete the current character (same as the X command)
D 0: Delete to the starting position of a row
D ^ Delete to the first character position of a line (excluding spaces or ta B characters)
D w: delete it to the end of a word
D 3 W Delete to the end of the third word
D B. Delete it to the start position of a word.
Delete d w to the end of a word with a space as the Separator
D B: delete it to the start position of a word with a space as the separator.
D 7 B is deleted to the start position of the first seven words with spaces as separators
D) Delete to the end of a statement
D 4) Delete to the end of the fourth statement
D (delete to the start position of a statement)
D} Delete to the end of a paragraph
D {Delete to the beginning of a paragraph
D 7 {Delete the first 7th paragraph positions before the start position of the current paragraph
D. Delete the current row.
D/t e x T Delete the position where the specified word "t e x T" appears from the text until the next one appears (but does not include this word) content
D FC deletes the position where the character "C" appears from the text until the next character appears (including
Between characters)
D TC deletes the content from the current row until the next character "C" appears
D. Delete it to the end of a row.
D $ Delete to the end of a row
D. Delete the contents of the five rows starting from the current row.
D l Delete the content until the last line on the screen
D H Delete the content until the first line on the screen
D G deletes the content until the end of the work Cache
D 1g content deleted until the beginning of the work Cache

7. Modify operations
C l change the current character
C w to the end of a word
C 3 W to the end of the third word
C B is changed to the start position of a word.
C w is changed to the end position of a word with a space as the Separator
C B is changed to the start position of a word with spaces as the separator.
C 7 B is changed to the start position of the first seven words with spaces as separators
C 0: change to the end of a row
C) modify to the end of a statement
C 4) modify to the end of the fourth statement
C (modify to the starting position of a statement)
C} modify to the end of a paragraph
C {modify to the start position of a paragraph
C 7 {change to the first 7th paragraph positions before the start position of the current paragraph
C TC modifies the content between the current row and the position where the next character C appears
C. modify it to the end of a row.
C. modify the current row
5 C: modify the content of the 5 rows starting from the current row
Line breaks
: Set wrapmargin = 4

: Set number plus row number!

: G/$/S // ABC/g
: G/^/S // DEF/g
Add a string at the beginning and end of each line of text.

8. Replacement Operation
S replaces the current character with one or more characters
S replaces the current row with one or more characters
5 s Replace the 5 characters starting from the current character with one or more characters
Note: VI replacement rules:
: G/S1/S/s2/S3/g
The first G indicates that each row including S1 is replaced, and the second G indicates that all S2 of each row including S1 are replaced by S3.
S indicates replacement. S2 indicates the string to be replaced. It can be the same as S1 (use // If the string is the same), and S3 indicates replacement string.

9. Mobile
FX
Move to the right to x-> FX
Move to the left to X-> FX
Move to the right to x before-> TX
Move to the left to X-> TX
(Note: X indicates the characters in the preceding four commands)
;-> Semicolon, used together with F and t, repeat once
,-> Comma, used together with F and t, and repeated in the opposite direction

10,
Mark text
Mchar: Mark the position of the current cursor with a letter char
'Char moves to the position marked by char
'Char moves to the beginning of the row where the char mark is located
"Move to the last position of the current row (after the cursor moves)-a double quotation mark
''Move to the beginning of the row where the first row is located (after the cursor moves)-two single quotes
CTRL + F flip down, CTRL + B flip up (useful for telnet)
Shift + 4 to the end of the row, Shift + 6 to the beginning of the row

11. append to the buffer zone
For a buffer with content already exists, you can append the content following it, for example:
"Ad4w deletes the last four words of the cursor and attaches them to buffer
"Ay) sample the content from the cursor to the end of the file and append it to buffer
"K3yy samples the content of three rows starting from the row where the cursor is located and attaches it to the buffer K

Note: In the command for appending a buffer, the original buffer name must be in uppercase to append the content. Otherwise, the content of the original buffer is overwritten. Append a line at the end of the original buffer.

12. In command line mode, you can first determine the command range.
. Indicates the current row. A number indicates the row number. For example, 1 indicates the first row and 2 indicates the second row. $ indicates the last row.
Range separated
Run the following command:
D Indicates deletion, y indicates copying ...... Everyone knows
Last sentence, % indicates the full text
: % D
Yes to delete full text

13. How many times did I restore the application? : Em52:

14.: N, N1 d
N and N1 are both rows, and D is to delete these rows. This indicates the current row.
: N
N is the number of rows and is directly transferred to N rows.
: R file name
Add the file to the current file
: N, N1 W file name
Write lines from N to N1 into another file

15. Search:
Use * on a word to search down the word, # It is up
Use /// <Hello/> to search for "hello", and no hello_world will be found (it is also useful to use this replacement)

Replace:
If there is a file, the content is:
AA
Bb
CC
Dd
Use: % S/. */printf ("& is: % d // n ",&)/
The file content is changed:
Printf ("AA is: % d/N", AA)
Printf ("BB is: % d/N", BB)
Printf ("cc is: % d/N", CC)
Printf ("DD is: % d/N", DD)
& For the found content
Use :~ For the previous replacement

Edit with system commands:
If there is a file, the content is:
333
222
334
444
111
553
554
233
Run the command: %! The content of the sort file is:
111
222
233
333
334
444
553
554
Using this method, you can use many system commands for editing.

Other special usage:
Q: What is the purpose? You can edit commands you have used.
Using k in a call function (such as printf) in the program, you can call up the help of printf directly.

These are frequently used commands that I think are useful. Please advise.

16,: N1, n2w filename (write the content between the row N1-N2 into the new file filename)
:. = (Displays the row number of the row where the cursor is located)
U (restore the last operation)
:! (Shell-command to execute shell commands)
: N (define the cursor directly to N rows)
: Line1, line2mline3: Move the content between line1-line2 to line3, equivalent to the word clipboard
: Line1, line2tline3: The content between the line1-line2 copyline3, equivalent to the word Paste

17. Because VI is built on ex, when you type:, it comes to the ex command status.
: AB string strings
For example, "AB USA United States of America ",
When you insert USA into a file
United States of America came out.
: Map keys new_seq
Define your current Keyboard Command
: Set [all]
VI or ex editing status
Show each line: Set nu

18,
3. Reverse the file content: G/^/M0/--> how can this M0 be parsed?

Valentine explanation:
XP and DDP should be understood as deleting a character (X) and deleting a row (dd), and then P (paste), so there is no need to remember the new command.
Reverse the file content (reverse by line)
: G/^/M0/should be: G/^/M0 no final/
M-> move
0-> line No 1
Imagine what will happen when each row is placed at the beginning of the file starting with row 3 ???

# Environment Configuration

Set nocp
This command allows Vim to work in incompatible mode. If the ": Set CP" command is used to enable the compatibility mode switch, VIM will try its best to imitate the VI operation mode. we recommend that you use the incompatible mode of vim. write "set nocp" in the first line of vimrc"

Set Ru
This command is used to open the vim status bar ruler to instantly display the row number, column number, and percentage of the entire file where the current cursor is located in the file.

Set HLS
Highlight the found text during search

Set is
Search starts when the text to be retrieved is not fully entered.

Syntax on
Enable keyword coloring

Set encoding = cp936
Set the current character encoding to simplified Chinese. There is also an encoding that can automatically determine the file,
Automatically select the appropriate. vimrc code. If you are interested, you can use it in your configuration file:
"Encoding settings
If has ("multi_byte ")
"Set fileencoding priority
If getfsize (expand ("%")> 0
Set fileencodings = ucs-bom, UTF-8, cp936, big5, EUC-JP, EUC-KR, Latin1
Else
Set fileencodings = cp936, big5, EUC-JP, EUC-KR, Latin1
Endif

"CJK environment detection and corresponding setting
If V: lang = ~ "^ Zh_cn"
"Use cp936 to support GBK, EUC-Cn = gb2312
Set encoding = cp936
Set termencoding = cp936
Set fileencoding = cp936
Elseif V: lang = ~ "^ Zh_tw"
"Cp950, big5 or EUC-TW
"Are they equal to each other?
Set encoding = big5
Set termencoding = big5
Set fileencoding = big5
Elseif V: lang = ~ "^ Ko"
"Copied from someone's dotfile, untested
Set encoding = EUC-KR
Set termencoding = EUC-KR
Set fileencoding = EUC-KR
Elseif V: lang = ~ "^ Ja_jp"
"Copied from someone's dotfile, unteste
Set encoding = EUC-JP
Set termencoding = EUC-JP
Set fileencoding = EUC-JP
Endif
& Quot; detect UTF-8 locale, and replace CJK setting if needed
If V: lang = ~ "Utf8 $" | V: lang = ~ UTF-8 $"
Set encoding = UTF-8
Set termencoding = UTF-8
Set fileencoding = UTF-8
Endif
Else
Echoerr "sorry, this version of (g) Vim was not compiled with multi_byte"
Endif

Set Sw = 4
The indentation size is 4 spaces when auto indent is performed.

Set Ts = 4
The tab width is 4 characters.

Set et
Replace all tabs with spaces during editing.

Set Sm
The matching conditions of parentheses are displayed. When this option is enabled, the following parentheses (including parentheses, brackets, and braces) are entered)
The cursor will jump back to the Front Bracket for a moment, and then jump back to display the matching of the brackets.

Colorscheme darkblue (Blue, shine ....)
Color Scheme

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.