Vim shortcut key (GO)

Source: Internet
Author: User
Tags repetition

VIM shortcuts:
Cursor movement:
Four directions
   k
h 0 l
   j
  Ctrl+f, ctrl+b page down, page up
  Ctrl+d, ctrl+u page down halfway, page up half
  $ move end of line
  0 move the beginning of the line
  w move the next word
  b Move to the previous word
  Gg jumps to the beginning of the document
  G jumps to the end of the document
  % jumps to the matching parentheses ("{ }""[]""()")
  Ctrl+i,tab jump to the next jump point
  Ctrl+o jump to the previous jump point
  23gg, 23G, :23 Jump to line 23
  Ctrl+i, tab Jump to the next jump point (Jump Point)
  Ctrl+o jump to the last hop
Find replacement:
# Find the entire keyword from the cursor backwards
* Find the entire keyword from the cursor forward
g# Find keywords from the cursor backwards
g* Find keywords from the cursor forward
Fx, tx, Fx, Tx find characters in the current line
Find a replacement example:
: s/SEARCH/REPLACE
: s/If/Since replaces the next "If" with "Since"
: %s/If/Since replaces all "If" with "Since"
: 1,3 s/If/Since/g is valid only for 1,3 lines. If there is no prefix, it is only valid for the current line.
expression:
Replace one character
* indicates a repetition of one or more characters

/d.*an can match dan, divan, debian

Word boundary:
Specify the word boundary: \
Such as: /\ Matches start with d, with any lowercase letters in the middle, words ending with an
/\$[0-9]*\.[0-9][0-9] Matching Find the number of $XX...X.XX, with only two decimal places
Common editing commands:
a, i is inserted after the cursor, inserted before the cursor
Dd delete a line
Cc,S deletes a line and enters insert mode
Dw delete a word
Cw deletes a word and enters insert mode
x, dl delete a character
s,cl deletes a character and enters insert mode
p paste
Xp exchanges two characters
Ddp exchange two lines
y copy
Yy copy a line
u undo
Ctrl+r redo
Repeat the last modification

Ctrl+r redo
Repeat the last modification

Divide the pane:
:split/vsplit separates a window
:new/vnew create a new window
:sf {filename} opens filename in a new window
:close closes the current window
:only closes all windows except the current window
:ctrl-w h to the left window
:ctrl-w j to the window below
:ctrl-w k to the window above
:ctrl-w l to the right of the window
:ctrl-w t to the top of the window
:ctrl-w b to the bottom of the window

Repeat operation (macro operation):
q[a-z] starts the recording operation and records it in the register [a-z]
q Stop recording operation
@[a-z] Execute the operation in the register
@@ Executes the operation recorded in the most recent register
Example: A buffer has two lines:
  Sys/types.h
  Stdio.h
--> To be changed to:
  #i nclude
  #i nclude
The operation is as follows:
  Qa #Start recording
  ^ #Move the beginning
  i #Enter insert mode
  #i nclude #Entering content
  q #STOP record
  Move another line:
  @a can perform the same operation

Visual Mode operation:
Ctrl+v enters block-based visual mode
v Enter character-based visual mode
V enters line-based visual mode

c delete the selected block
I{string} After selecting the block, press the uppercase I, enter the string, and press ESC to insert the same content in each line of the block.

