Vim command set and linuxvim command set in Linux

Source: Internet
Author: User
Tags printable characters perl script

Vim command set and linuxvim command set in Linux

Vim command collection

Command history

Commands starting with ":" and "/" have historical records. You can first type ":" or "/" and then press the up or down arrow to select a historical command.

StartVim

Enter the following command in the command line window:

Vim directly starts vim

Vim filename open vim and create a file named filename

FILE command

Open a single file

Vim file

Open multiple files at the same time

Vim file1 file2 file3...

Open a new file in the vim window

: Open file

Open a file in a new window

: Split file

Switch to the next file

: Bn

Switch to the previous file

: Bp

View the list of currently opened files. The files being edited are included in.

: Args

Open a remote file, such as ftp or share folder.

: Eftp: // 192.168.10.76/abc.txt

: E \ qadrive \ test \ 1.txt

Vim.

In normal mode (Press Esc or Ctrl + [enter). The file name is displayed in the lower left corner or is empty.

INSERT mode (press the I key to enter) display in the lower left corner -- INSERT --

VISUAL mode (do not know how to enter) display in the lower left corner -- VISUAL --

Navigation command

% Matching brackets

INSERT command

I insert before the current position

I insert at the beginning of the current row

Insert a after the current position

A is inserted at the end of the current row.

O insert a row after the current row

O insert a row before the current row

SEARCH Command

/Text Search text, search for the next one by njian, and search for the previous one by njian.

? Text Search text, reverse search, search for the next one by the n key, and search for the previous one by the N key.

Some special characters in vim need to be escaped during search. * [] ^ % /?~ $

: Set ignorecase case-insensitive search

: Set noignorecase case-insensitive search

Search for a long word. If a word is long and difficult to type, you can move the cursor over the word and press the * or # key to search for the word, which is equivalent to/search. And # The command is equivalent? Search.

: Set hlsearch highlights the search results. All results are highlighted, rather than displaying only one match.

: Set nohlsearch disable highlighted search display

: Nohlsearch disables the current highlight. If you search again or press the n or N key, it is highlighted again.

: Set incsearch gradually searches for the currently typed characters without waiting for the completion of the typing.

: Set wrapscan: Re-searches. When the file header or tail is searched, the system returns to continue searching. This function is enabled by default.

Replacement command

Ra replaces the current character with a, and the current character is the character where the cursor is located.

S/old/new/Replace new with old to replace the first match of the current row

S/old/new/g Replace new with old, replace all matches of the current row

% S/old/new/Replace new with old, replace the first match of all rows

% S/old/new/g Replace new with old, replace all matching

: 10, 20 s/^ // g add four spaces in front of each row in 10th rows to indent.

Ddp switches the row of the cursor and the row next to it.

Move command

H shifts one character to the left

L shifts one character to the right. This command is rarely used and is generally replaced by w.

K move one character up

J. Move one character down

The preceding four commands can be used with numbers. For example, 20 J means moving 20 lines down and 5 characters to the left for 5 hours. In Vim, many commands can be used with numbers, for example, you can delete 10 characters (10 x) and insert 3 after the current position !, 3a! The Esc command is required. Otherwise, the command does not take effect.

W moves a word forward (cursor stops at the word header). If it is at the end of the line, it is transferred to the beginning of the next line. This command is fast and can replace the l command.

B: move one word backward. 2b: move two words backward.

E, w, but the cursor stops at the end of the word.

Ge, the same as B, the cursor stops at the end of the word.

^ Move to the first non-blank character in the row.

0 (number 0) Move to the first character of the row,

Move to the first character of the line. Same as 0.

$ Move to the end of a row 3 $ move to the end of the following three rows

Gg moves to the file header. = [[

G (shift + g) moves to the end of the file. =]

The f (find) command can also be used to move. fx finds the first character x after the cursor, and 3fd finds the third character d.

F is the same as f, and reverse lookup is performed.

Jump to the specified row, colon + row number, and press Enter. For example, to jump to row 240, press ENTER 240. Another method is to jump to the row number + G, for example, 230G.

Ctrl + e scroll down a row

Ctrl + y scroll up a row

Ctrl + d scroll down the half screen

Ctrl + u scroll up half screen

Ctrl + f scroll down one screen

Ctrl + B scroll up one screen

Undo and redo

U Undo)

U undo the entire row

Ctrl + r Redo (Redo), that is, the Undo.

DELETE command

X Delete the current character

3x Delete the current cursor starting with three characters backward

X deletes the first character of the current character. X = dh

Dl deletes the current character, dl = x

Dh deletes the previous character

Dd deletes the current row

Dj deletes the previous row

Dk deletes the next row

Delete 10 rows starting with the current row within 10 days.

D. Delete the current character to the end of the line. D = d $

D $ Delete All characters after the current character (ROW)

Kdgg deletes all rows before the current row (excluding the current row)

JdG (jd shift + g) deletes all rows after the current row (excluding the current row)

: 1, 10 days delete 1-10 rows

: 11, $ d Delete 11 rows and all subsequent rows

: 1, $ d delete all rows

J (shift + j) deletes empty rows between two rows, which are actually merged.

Copy and paste

Yy copy the current row

Nyy copies the n rows starting from the current row, for example, 2yy copies the current row and the next row.

P is pasted after the current cursor. If you have used the yy command to copy a row, paste it in the next row of the current row.

Shift + p paste in front of current row

: 20th co 20 insert 1-10 rows to rows.

: 1, $ co $ copy the entire file and add it to the end of the file.

In normal mode, press v (verbatim) or V (line-by-line) to enter the visual mode. Then, use the jklh command to move and select certain lines or characters. Then press y to copy them.

Ddp exchange the current row and next row

Xp exchanges the current character with the next character

Cut command

In normal mode, press v (verbatim) or V (line-by-line) to enter the visual mode. Then, use the jklh command to move and select certain lines or characters. Then press d to cut.

Ndd cut the n rows after the current row. The p command can be used to paste the cut content.

: 1, 10 days Cut 1-10 rows. The p command can be used to paste the cut content.

: 1, 10 m 20 move rows 1-10 to rows 20th.

Exit command

: Wq save and exit

ZZ save and exit

: Q! Force exit and ignore all changes

: E! Discard all modifications and open the original file.

Window command

: Split or new opens a new window, And the cursor stops at the top-level window

: Split file or: new file open the file in a new window

The split window is displayed horizontally. You can use vsplit to open the window vertically.

Ctrl + ww move to the next window

Ctrl + wj move to the window below

Ctrl + wk move to the window above

Close Window

: You cannot use this command to close the last window to prevent accidental exit of vim.

: Q if it is the last closed window, it will exit vim.

ZZ save and exit.

Close all windows and keep only the current window

: Only

Recording macro

Press the q key and add any letter to start recording, and then press the q key to End recording (which means That Macros in vim cannot be nested). When using the q key, @ add macro name, such as qa... Q: The recording name is a macro. @ a uses this macro.

RunShellCommand

:! Command

:! Ls to list files in the current directory

:! Perl-c script. pl check the perl script syntax. It is very convenient to exit vim.

:! Perl script. pl: It is very convenient to execute a perl script without exiting vim.

: Suspend or Ctrl-Z suspends vim and returns to shell. Press fg to return vim.

Comment command

In the perl program, # The Beginning of the behavior annotation, so to comment some rows, you only need to add at the beginning of the line #

3, 5 s/^/#/g comment line 3-5

3, 5 s/^ # // g uncomment 3-5 rows

1, $ s/^/#/g comment the entire document.

: % S/^/#/g to comment out the entire document. This method is faster.

Help commands

: Help or F1 displays the entire help

: Help xxx displays the help of xxx, for example: help I,: help CTRL-[(that is, Ctrl + [help ).

: Help 'number' the Vim option is enclosed in single quotes

: Help Help with special keys <> expanded

: Help-t help for Vim startup parameters-

: Help I _ Esc help in insert mode, help mode in a mode-topic Mode

The content in the Help file is a hyperlink. You can press Ctrl +] to enter the link, and press Ctrl + o (Ctrl + t) to return

Other non-edit commands

. Repeat the previous command

: Set rtasks? Check whether rsequence is set. In. vimrc, you can use this command to view the set options.

: Scriptnames: view the location of the vim script file, such as The. vimrc file, syntax file, and plugin.

: Set list displays non-printable characters, such as tab, space, and end of line. If the tab cannot be displayed, use the set lcs = tab:>-command to set it. vimrc file, and make sure that there is a tab in your file. If expendtab is enabled, the tab will be expanded to a space.

Vim tutorial

On Unix systems

$ Vimtutor

On Windows

: Help tutor

: Syntax list defined syntax items

: Syntax clear defined syntax rules

: Syntax case match is case sensitive. int and Int are considered different syntax elements.

: Syntax case ignore is case-insensitive. int and Int are treated as the same syntax elements and use the same color scheme.

Vi usage

The vi editor is a standard editor for all Unix and Linux systems. It is not inferior to any of the latest text editors. Here we just briefly introduce its usage and a few instructions. The vi editor is identical for any version of Unix and Linux systems, so you can learn more about it in any other section about vi. Vi is also the most basic text editor in Linux. After learning it, you will be able to enjoy the free access in the Linux World.

1. Basic concepts of vi

Basically, vi can be divided into three states: command mode, Insert mode, and last line mode. The functions of each mode are as follows:

1) command Line mode)

