Tips for using Vim

Source: Internet
Author: User
Operations in VIM

I. Set the size-character Mismatch
Set ignorecase/set IC
Set noignorecase/set noic
2. Set to highlight and not show when searching
Set hlsearch/set HLS
Set nohlsearch/set nohls
3. Set to display matching points during the search process and do not display matching points during the search process
Set incsearch/set is
Set noincsearch/set nois
4. Set the search end or start to stop and find the end to fold back to the start of the file and re-search
Set wrapscan/set WS
Set nowrapscan/set nows
5. Set the display row number
Set number/set nu
Set nonumber/set Nonu
6. Set the tab key width
Set Ts = 4

Move commands in VIM

http://vcd.gro.clinux.org/doc/usr_03.html#usr_03.txt
1. Move one word forward
W 3 w B 2B (move one word forward/move three words forward/move one word backward two words)
E (move to the end of each word) Ge (move to the end of the previous word)
commands for moving words include: w/E/B/GE/Ge
2. move the cursor to the beginning or end of the line
"$" to move the cursor to the end of the current line. The "^" command moves the cursor to the first non-empty character of a line, while the "0" command moves the cursor to the first character of a line,
3. Move to a specified character
the single character SEARCH Command is one of the most useful mobile commands. The "FX" command looks forward to the character X in the row.
you can add the Count prefix before this command. Therefore, you can use the "3fl"
"F" command to search left:
the "TX" command is similar to "FX", but it only moves the cursor to the first character of the target character.
4. Move to the specified line:
"33g" sends you to 33
"G" command to move the cursor to the end of the file. The command to move to the beginning of the file is "GG ".
"50%" is moved to the middle of the file, while "90%" is moved to almost the end of the file.
"H" indicates that "home" (header) is moved to the top of the screen, and "M" indicates "Middle" (medium) move to the middle of the screen and "L" means "last" (tail) Move to the bottom of the screen.
5. Scrolling
CTRL-U commands scroll down the half screen CTRL-D command move the window down the half screen
the command for each row rolling is a CTRL-E (roll up) and CTRL-Y (rollback ).

Set Vim options
If you need to set some options every time you use vim, such as whether to match the case or not and display the row number, you can write these commands to the vim Startup File.
Use: scriptnames to find out where the vim Startup File is located.
Then: Edit ~ /. Vimrc
You can set some options in this file.

 

Use tags in VIM

1. When you use the "G" command to jump to another place, VIM will remember where you jumped. This location becomes a tag. to return to the original location, run the following command :''
The ''command can be jumped in two locations. The CTRL-O command jumps to a "older" place (Tip: O indicates older ). The CTRL-I jumps to a newer place.
: The jumps command can output a list of locations you can jump. The last tag you use is marked with the ">" symbol.
2. Vim allows you to place custom tags in the text. Run the "Ma" command to mark the current cursor position with. You can use 26 tags (A to Z) in the text ). These tags are invisible, but they are only remembered by VIM.
To jump to a tag you defined, run '{mark}. Here {mark} refers to the letter that defines the tag. Therefore, the command to move to tag a is: 'A
3. You can use the following command to obtain the list of all tags: marks
To some special tags, including:
'Cursor position before jump
"Last edited cursor position
[Last modified start position
] End position of the last modification

 

Operators and actions in VIM

I. Operator-Action Mode
The "DW" command deletes a word. You may recognize that "W" is a word moving command. In fact, the "D" command can be followed by any "action" command, which deletes all content from the current position to the target position to which the cursor moves.
The preceding command combination mode is defined: Operator-action. First, enter an operator command. For example, "D" is a delete operator. Enter an action command, for example, "4l" or "W ". This method enables you to perform various operations on any text that you can pass.
The other operator command is "C", which indicates modification and change. It works in a similar way as the "D" operator, but will switch to the insert mode after completion. For example, "CW" modifies a word. More precisely, it deletes a word and switches to the insert mode.
To err is human
------->
C2wbe <ESC>
To be human
Here, "c2wbe <ESC>" includes the following operations:
C. Modify Operators
2 W to move the distance between two words (combined with the operator, it deletes two words and enters the insert mode)
Be inserts the word "be ".
2. More command changes
Like "DD", "cc" can be used to delete a row. However, it retains the indentation of this line (leading space ).
"D $" is deleted to the end of the row; "C $" is modified to the end of the row.

