My Vim skill set

Source: Internet
Author: User
The usual Vim skills are recorded on the blog to facilitate searching.

In vim, move the cursor to the first word of the next word, use the command "W" to move the cursor to the first word of the previous word, and use the command "B "; move the cursor to the end of the next word, run the "e" command to move the cursor to the end of the previous word, and run the command "Ge ".

The preceding commands use characters in the 'iskeyword' option to determine the word boundary. There are several other commands that only use blank characters as the "word" boundary. Of course, the "word" mentioned here is no longer a traditional word, but a string consisting of non-blank characters. Command "W" to move the cursor to the start of the next string, command "B" to the start of the previous string; command "E" to the end of the next string, the command "Ge" moves to the end of the previous string.

The h/m/L key enables the cursor to jump to the top, middle, and bottom of the current window and stay on the first non-null character. You can add a number before the H and l commands, but the number does not mean a multiple, but the number of rows from the top and bottom of the window. For example, "3 H" indicates that the cursor moves to the position of 3rd rows from the top of the window; "5l" indicates that the cursor moves to the position of 5 rows from the bottom of the window.

When reading the code, we sometimes need to scroll the screen based on the position of the cursor, move the row where the cursor is located to the top, middle, or bottom of the window, "ZT", "ZZ", and "ZB" can be used ". Compared with page flip, this scrolling method has the advantage that you can always use the current cursor position as a reference and will not find yourself lost after page flip several times.
Pai_^
**************************************** ****************************************