Controls the movement of the screen cursor, the deletion of characters, words, or rows, the movement of copying a segment and entering the Insert mode, or to the last line mode.

2) Insert mode)

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

3) Baseline mode)

Save the file or exit vi. You can also set the editing environment, such as searching strings and listing row numbers ...... .

However, we generally simplify vi into two modes in use, that is, the last line mode is also included in the command line mode ).

2. Basic operations of vi

A) Go to vi

After the system prompts you to enter the vi and file name, you will be taken to the vi full screen editing screen:

$ Vi myfile

Note that after entering vi, you are in command mode. You must switch to Insert mode to Enter text. People who use vi for the first time will want to move the cursor first with the upper and lower right keys. As a result, the computer keeps beeping and getting angry with themselves. So after entering vi, do not tamper with the mouse, switch to "Insert mode!

B) switch to Insert mode to edit the file

Click "I" under "command mode" to enter "Insert mode". Then, you can enter the text.

C) Insert switchover

You are currently in "Insert mode", and you can only enter text all the time. If you find that you have entered an error! To move the word back with the light mark key, you must first Press ESC to switch to command mode and then delete the text.

D) Exit vi and save the file

Under "command mode", click ":" colon to enter "Last line mode". For example:

: W filename (Enter "w filename" to save the article with the specified file name filename)
: Wq (Enter "wq" to save the disk and exit vi)
: Q! (Enter q !, Force exit vi without saving the disk)

