VIM command, operation and shortcut key

Time:2021-5-13

Official account: MarkerHub, website:https://markerhub.com

Read by little hub:

Know that you use VIM less, many commands easy to forget, so specially collected a wave of common command set, hope to help you ha!


Command history

Commands beginning with: and / have a history. You can first type: or / and then press the up and down arrows to select a history command.

Start VIM

Enter the following command in the command line window

VIM starts VIM directly

VIM filenameopen 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 the file in a new window

:split file

Switch to the next file

:bn

Switch to previous file

:bp

View the list of currently opened files, and the files currently being edited will be enclosed with [].

:args

Open a remote file, such as FTP or share folder

:e ftp://192.168.10.76/abc.txt

:e \qadrive est.txt

The mode of vim

In normal mode (press ESC or Ctrl + [enter), the file name is displayed in the lower left corner or empty
Insert mode (press I key to enter) display in the lower left corner — insert–
Visual mode (don’t know how to enter) lower left corner display — Visual–

Navigation command

%Bracket matching

Insert command

I insert in the current position

I insert at the beginning of the current line

A insert after current position

A inserts at the end of the current line

O insert a row after the current row

O insert a row before the current row

Find command

/Text to find text, press the N key to find the next one, and press the N key to find the previous one.

? Text search text, reverse search, press n key to find the next, press n key to find the previous.

There are some special characters in VIM that need to be escaped when searching~$

: set ignorecase ignore case lookup

: set no ignore case lookup

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

: set hlsearch highlights the search results. All results are highlighted instead of just one match.

: set nohlsearch to turn off the highlight search

: nohlsearch turns off the current highlight. If you search again or press the N or n key, it will be highlighted again.

: set incsearch stepwise search mode, searches for the currently typed characters without waiting for the completion of typing.

: set wrapscan to search again. When the header or tail of the file is found, return to continue searching. It is on by default.

Replace command

RA replaces the current character with a, and the current character is the character of the cursor.

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

S / old / new / g replace new with old to replace all matches in the current row

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

%S / old / new / g replace new with old to replace all matches of the whole file

:10,20 s/^/     / G indent lines 10 to 20 with four spaces in front of each line.

DDP exchanges the line where the cursor is located and the line next to it.

Move command

H moves one character to the left
L move one character to the right, this command is rarely used, generally w instead.
K up one character
J moves one character down
The above four commands can be used with numbers. For example, 20j is to move 20 lines down and 5h is to move 5 characters to the left. In VIM, many commands can be used with numbers, such as deleting 10 characters 10x and inserting 3 characters after the current position!, 3a!< ESC >, where ESC is required, otherwise the command will not take effect.

W move one word forward (the cursor stops at the beginning of the word), if it has reached the end of the line, go 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. The same as W, but the cursor stops at the end of the word

The cursor stops at the end of the word.

^Move to the first non blank character on the line.

0 (digit 0) is moved to the first character on the line,

< Home > moves to the first character on the line. The same as 0.

$move to the end of the line 3 $move to the end of the next 3 lines

GG to the header[[

G (Shift + G) to the end of the file. =]]

The f (find) command can also be used to move. FX will find the first X character after the cursor, and 3fd will find the third D character.

F is the same as F, reverse search.

Jump to the specified line, colon + line number, enter, for example, jump to line 240 is: 240 enter. Another method is line number + g. for example, 230g jumps to line 230.

CTRL + e scroll down one line

CTRL + y scroll up one line

CTRL + D scroll down half screen

CTRL + U scroll up half screen

CTRL + F scroll down one screen

CTRL + B scrolls up one screen

Undo and redo

U undo
U undo the operation on the whole line
CTRL + R redo (redo), which is the undo of undo.

Delete command

X delete the current character

3x delete the current cursor three characters backward

X removes the previous character of the current character. X=dh

DL delete the current character, DL = x

DH delete the previous character

DD delete current line

DJ delete previous line

DK delete next line

10d delete the first 10 lines of the current line.

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

D $delete all characters after the current character (line)

Kdgg delete all lines before the current line (excluding the current line)

jdG(jd shift + g)   Delete all rows after the current row (excluding the current row)

: 1,10d delete 1-10 lines

: 11, $d delete 11 lines and all subsequent lines

: 1, $d delete all lines

J (Shift + J) removes the empty line between the two lines, which is actually merging the two lines.

Copy and Paste

YY copy current line

NYY copies the N lines starting after the current line. For example, 2yy copies the current line and its next line.

p   Paste after the current cursor. If you used the YY command to copy a line before, paste it on the next line of the current line.

Shift + P paste before current line

: 1,10 co 20 insert lines 1-10 after line 20.

: 1, $co $copies the entire file and adds it to the end of the file.

In normal mode, press V (word by word) or V (line by line) to enter visual mode, then use jklh command to move to select some lines or characters, and then press y to copy

DDP exchanges the current row with its next row

XP exchanges the current character and the next character

Cut command

In normal mode, press V (word by word) or V (line by line) to enter visual mode, then use jklh command to move to select some lines or characters, and then press D to cut

NDD cuts the n rows after the current row. Use the P command to paste the cut content

1,10 d cut 1-10 lines. Use the P command to paste the cut content.

: 1, 10 m 20 move lines 1-10 after line 20.

Exit command

: WQ save and exit

ZZ save and exit

:q! Force exit and ignore all changes

:e! Discard all changes and open the original file.

Window command

: split or new opens a new window with the cursor resting on the top window

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

Split open windows are horizontal, using VSplit can open windows vertically.

CTRL + WW to move to the next window

CTRL + WJ move to the lower window

CTRL + wk move to the upper window

close window

: close the last window can’t use this command to prevent the VIM from being exited accidentally.

: q if it is the last closed window, VIM will exit.

ZZ saves and exits.

Close all windows and keep only the current window

:only

Record macro

Press Q and any letter to start recording, and then press Q to end recording (this means that the macro in VIM cannot be nested). When using @ add the macro name, such as QA… Q records a macro named a, which @ a uses.

Execute shell command

:!command

:! Ls lists the files in the current directory

:! Perl – C script. PL check the Perl script syntax, you do not need to exit VIM, very convenient.

:! Perl script.pl can execute Perl script without exiting VIM, which is very convenient.

: suspend or Ctrl – Z to suspend vim and return to shell. Press FG to return to vim.

Comment command

In Perl program, # starts with behavior annotation, so to annotate some lines, you only need to add some comments at the beginning of the line#

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

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

1, $s / ^ / # / g comments the whole document.

:% s / ^ / # / g annotates the entire document, which is faster.

Help command

: help or f1displays the entire help
: help XXX displays the help of XXX, such as: help I,: help Ctrl – [(that is, help of Ctrl +].
Help for the: help ‘number’ VIM option is enclosed in single quotation marks
The help of: help < ESC > special key is expanded with < >
: Help – t help with VIM startup parameters-
:help i_< ESC > help for ESC in insert mode. Help for a certain mode is in insert mode_ The mode of theme
The content between |, in the help file, is a hyperlink. You can use Ctrl +] to enter the link and Ctrl + O (Ctrl + T) to return

Other non editing commands

. repeat the previous command

:set ruler? Check whether ruler is set. In. Vimrc, the options set with the set command can be viewed through this command

: scriptnames to view the location of vim script files, such as. Vimrc file, syntax file and plugin.

: set list displays nonprintable characters, such as tab, space, end of line, etc. If the tab cannot be displayed, please make sure that the. Vimrc file is set with the set LCS = tab: > – command, and make sure that there is a tab in your file. If the expandtab is turned on, the tab will be expanded to a space.

VIM tutorial

On UNIX system
$ vimtutor
On Windows system
:help tutor
: syntax lists the defined syntax items
: syntax clear clears defined syntax rules
: syntax case match is case sensitive, int and int are treated as different syntax elements
: syntax case ignore is case independent, int and int are treated as the same syntax elements and use the same color scheme

Recommended reading

Great! This Java website has all kinds of projects! https://markerhub.com

The up Master of the B station, Java is really good!

Great! The latest version of Java programming ideas can be seen online!