1.
Select multiple files and open them with vim. Only one window is used. All files are listed in the buffer. (This is a method found in windows. I used TC + f4menu, in the f4menu setting, call Vim's "open mode" option and select "open all files in list mode". The selected files are read into the buffer, but only one is displayed in the window ),
Gvim -- remote-tab-silent
If you add the parameter-P to the command line, you can open the files on multiple tab pages, such as vim-P.
File1.c file2.c

2. How does Vim display hexadecimal notation?
: %! Xxd

3. How to paste the content in the desktop clipboard in Vim?
For example, paste the content I copied in opera into vim.
"* P
"+ P

4. How can I jump to the previous line instead of the previous line when I use wrap? As in notepad?
Add a G, such as GJ, GK .....
D: \ soft

5. Can Vim be copied and pasted on the command line?
CTRL + R "or Ctrl + R Ctrl + W can be pasted to the command line"
You can copy the editing area to the command line first, such as register a, and then press Ctrl-r a on the command line.

6. How to sort rows in reverse order
For example
A
C
B
Change
B
C
A
Answer: G/./M0
Explanation:
: H: m
M-> move <address>
M0-> move the matching row before the first row

This is a tip in the Vim Help document.
: H 12.4

7. How to sort the text naturally.
For example
D
A
C
B
Arrange
A
B
C
D
Answer: Sort

8. How do I delete rows containing some content?
For example, how can I delete a row containing the console?
: G/console/d

9. How do I delete the five rows above a row?
: G/string/norm d5k
: G/search content/normal d5k

10. How can I insert an empty row at intervals of X rows?
A:
Suppose every five rows,
Qa4jo <ESC> JQ
Then @.
Or:
: % S/\ (. * \ n \) \ {3}/\ 0 \ r/g
3. Change to the number you want.

11. How to add comments using Vim Column Operations
Select a column with C-V, input uppercase I or uppercase A, then enter comment, and then ESC

12. Operations on multiple files are actually super simple
O newfilename
: BN buffer next
: BP buffer pervious
: Bn the list number N of the buffers
: SP newfile // open a newfile with a splited window
: VS newfile // open a newfile with a vertical splited window
[CTRL] + J // down window (same with the key J)
[CTRL] + K // up window (same with the key K)
[CTRL] + H // left window (same with the key H)
[CTRL] + L // right window (same with the key L)

13. Vim Motion
With vim for such a long time, when you see the following sentence, you can improve your understanding of vim.
CI [deletes all the characters in a pair of [] and enters the insert mode
Ci (delete all the characters in a pair of () and enter the insert mode
CI <deletes all the characters in a pair <> and enters the insert mode
CI {Delete All characters in a pair {} and enter the insert mode
CIT deletes all characters in an HTML/xml tag and enters the insert mode.
Ci "Ci 'ci' deletes all the characters in a pair of quotation marks (" or ') and enters the insertion Mode

14. count the number of keywords
: % S/pattern/&/g
& Indicates the string to be compared. Therefore, this command does not change the file itself. However, VIM will tell you how many strings are replaced from the number of rows. It is easy to use a line of command to solve this problem.
Enter a command with only one line: The result will appear on the last line and will not change to the file itself.

15. Search for keywords
You can press "*" to select a keyword. However, VIM searches for the boundary based on the blank space, such as "{" and 「{」,「(
These symbols are determined, so for Chinese, this function may not be so useful. You can use "v" to select and copy "Y. When searching, after "/", Press <
CTRL-r> followed by the number key 0, the string copied to register will appear. The command is as follows:
/<Ctrl-r> 0 <CR>
In Vim's register, register 0 is the default temporary register used for deletion or Yank action.
However, we can see that if we use "*" for search, the front and back of the string will be "\ <\>
. For example, if you are looking for \ <link \>, then, min_links
It does not match. Similarly, linknode does not. This is to say "\ <
"Indicates the header of a word, and" \>
"Represents the end of a word. When you write the \ <link, it indicates that the word you are looking for starts with the link. Similarly, link \> is the word ending with Link. Therefore, when two clips are used, it means that the string is indeed the word you want.

Now that "*" is available, you can find it back. Press 「#」
If you are too reluctant to remember so many words, use "*" and match "N!

At this point, you may think that if I don't want to play so many words, but I don't want to do the same thing, what should I do?
Vim also has something like this, that is, before you play "*", add a "g", that is
G *
In this way, VIM does not include the \ <\> items before and after the search. Similarly 「#
"Can also be changed to" G #」

16. View symbol Definitions
VI provides functions like Win32 IDE, allowing you to easily view function prototypes, structure declarations, and macro definitions. However, its functions are weaker than those of others. However, some other techniques can fully meet your needs. To use this function, you need to make some settings:
1.
Install the ctags package. You can find the installation disc, or download the source code package online and compile it yourself.
2. Generate the tags file. Go to the directory where your source code is located and run ctags
-R command, which will create an index for the source program in the current directory and subdirectory, and create a tags file under the current directory, which stores the symbolic index information.
3.
Set the tags path. In the vi startup script file (generally ~ /. Vimrc), tell VI where to find the tags file, usually try to use the relative path. For example: Set
Tags =./tags,.../tags
4. Jump to the specified symbol. <Ctrl> +]
You can jump to the definition of the symbol where the cursor is located.
5. Return to the original location. <Ctrl> + T or <Ctrl> + O can return the original position.

17. view the help of system functions.
Vi can also easily jump to the help of system functions like VC. The principle is very simple. Just run the external command man in VI :! Man
Fopen. This may cause some trouble. VI provides a quick method: <shift> +
K can jump to the help of the symbol where the cursor is located.

18. automatically locate compilation errors
In VC, if a compilation error occurs during compilation, double-click the error message, and the Editor automatically switches to the location where the error occurs, isn't it convenient? In fact, you don't need to envy it. VI also has this function. In VI, after running the make command, if there is a compilation error, press enter and VI will automatically locate the first compilation error. Remember to use the built-in make command, that is, run: make, not an external command :! Make.

19. automatically locate the search results
When writing a program, we often want to know where a function is used. What should we do? In shell, you can use grep to search and open the corresponding file, you can see the relevant context information. But this is more troublesome. VI has a built-in grep command, which is very convenient to use:
1. Search. The usage is the same as that of grep in shell.
2. Jump to the first search result. Just press Enter.
3.
Lists All search results. : The CL command can list all the search results. Each result has a number.
4. Jump to the file of a search result. : CC <No.>
Command to jump to the file with the specified number to find the result. <number> is the number listed in the CC column.
5. Return to the original location. <Ctrl> + O can return the original location.

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.