Both the "3dw" and "d3w" commands delete three words. If you have to search for the root, "3dw" indicates that one word is deleted three times, and "d3w" indicates that three words are deleted once. This is a separate one. In fact, you can put two counting prefixes. For example, "3d2w" deletes two words three times, totaling six words.
3. Repeat one modification
"." Is a very simple and useful command in vim. It repeats the last modification operation. For example, if you are editing an HTML file, you want to delete all the <B> tags. Move the cursor to the first "<" and then run the "DF>" command to delete <B>. Then you can move it to </B> and use the "." command to delete it. "."
Command to execute the last modification command (in this example, it is "DF> "). Delete the next <B> tag, move it to the next <location, and then run the "." command.
To <B> Generate </B> A table of <B> Contents
F <find the first <--->
DF> Delete to> -->
F <find the next <--------->
. Repeat DF> --->
F <find the next <------------->
. Duplicate DF> -->

The "." command repeats any modifications except the "U" (UNDO), The CTRL-R (redo), and the colon command.
Another example: You want to change "four" to "five ". Such modifications are required in several places. You can
Run the following command to quickly complete the operation:

/Four <enter> find the first "four"
Change cwfive <ESC> to "five"
N find the next "four"
. Repeat the modification operation.
N find the next "four"
. Modify again
And so on ......
Iv. Visual Mode
To delete some simple things, you can do well with the "operator-action" command. However, in many cases, it is not easy to determine which command can be used to move to the place you want to modify. In this case, you need the visual mode.
You can use the "v" command to start the visual mode. You can move the cursor to the desired place. When you do this, the text in the middle is highlighted. Finally, run the "operator" command.
Select by line
If you want to perform operations on the entire line, you can use the "v" command to start the visual mode. You will find that the entire line is highlighted before you move it. Moving left and right does not have any effect. By moving up and down, you can select multiple rows at a time.
For example, you can use "vjj" to select three rows:

Copy in VIM

I. Copy and past
To copy the text from one place to another, you can delete it, run the "U" command to restore it, and then copy it to another place using "p. There is also a simple method: extract (Yank ). The "Y" command can copy text to registers. Then paste it elsewhere with the "P" command.
Yanking is the name of the Copy command in vim. Since "C" has been used to indicate change, copy cannot be used again. However, "Y" is still available. This command is called "yanking" to make it easier to remember the "y" key.
Because "Y" is an operator, the "yw" Command copies a word. Of course, the Count prefix is also valid. To copy two words, you can use "y2w"
The "YY" Command copies a whole line, just like "DD" deletes a whole line. Unexpectedly, "D" is deleted to the end of the row, while "Y" is a whole row. Pay attention to this difference! Copy "Y $" to the end of the row.
Copy and paste from the clipboard:
Copy a row to the clipboard: "* YY
Paste it back: "* P
The above only works in the vim version that supports clipboard. See more: http://vcd.gro.clinux.org/doc/usr_09.html#09.3

Text objects in VIM

1. Text objects:
Text objects are processed as a whole. The position of the cursor in the object does not matter.
For example, aw represents a Text object where the cursor is located. It is a word. For example:
"D" of "daw" is the delete operator. "Aw" is a text object. Tip: "aw" indicates "a word" (a word). In this way, "daw" is "delete a word" (delete a word ).
Is indicates inner sentence, indicating the row where the cursor is located
"CIS" includes "C" (change, modify) operators and "is" text objects. This indicates "inner sentence" (containing sentences ). Another text object is "as". The difference is that "as" contains white spaces after a sentence, but "is" does not. If you want to delete a sentence and you want to delete the white space after the sentence at the same time, use "das"; if you want to retain the white space and Replace the sentence, use "CIS ".
You can also use text objects in visual mode. This will select a Text object and remain in the visual mode. You can continue to execute the Text object command multiple times. For example, you can start the visual mode with "V" and select a sentence with ". If "as" is repeated, more sentences will be selected. Finally, you can use an operator to process these selected sentences.
For detailed text objects, see http://vcd.gro.clinux.org/doc/motion.html?text-objects:

