VIM + ctags usage (reproduced)
[/Home/Brimmer/src] $ ctags-R
"-R" indicates recursive creation, which includes the source program under all subdirectories under the source code root directory. The "tags" file contains a list of these objects:
L macro defined by # define
L enumerated variable value
L function definition, prototype, and Declaration
L namespace)
L type definition (typedefs)
L variables (including definitions and declarations)
Class, struct, Enum, and Union)
L class, structure, and union member variables or functions
Vim uses this "tags" file to locate the marked objects above. The following describes how to locate these objects:
1) Use the command line. Add the "-T" parameter when running vim, for example:
[/Home/Brimmer/src] $ vim-T foo_BAR
This command will open the file defining "foo_BAR" (variable or function or other), and position the cursor to this line.
2) use the ": Ta" command in the vim Editor, for example:
: Ta foo_BAR
3) the most convenient way is to move the cursor over the variable or function name, and then press "Ctrl-]". Return the original location with "Ctrl-o.
Note: When running vim, you must run it in the directory where the "tags" file is located. Otherwise, run the ": Set tags =" command to set the path of the "tags" file.
To find the "tags" file.
Move cursor in Function
[{Go to the previous "{" in the first column "{"
}] To the next "{" in the first column "{"
{Go to the previous empty line
} Go to the next empty line
GD refers to the definition of the local variable to be transferred to the current cursor
* Go to the next occurrence of the word indicated by the current cursor
# Go To The last occurrence of the word pointed by the current cursor
Vim was created by a computer programmer, so it is not surprising that Vim has many functions to help write programs:
Jump to the location where the identifier is defined and used, and preview the related declaration in another window. In the next chapter
Will introduce more features.
* 29.1 * What are tags used?
A tag is the place where an identifier is defined. An example is the function definition in a C or C ++ program. The tag list can be saved in a Tag file. Vim can use it
Jump from anywhere to the tag, that is, the location where an identifier is defined. Generate a Tag file for all c files in the current directory. Run the following command: ctags
*. C "ctags" is an independent program. It has been installed on most Unix systems. If you have not installed exuberant, you can find "Exuberant ctags" here ":
Http://ctags.sf.net now you can use the following command to jump to a function definition: Tag startlist this command will find the function "startlist", even if the function
The number is in another file. CTRL-] The command jumps to the word label under the current cursor. In this way, browsing C code without a clue will become easier. For example, assume that you are
"Write_block. You can see that it calls the function "write_line ". But what does "write_line" do? Place the cursor in the place where "write_line" is called and then press
CTRL-], and you will jump to the definition of this function. The "write_line" function calls "write_char ".
You need to know what it does. Position the cursor to the place where "write_char" is called, and press Ctrl-] to define "write_char.
+ -- + |
Void write_block (char ** s; int CNT) |
{| Int I; | for (I = 0; I <CNT; ++ I)
| Write_line (s [I]); |} | + ---- | ---- + | Ctrl-] | + ------- ++ -->
| Void write_line (char * s) | |{| | while (* s! = 0) | write_char (* s ++); | |}| +-| ----- + | Ctrl-] | +-++ --> | void
Write_char (char c) |{| | putchar (INT) (unsigned char) C); |}|
+-+ ": Tags" command to display the list of tags you have passed through: tags # To tag from line in file/Text 1 write_line 8 write_block.c 2 1 write_char 7
Write_line.c>
This section describes redirection. The CTRL-T command redirects to the previous tag. In the above example, you will return to the place where the "write_line" function calls "write_char. This command accepts
Count parameters, used to indicate the number of tags that are redirected back. You have already jumped forward, and now you have jumped back. Now let's jump forward again. The following command jumps to the tag list
Top Tag: tag you can add the number of tags to jump forward. For example, ": 3tag ". The CTRL-T can also add a count parameter. With these commands, you can
Jump forward with Ctrl-] extended call tree, jump back with CTRL-T, use the ": tags" command to display the current position. The split window port ": Tag" command replaces the file in the current window.
Is the file that contains the new function. How can I view two files simultaneously? You can use the ": Split" command to separate windows and then use the ": Tag" command. Vim has an abbreviated command.
Do this: stag tagname use the following command to split the current window and jump to the label under the cursor: CTRL-W] If the Count parameter is specified, the new window will contain the specified
Multiple lines. If multiple mark files exist in multiple directories, you can create a Tag file under each directory. Vim can only jump to the label under that directory. Connect
You can use multiple related tag files by setting the 'tags' option. For example: Set tags =./tags,./../tags,./*/tags. This will make Vim find the directory where the current file is located.
And its parent directory and the label files under all subdirectories. This is already a lot of tag files, but it may not be enough. For example, when editing "~ /Proj/src "directory, you
Unable to find "~ The tag file under the/proj/SUB/tags directory. In this case, VIM provides a method to find the Tag file under the entire directory, such as: Set
Tags = ~ /Proj/**/tags a single Tag file. When Vim searches for tag files in multiple locations, you will hear the hard disk grapping. This will be a little slow. In this case
This is a good way to generate a large Tag file. You can wait for a while. You need to use the "Exuberant ctags" program mentioned above. It has an option to search for an integer.
Directory tree: Cd ~ /Proj ctags-R. The advantage of doing so is
"Exuberant ctags" can recognize multiple file types. It is applicable not only to C and C ++ programs, but also to Eifel and even Vim scripts. Please refer to the ctags document for Adjustment
Parameters used. Now you only need to tell Vim where your tag file is: Set tags = ~ /Proj/tags multiple matches when a function (or method in the class) is defined multiple times,
The ": tags" command jumps to the first place. If a match exists in the current file, it will be used first. Now you can jump to another matching position of the same tag: tnext
Execute this command to find more matches. If there are many matches, you can select which one to jump:
: Tselect tagnamevim will show you a selection list: # pri kind Tag file 1 f mch_init OS _amiga.c mch_init () 2 f mch_init
OS _mac.c mch_init () 3 F mch_init OS _msdos.c mch_init (void) 4 f
Mch_init OS _riscos.c mch_init () Enter NR of choice (<CR>
To abort): You can enter the matching code to jump to (in the first column ). The information of other columns allows you to know where the matching is defined. You can use these commands
Move Between tags: tfirst to the first match: [count] tprevious forward [count] Match: [count] tnext backward [count] Match: tlast to the last match
If not specified, [count] is saved as one.
Command Line completion is a good way to avoid entering the long sign signature. Enter the start part and press <tab>: Tag write _ <tab> to obtain the first match. For example
If this is not what you want, repeat <tab> until you find the correct match. Sometimes you only know a part of a function name, or you have many
The same tag. In this case, you can tell Vim to use a mode to find tags. Suppose you want to jump to a tag containing "Block. First, enter: tag/block for reuse now
Command Line completion function: Enter <tab>. Vim finds all tags that contain "Block" and uses the first match. "/" Before the label name tells Vim that this is not a definite label
The signature is a mode. You can use all the features related to the search mode. For example, if you want to select all the labels starting with "Write _": tselect/^ write _ "^"
The specified tag starts with "Write _". Otherwise, the tag name containing "Write _" in the middle will also be found. Similarly, "___ fckpd ___ 0 quot; specifies the matching at the end of the tag name.
The tab viewer Ctrl-] allows you to jump to the definition of the identifier where the cursor is located, so you can use the identifier list to form a directory.
Here is an example. First, generate an identifier list (requires exuberant ctags): ctags -- c-types = f-f functions *. C now open vim and
Edit this file in the direct split window: The VIM: vsplit functions window contains a list of all functions. Other things can be ignored. Run the ": setlocal Ts = 99" command
Make it clearer. In this window, define a mapping: nnoremap <buffer> <CR> 0ye <C-W> W: tag <C-R> "<CR> move the cursor to the function to jump
And enter <enter>. Vim will jump to the selected function definition in another window. You can set the 'ignorecase' option to ignore
Case. The 'tagsearch' option indicates whether tag files are sorted. Saving is assumed that the Tag file has been sorted, which will make the search faster, but if the file is not sorted, it cannot work.
. The 'taglength' option can be used to indicate the number of valid characters of a label in Vim. When you use the sniff + program, you can use the vim interface | sniff |.
Sniff + is a commercial software. Cscope is a free software. It not only finds the place where an identifier is declared, but also the place where the identifier is used. See
| Cscope |.
---
* 29.2 * in the preview window, when editing code containing function calls, you need to use the correct call parameters.
To know the value to be passed, you can view how this function is defined. The label mechanism is very applicable to this. It would be better if the definition can be displayed in another window. We can
To use the preview window. Open a preview window to display the function "write_char": ptag write_charvim. A window is opened to jump to the "write_char" tab. Then
It will return to the original position. This way you can continue typing without using the CTRL-W command. If the function name appears in the text, you can use the following command to get its
Righteousness: CTRL-W} has a script that automatically displays tag definitions at the cursor. See | cursorhold-example |. Run the following command to close the preview window: pclose needs to be previewed
In the window, edit a specified file with ": pedit ". This is useful when editing header files, such as: pedit defs. H. Finally, "psearch" can be used to find the current file and any
Contains the words in the file and displays a match in the preview window. This is useful when using library functions without tag files. For example, psearch popen will display
Popen () prototype "stdio. H "file: file * popen _ p (const char *, const char *); you can use the 'previewheight' option to specify that the preview window opens.
.
---
* 29.3 * move between codes because the program code is structured, VIM can identify related projects.
Some specific commands can be used to complete the relevant movement. C programs often contain code similar to the following: # ifdef use_popen FD = popen ("ls", "R") # else FD =
Fopen ("tmp", "W") # endif is sometimes longer, and may be nested.
Place the cursor at "# ifdef" and press %. Vim jumps to "# else ". If you press % again, the page will jump to "# endif ". Press % again and return to the original "# ifdef ". When the code is embedded
, VIM will find the matched project. This is a good way to check whether you forget "# endif.
When you are in a "# ifdef"-"# endif" block, you can use the following command to return to the start position: [# If your location is not after "# If" or "# ifdef,
Vim sounds.
Use the following command to jump to the next "# else" or "# endif":] # these two Commands will skip the "# If"-"# endif" block that it passes. Example: # If defined
(Has_inc_h)
A = a + Inc ();
# Ifdef use_theme
A + = 3;
# Endif
Set_width ();
If the cursor is on the last line, "[#" moves to the first line. The "# ifdef"-"# endif" block in the middle is skipped. Move the C code block inside the code block and include it in {}. Sometimes
A code will be long. To jump to the beginning of an external code block, run the "[" command. Use "] [" to find the end. (The premise is that "{" and "}" are both in the first column .) "[{" Command jump
To the beginning of the current code block. It skips the {} pairs of the same level. "]}" Jump to the end. An overview: function (int A) +-> {| if (a) | +-> {[|
For (;) -- + | +-> {| [{| Foo (32); | -- + | [{| if
(Bar (A) -- + |]} | + -- break; |]} | |}<-+ |] [+ --
Foobar (a) ||}<-+ |}<-+ when writing C ++ or Java code, the external code block is a class, and the next {} is a method. You can use "[M" in the class to find the previous method.
. "] M" will find the start of the next method. In addition, "[]" moves backward to the end of a function, "]" moves forward to the end of a function. The function ends
"}" In the first column "}". Int func1 (void) {return 1; + --- >}| [] | int func2 (void) | +-> {| [[| if (FLAG) start + -- Return flag;
|] [| Return 2; | +->}] | int func3 (void) + ---> {return 3 ;} do not forget that you can also use "%" to move between matched (), {}, and. In
They are still applicable when they are separated by many rows. The "[(" and "])" command "[}" and "]}" are moved within the period, except that they apply to () instead of {} pairs. [(<----
<------- If (a = B & (C = d | (E> F ))&&
X> Y) -------> ---->]) move between the annotations to start with a comment and move forward "[/";
Use "]/" at the end of the comment. This is only valid for/*-*/annotations. +->/* | [/| * A comment about -- + [/| + -- * wonderful life. |]/| */
<-+ | + -- Foo = bar * 3; -- + |] // * a short comment */<-+
---
* 29.4 * search for the GLobal IDEntifier you are editing a C program and want to know whether a variable is declared as "int" or "unsigned ".
A quick way is to use the "[I" command for search.
Assume that the cursor is at the word "column. Input: [ivim will list the matching rows it finds, not only in the current file, but also in all contained files. The result is as follows:
As shown in:
Structs. H 1: 29 unsigned column;/* column number */the advantage of using the label file or preview window is that the inclusion file is also searched. In most cases
To the correct statement. Even if the Tag file has expired or you have not created a tag for the included file, the result will not be affected. But some preparations are necessary. Otherwise, "[I" won't work.
. First, the 'include 'option must specify how the file is contained. The saved value applies to C and C ++. For other languages, you need to set them by yourself. Fixed-bit package with file Vim
The contained file in the path specified by the 'path' option is found. If a directory is missing, some contained files will not be found. You can use this command to view: checkpath, which will display Columns
Contains files that cannot be found, and the files that can be found. An output example:
--- Specified ded files not found in path ---
<Io. h> Vim. h -->
<Functions. h> <cLib/exec_protos.h>
The file "Io. H" is included in the current file but cannot be found. "Vim. H" can be found. In this way, ": checkpath" follows up with this file and checks its inclusion files. The result cannot be found.
"Vim. H" contains the "functions. H" and "cLib/exec_protos.h" files.
Note: Vim is not a compiler. It cannot recognize the "# ifdef" statement. This means that all the "# include" statements will be used, even after "# If never.
Add a directory to the 'path' option to correct the file failure. A good reference is makefile. Note the entries that contain "-I", such "-
I/usr/local/X11 ".
To add this directory, use: set path + =/usr/local/X11. If there are many subdirectories, you can use the "*" wildcard.
For example:
: Set path + =/usr/*/include this will find the files under the "/usr/local/include" and "/usr/X11/include" directories. If all the files contained in your project are
"**" Is very useful under an embedded directory tree. It searches all subdirectories. For example: set path + =/projects/invent/**/include.
File:/projects/invent/include/projects/invent/main/OS/include etc. There are other possibilities. More information
, See the 'path' option.
If you want to view the contained files, run the following command: checkpath! You will get a (very long) List of contained files. To make it shorter, VIM will
Show "(already listed)" instead of re-displaying it again. Jump to the match "[I" to generate a list with only one line of text for each item. If you want to further view the first item,
You can jump with this command: [<tab> you can also use "[CTRL-I" because the CTRL-I works the same way as pressing <tab>. The list generated by "[I" has an order at the beginning of each line.
. If you want to jump to other items other than the first item, first enter the serial number: 3 [<tab> to jump to the third item in the list. Remember that you can jump back to the original place with a CTRL-O. Correlation
Life order [I only list the first match] I only list the items under the cursor] I only list the first match under the cursor to find the macro definition mark "[I" command find any identifier
. Only search for macros defined by "# define", and use: [D, which will be searched in all included files. The 'delimiter' option specifies the predefined style that "[d" looks. You need to change
It is applicable to languages other than C or C ++. "[D" related commands: [d only lists the first match] d only lists the items under the cursor] d only lists the first match under the cursor
---
* 29.5 * run the "[I" command to find all contained files. To search for and jump to the place where the word is first used at the cursor in the current file, use:
GD prompt: goto definition. This command is useful for finding variables or functions declared in the local ("static" in C language. For example (the cursor is at "counter"): +->
Static int counter = 0; | int get_counter (void) Gd | {| ++ counter; + -- return counter;} to further narrow the search range, only in the current function
Search within a few minutes. Use this command: Gd to return to the beginning of the current function to find the first place where the word appears at the cursor. In fact, it finds an empty value above the first column '{'.
And then look for the Identifier from there. For example, (the cursor is on idx): int find_entry (char * Name) {+-> int idx; | GD | for (idx = 0; idx
<Table_len; ++ idx) | if (strcmp (Table [idx]. Name, name) = 0) + -- Return idx ;}