3. command mode function key

1). insert mode

Press "I" to switch to the insert mode "insert mode 」 After you press "I" to enter the insert mode, the input file starts from the current position of the cursor.
Press a to enter the insert mode. Is to input text from the next position where the current cursor is located
Press "o" to enter the insert mode. Is to insert a new line, from the beginning of the line Input text

2) switch from insert mode to Command Line Mode

Press ESC.

3) move the cursor

Press ctrl + B 」 Move one page from the screen to the back Press ctrl + f 」 Move the screen to the front
Press ctrl + u 」 The screen moves half a page to the rear. Press ctrl + d 」 Move the screen to the front half of the page
Press the number "0 」 Move to the beginning of the article Press G 」 Move to the end of the article
Press "$ 」 Move to the end of the row where the cursor is located" Press "^ 」 Move to the "first line" of the row where the cursor is located"
Press "w 」 Jump the cursor to the beginning of the next word Press "e 」 Move the cursor to the end of the next word
Press "B 」 The cursor returns to the beginning of the previous word Press "# l 」 Move the cursor to the # position of the row, such as 5l and 56l.

Vi can be moved up, down, left, and right directly with the cursor on the keyboard, but the regular vi uses lowercase letters "h", "j", "k", and "l 」, the cursor is controlled to move one cell to the left, down, up, and right respectively.

4). delete text

「 X 」 Each time you press the cursor, delete the "Next" character at the cursor position.
「 # X 」 For example, "6x" indicates to delete the "6" characters after the cursor position
「 X 」 Uppercase X: deletes the "front" character at the cursor position every time it is pressed.
「 # X 」 For example, "20X" indicates to delete the "front" of the cursor 20 characters
"Dd 」 Delete the row where the cursor is located
「 # Dd 」 Delete from the row where the cursor is located # Row

5). Copy

「 Yw 」 Copy the character at the end of the cursor to the buffer.
「 # Yw 」 Copy # words to the buffer zone
「 Yy 」 Copy the row where the cursor is located to the buffer zone
「 # Yy 」 For example, "6yy" indicates copying 6 lines of text from the row where the cursor is located "down"
「 P 」 Place the characters in the buffer to the cursor. Note: All copy commands related to "y" must work with "p" to complete the copy and paste function.

6). Replace

「 R 」 Replace the character at the cursor.
「 R 」 Replace the character wherever the cursor goes until you press the ESC key.

7). Reply to the previous operation

"U": If you mistakenly execute a command, you can immediately press "u" to return to the previous operation. You can perform multiple replies for multiple times by "u.

8). Change

「 Cw 」 Change the word at the cursor to the ending point.
「 C # w 」 For example, "c3w" indicates to change three characters.

9). Jump to the specified row

Ctrl + g lists the row numbers of the cursor.

"# G": for example, "15G" indicates moving the cursor to the first row of the article.

4. Introduction to commands in Last line mode

Before using "last line mode", remember to press the "ESC" key to confirm that you are already in "command mode" and then press 「: "colon to enter" last line mode 」.

A) List row numbers

「 Set nu 」 After "set nu" is entered, the row number is listed before each row in the file.

B) jump to a row in the file.

"#": "#" Indicates a number. Enter a number after the colon and press enter to jump to the row. For example, enter the number 15 and press Enter, this will jump to the second line of the article.