Jump to the statement:
[[ Forward to the first "{" of the top grid
[] Jump forward to the first "}" of the top grid
][ Jump back to the first "{" of the top grid
]] Jump back to the first "}" of the top grid
[{ Jump to the beginning of this code block (defined by {})
[} Jump to the end of this code block
Shell:
:ctrl+z/suspend hangs vim in shell; minimizes window under gui
:!{command} Execute shell commands
:shell open a new shell

Save vim state (hang?):
:mksession session.vim save the current vim state
:source session.vim Reply to vim status
Vim -S session.vim restore session when starting vim
High efficiency movement
Outside of insert mode
Basically, you should stay in the insert mode as little as possible, because in the insert mode VIM is like a "dumb" editor. Many newbies will stay in the insert mode because it's easy
use. But the power of VIM is his command line mode! You will find that after you get to know VIM more and more, you will spend less and less time using insert mode.
Use h,j,k,l
The first step in using VIM for efficient editing is to abandon the use of the arrow keys. With VIM, you don't have to move between the arrow keys and the letter keys frequently, which saves you a lot of time. When you are in command mode
When you use h, j, k, l to implement the functions of the left, bottom, top, and right arrows respectively. You may need to get used to it at first, but once you get used to it, you will find it efficient.
When you edit your email or other text with paragraphs, you may find that using the arrow keys is not the same as you expected, and sometimes you may skip a lot of lines at once. This is because your paragraph is looking at VIM
Come is a big long line. At this point you can type a g before pressing h, j, k or l, so that VIM will move as you wish by pressing the line above the screen.
Move the cursor effectively in the current line
Many editors only provide simple commands to control the movement of the cursor (such as left, up, right, down, to the beginning/end of the line, etc.). VIM provides a lot of powerful commands to satisfy your desire to control the cursor. When the cursor is from one
The point moves to another point, and the text between the two points (including the two points) is called "crossed", and the command here is also called motion. (Simplified explanation, this important concept will be used later)
Here are some commonly used commands (motion):
Fx: Moves the cursor to the next x of the current line. Obviously, x can be any letter, and you can use it to repeat your last f command.
Tx: Similar to the above command, but moved to the left of x. (This is really useful)
Fx: Similar to fx, but looking back.
w: The cursor moves forward one word.
b: The cursor moves back one word.
0: Move the cursor to the beginning of the current line.
^: Moves the cursor to the first letter position of the current line.
$: Moves the cursor to the end of the line.
): Move the cursor to the next sentence.
( : Moves the cursor to the previous sentence.
Move the cursor effectively in the entire file
VIM has a lot of commands that can be used to get to where you want to go in the file. Here are some commands that move inside the file:
: Move down one screen.
: Move up one screen.
G: to the end of the file
numG: Moves the cursor to the specified line (num). (For example, 10G is to the 10th line)
Gg: to the beginning of the file
H: Move the cursor to the top of the screen
M: Move the cursor to the middle of the screen
L: Move the cursor to the bottom of the screen
*: Read the string at the cursor and move the cursor to where it appears again.
#: Similar to the above, but looking in the opposite direction.
/text: Searches for the string text from the current cursor and reaches where the text appears. You must use the carriage return to start this search command. If you want to repeat the last search, press n.
? Text: Similar to the above, but in the opposite direction.
Ma: Mark a bookmark at the current cursor position with the name a. Book signatures can only be lowercase letters. You can't see the existence of the bookmark, but it is already there.
`a: Go to bookmark a. Note that this is not a single quote, it is usually located to the left of the 1 of most keyboards.
`.: To the place where you last edited the file. This command is useful and you don't have to mark it yourself.
Efficient input
Automated using keywords
VIM
There is a very beautiful keyword auto-complete system. This means that you can type in a part of a long word and then press a key, then VIM will complete the input of the long word for you. For example: you have
A variable named iAmALongAndAwkwardVarName is somewhere in the code you write. Maybe you don't want to enter it one letter at a time.
To use the keyword auto-completion feature, you only need to enter the first few letters (such as iAmAL), then press (hold Ctrl, then N) or. If VIM doesn't give you the word you want, basically press it until you are satisfied, VIM will always loop through the matching string it finds.
Smart entry mode
Many newcomers enter the insert mode with just i. This of course can enter the insert mode, but usually not so suitable, because VIM provides a lot of commands to enter the insert mode. Here are some of the most common ones:
i: insert on the left side of the current character
I: Insert at the beginning of the current line
a: insert on the right side of the current character
A: Insert at the end of the current line
o: insert a new line below the current line
O: Insert a new line above the current line
c{motion}: Deletes the characters crossed by the motion command and enters insert mode. For example: c$, this will delete the characters from the cursor position to the end of the line and enter the insert mode. Ct! This will remove the exclamation point from the cursor position to the next (but not included) and then enter insert mode. The deleted characters are stored in the clipboard and can be pasted again.
d{motion}: Similar to the above, but does not enter insert mode.
Effectively move large sections of text
Use visual selections and appropriate selection modes
Don't want the original VI, VIM allows you to highlight (select) some text and do it. There are three visual selection modes:
v: Select by character. The pattern you use often, so try it out for yourself.
V: Select by row. This is especially useful when you want to copy or move a lot of lines of text.
: Select by block. Very powerful, this feature is only available in a few editors. You can select a rectangular block and the text inside the rectangle will be highlighted.
Use the arrow keys and commands described above when selecting a mode. For example, vwww will highlight the three words in front of the cursor. Vjj will highlight the current line and the next two lines.
Cut and copy in visual selection mode
Once you highlight the selection, you may want to do something:
d: Clip the selected content to the clipboard.
y: Copy the selected content to the clipboard.
c: Clip the selected content to the clipboard and enter the insert mode.
Cut and copy in non-visual selection mode
If you know exactly what you want to copy or cut, you don't need to enter the visual selection mode at all. This will also save time:
d{motion}: Cuts the characters crossed by the motion command to the clipboard. For example, dw will cut a word and dfS will cut the character from the current cursor to the next S to the clipboard.
y{motion}: Similar to the above, but a copy.
c{motion}: Similar to d{motion}, but finally enters insert mode.
Dd: Cut the current line.
Yy: Copy the current line.
Cc: Cuts the current line and enters insert mode.
D: Cut from the cursor position to the end of the line to the clipboard.
Y: Copy the current line.
C: Similar to D, and finally enters insert mode.
x: Cut the current character to the clipboard.
s: Similar to x, but finally enters insert mode.
Paste
Pasting is very simple, press p.
Use multiple clipboards
Many editors only provide a clipboard. There are many VIMs. The clipboard is called Registers in VIM. You can list all the currently defined register names and their contents. The command is ":reg". It is best to use lowercase letters as the name of the register, because some of the uppercase is occupied by VIM.
The command to use the register is double quotes ".
For example: we want to copy the current line to register k. You should press "kyy. (You can also use V"ky. Why is this also possible?) Now the current line should already exist in register k until you copy something into register k. Now you can use the command "kp" to paste the contents of register k to the location you want.
Avoid repetition
Amazing . order
In the VI, type . (decimal) to repeat the last command you gave. For example, if your last command is ‘dw‘ (delete a word), the VI will delete another word.
Using numbers
Using numbers is also one of VIM's powerful and time-saving features. A number can be used before many VIM commands. This number will tell VIM that this command needs to be executed several times. such as:
3j will move the cursor down three lines.
10dd will be deleted Ten lines.
Y3" will copy the contents from the current cursor to the third occurrence of the quotes to the clipboard.
Numbers are a very effective way to extend the scope of the motion command.
Record macro
Sometimes you will find yourself repeating the same series of actions in each or every line of the article. VIM allows you to record a macro to fulfill your specific needs.
Qregister: Record the macro to register register, where register is the name of any register you have. For example, qa will record and store the macro in register a.
q: End the record of the macro.
@register: Use the macro that exists in register register. For example @a, the macro stored in register a will be used.
It's important to remember that macros only record your series of keys and repeat them. They are not magic. Because there are many ways to accomplish your goals in VIM, sometimes you have to choose commands to record your macros. Because they will execute wherever you want to execute it.
Write code with VIM
VIM is a great editor to write code because it has some features that are designed for programmers. Here are some common ones:
]p: Similar to p, but it automatically adjusts the indentation of the pasted text to fit the current code. try it!
%: Match curly braces, square brackets, parentheses, etc. Above a parenthesis, then press % and the mouse will appear in the other half of the matching bracket.
>>: Indent all selected code


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.