Simple keyboard ing

1. Map command:
Ing allows you to bind a series of VIM commands to a separate key. Suppose you want to use a braces
Fixed words. For example, convert "amount" to "{amount }". Run the ": Map" command to make F5
To complete the work. The command is as follows:
: Map <F5> I {<ESC> EA} <ESC>
When you enter this command, <F5> must be expressed in four characters. Similarly, input <ESC> instead of simply pressing the <ESC> key, it is entered with five characters.
For example, when writing a CPP file, use the <F5> key to automatically insert {}. You can map the keyboard as follows:
: Map <F5> I {<enter> <enter >}< ESC> Ki <tab>
For another example, the keyboard ing \ I command automatically adds () to the word. You can:
: Map \ I BI (<ESC> EA) <ESC>
With the map command, you can map commonly used commands to the keyboard, and directly call the shortcut key when necessary.

There is also a map command for the visual mode: vmap
For example, add the <small> </small> label to the selected text, and press Ctrl + S.
: Vmap <C-S> C <small> <ESC> Pa </small> <ESC>
Or: vmap <C-S> S <small> <C-R> "</small> <ESC> // Where <C-R>" represents register"
Cancel keyboard ing: vunmap <C-S>

 

Some other commands:

*: Read the string at the cursor and move the cursor to where it appears again.
#: It is similar to the above, but it is looking in the opposite direction.
I: insert it to the left of the Current Character
I: insert at the beginning of the current row
A: insert it to the right of the Current Character
A: insert at the end of the current row
O: Insert a new row under the current row.
O: Insert a new row on the current row.
Vim has a very beautiful keyword auto-completion system. This means that you can enter a part of a long term, and then press a key. Then Vim completes the long term input for you. For example, you have a variable named iamalongandawkwardvarname written inCode. Maybe you don't want to input it one by one every time.
With the keyword AutoComplete function, you only need to enter the first few letters (such as iamal), then press <C-N> (press Ctrl, then press N) or <C-P>. If Vim does not give the words you want, continue to press until you are satisfied, VIM will keep repeating the matching strings it finds.

Use Vim to write code

P: similar to P, but it automatically adjusts the indent of the pasted text to adapt to the position of the current Code. Try it!
%: Matching curly braces, square brackets, and brackets. On the top of a bracket, press %, and the mouse will appear at the other half of the matching bracket.
>>: Indent all selected codes.
<: Similar to the preceding but not indented
GD: arrives at the position where the cursor is located, where the function or variable is defined.
K: Search for the word at the current position of the cursor in man.

Display the tab key:

When the file contains the tab key, you cannot see it. To display it:
: Set list
Control the display style of the tab:
: Set listchars = tab:>-. Now, the tab is displayed as "> ---"
: Set listchars = tab:-. Now, the tab is displayed "-..."
As shown above, the tab display style requires two symbols to be controlled.
For details about how to set listchars, see help 'listchars.

Set the file category:

Set filetype = CPP/set Ft = Perl
Set syntax highlighting:
To use highlight in the current buffer, you need to set the 'syntax 'option:
: Set syntax = on
If you want to highlight a specific file with syntax only, you can use this command:
: Syntax Manual
If you want to completely disable this function:
: Syntax off
Color text may affect the efficiency. If you think the display is slow, you can temporarily turn off this function:
: Syntax clear

 

Use different colors

: Colorscheme evening
"Evening" is the name of the color scheme. There are several alternatives to try. In $ vimruntime/colors
You can find these solutions.

 

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.