C) Search for characters

"/Keyword": First press the "/" key and then enter the character you want to search. If the first search keyword is not what you want, you can always press "n" until you find the keyword you want.

「? Keyword: First press 「?」 Enter the character you want to search for. If the keyword you want for the first time is not what you want, you can press "n" until you find the keyword you want.

D) save the file

「 W 」 Enter "w" in the colon to save the file. 「 E 」 Leave vi
「 Q 」 Press "q" to exit. If you cannot exit vi, you can add "!" to "q 「!」 Force exit vi 「 Qw 」

We recommend that you use it with "w" when you exit,

In this way, you can save files when exiting.

5. vi command list

1. The following table lists some key functions in command mode:

L

Move the cursor one character to the right K Move the cursor up a row

H

Move the cursor one character left J Move the cursor down a row

^

Move the cursor to the beginning of the line 0 Number "0", move the cursor to the beginning of the article

G

Move cursor to the end of the article $ Move the cursor to the end of the row

Ctrl + f

Forward screen flip Ctrl + B Flip back

Ctrl + d

Front Half Screen Ctrl + u Flip back half screen

I

Insert characters before the cursor position A Add the next character at the cursor position

O

Insert a new row and start from the beginning of the row. ESC Return from input to command status

X

Delete characters after the cursor # X # Characters After deleting the cursor

X

(Uppercase X), delete the character before the cursor # X Delete # characters before the cursor

Dd

Delete the row where the cursor is located # Dd Delete # rows from the number of rows where the cursor is located

Yw

Copy a word at the cursor position # Yw Copy the # characters at the cursor position

Yy

Copy a line at the cursor position # Yy Copy the number of rows from the cursor

P

Paste U Cancel operation

Cw

Change the cursor position. # Cw # Words for changing the cursor position

2. Some commands in the following Table travel command mode

W filename Save the file being edited as filename
Wq filename Save the file being edited as filename and exit vi
Q! Discard all modifications and exit vi
Set nu Show row number
/Or? Search, enter the content to search after/
N And/or? If the search content is not the keyword, press n or backward (and/) or forward (and? Continue searching until it is found.

Note the following when using vi for the first time:

1. After opening a file with vi, it is in command mode. You must switch to Insert mode to Enter text. Switch method: click "I" under "command mode" to enter "Insert mode )」, now you can start to input text.

2. After editing, you must switch from the insert mode to the command line mode to save the file. to switch to the mode, Press ESC.

3. Save and exit the file: In command mode, enter wq! (Don't forget the one before wq)

ViUser Manual

VI is the most commonly used text editing tool on unix. As a unix Software tester, it is necessary to master it.

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 editing with vi last time, restoring filename
Vi filename... filename Open multiple files and edit them one by one

Move cursor command

H Move the cursor one character 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 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 by the cursor
B or B Move one word left to the beginning of the word E or E

Move the cursor to the right of a word j to the end of the word

): 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 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 cursor to the middle line of the screen
L Move 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 row    

Screen tumble command

Ctrl + u Half screen at the beginning of a file Ctrl + d Flip the half screen to the end of the file
Ctrl + f Flip the screen at the end of the file Ctrl + B Flip the 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 Behind 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 Replaces a specified number of characters with input text starting from the current cursor position S Delete a specified number of rows and replace them with the input text
Ncw or nCW Modifies a specified number of characters. NCC Modifies a specified number of rows.

DELETE command

Ndw or ndW Delete n-1 characters starting at and following the cursor Do Delete to the beginning of a row
D $ Delete to end of line Ndd Delete the current row and n-1-1 row
X or X Delete a character, x deletes the cursor, and X deletes Ctrl + u Delete text entered in input mode

Search and replace commands:

/Pattern Search for pattern from the beginning of the cursor to the end of the file
? Pattern Search for pattern from the beginning of the cursor
N Repeat the previous search command in the Same Direction
N

: Repeat the last 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 files with p2.

Option settings

All List all options Term Set the terminal type
Ignorance Case Insensitive in search List Display the tab (Ctrl + I) and the end of the line sign ($)
Number Show row number Report Display the number of changes made by the line-oriented command
Terse Show brief warning information Warn NO write information is displayed if the current file is not saved when it is transferred to another file.
Nomagic In search mode, special characters without "\" are allowed. Nowrapscan Prohibit vi from starting from the other end when searching to the two ends of the file
Mesg Allow vi to display the information that other users write to their terminal using write    

Last-line command

: N1, n2 co n3 Copy the 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 line n1 and line n2
: W Save current file
: E filename Open the file filename and edit it.
: X Save the current file and exit
: Q Exit vi
: Q! Do not save the file and exit vi
:! Command Execute shell command
: N1, n2 w! Command The content of lines n1 to 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 command to the current line

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.