blob: cf2aff2bcc48a472904ada6c2085c330f9b5d20f [file] [log] [blame]
Bram Moolenaarb1c91982018-05-17 17:04:55 +02001*if_cscop.txt* For Vim version 8.1. Last change: 2018 Jan 21
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Andy Kahn
5
6 *cscope* *Cscope*
7This document explains how to use Vim's cscope interface.
8
9Cscope is a tool like ctags, but think of it as ctags on steroids since it
10does a lot more than what ctags provides. In Vim, jumping to a result from
11a cscope query is just like jumping to any tag; it is saved on the tag stack
12so that with the right keyboard mappings, you can jump back and forth between
13functions as you normally would with |tags|.
14
151. Cscope introduction |cscope-intro|
162. Cscope related commands |cscope-commands|
173. Cscope options |cscope-options|
184. How to use cscope in Vim |cscope-howtouse|
195. Limitations |cscope-limitations|
206. Suggested usage |cscope-suggestions|
217. Availability & Information |cscope-info|
22
23This is currently for Unix and Win32 only.
24{Vi does not have any of these commands}
25
26==============================================================================
271. Cscope introduction *cscope-intro*
28
29The following text is taken from a version of the cscope man page:
30
31 -----
32
33 Cscope is an interactive screen-oriented tool that helps you:
34
35 Learn how a C program works without endless flipping through a thick
36 listing.
37
38 Locate the section of code to change to fix a bug without having to
39 learn the entire program.
40
41 Examine the effect of a proposed change such as adding a value to an
42 enum variable.
43
44 Verify that a change has been made in all source files such as adding
45 an argument to an existing function.
46
47 Rename a global variable in all source files.
48
49 Change a constant to a preprocessor symbol in selected lines of files.
50
51 It is designed to answer questions like:
52 Where is this symbol used?
53 Where is it defined?
54 Where did this variable get its value?
55 What is this global symbol's definition?
56 Where is this function in the source files?
57 What functions call this function?
58 What functions are called by this function?
59 Where does the message "out of space" come from?
60 Where is this source file in the directory structure?
61 What files include this header file?
62
63 Cscope answers these questions from a symbol database that it builds the
64 first time it is used on the source files. On a subsequent call, cscope
65 rebuilds the database only if a source file has changed or the list of
66 source files is different. When the database is rebuilt the data for the
67 unchanged files is copied from the old database, which makes rebuilding
68 much faster than the initial build.
69
70 -----
71
72When cscope is normally invoked, you will get a full-screen selection
73screen allowing you to make a query for one of the above questions.
74However, once a match is found to your query and you have entered your
75text editor to edit the source file containing match, you cannot simply
76jump from tag to tag as you normally would with vi's Ctrl-] or :tag
77command.
78
79Vim's cscope interface is done by invoking cscope with its line-oriented
80interface, and then parsing the output returned from a query. The end
81result is that cscope query results become just like regular tags, so
82you can jump to them just like you do with normal tags (Ctrl-] or :tag)
83and then go back by popping off the tagstack with Ctrl-T. (Please note
84however, that you don't actually jump to a cscope tag simply by doing
85Ctrl-] or :tag without remapping these commands or setting an option.
86See the remaining sections on how the cscope interface works and for
87suggested use.)
88
89
90==============================================================================
912. Cscope related commands *cscope-commands*
92
93 *:cscope* *:cs* *:scs* *:scscope* *E259* *E262* *E561* *E560*
Bram Moolenaar74675a62017-07-15 13:53:23 +020094All cscope commands are accessed through suboptions to the cscope commands.
95 `:cscope` or `:cs` is the main command
96 `:scscope` or `:scs` does the same and splits the window
97 `:lcscope` or `:lcs` uses the location list, see |:lcscope|
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99The available subcommands are:
100
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200101 *E563* *E564* *E566* *E568* *E622* *E623* *E625*
102 *E626* *E609*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 add : Add a new cscope database/connection.
104
105 USAGE :cs add {file|dir} [pre-path] [flags]
106
107 [pre-path] is the pathname used with the -P command to cscope.
108
109 [flags] are any additional flags you want to pass to cscope.
110
111 EXAMPLES >
112 :cscope add /usr/local/cdb/cscope.out
113 :cscope add /projects/vim/cscope.out /usr/local/vim
114 :cscope add cscope.out /usr/local/vim -C
115<
Bram Moolenaar4a748032010-09-30 21:47:56 +0200116 *cscope-find* *cs-find* *E567*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 find : Query cscope. All cscope query options are available
118 except option #5 ("Change this grep pattern").
119
120 USAGE :cs find {querytype} {name}
121
122 {querytype} corresponds to the actual cscope line
123 interface numbers as well as default nvi commands:
124
125 0 or s: Find this C symbol
126 1 or g: Find this definition
127 2 or d: Find functions called by this function
128 3 or c: Find functions calling this function
129 4 or t: Find this text string
130 6 or e: Find this egrep pattern
131 7 or f: Find this file
132 8 or i: Find files #including this file
Bram Moolenaar802a0d92016-06-26 16:17:58 +0200133 9 or a: Find places where this symbol is assigned a value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000135 For all types, except 4 and 6, leading white space for {name} is
136 removed. For 4 and 6 there is exactly one space between {querytype}
137 and {name}. Further white space is included in {name}.
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 EXAMPLES >
140 :cscope find c vim_free
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000141 :cscope find 3 vim_free
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142<
Bram Moolenaar80b6a0e2009-03-18 13:32:24 +0000143 These two examples perform the same query: functions calling
144 "vim_free". >
145
146 :cscope find t initOnce
147 :cscope find t initOnce
148<
149 The first one searches for the text "initOnce", the second one for
150 " initOnce". >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152 :cscope find 0 DEFAULT_TERM
153<
154 Executing this example on the source code for Vim 5.1 produces the
155 following output:
156
157 Cscope tag: DEFAULT_TERM
158 # line filename / context / line
159 1 1009 vim-5.1-gtk/src/term.c <<GLOBAL>>
160 #define DEFAULT_TERM (char_u *)"amiga"
161 2 1013 vim-5.1-gtk/src/term.c <<GLOBAL>>
162 #define DEFAULT_TERM (char_u *)"win32"
163 3 1017 vim-5.1-gtk/src/term.c <<GLOBAL>>
164 #define DEFAULT_TERM (char_u *)"pcterm"
165 4 1021 vim-5.1-gtk/src/term.c <<GLOBAL>>
166 #define DEFAULT_TERM (char_u *)"ansi"
167 5 1025 vim-5.1-gtk/src/term.c <<GLOBAL>>
168 #define DEFAULT_TERM (char_u *)"vt52"
169 6 1029 vim-5.1-gtk/src/term.c <<GLOBAL>>
170 #define DEFAULT_TERM (char_u *)"os2ansi"
171 7 1033 vim-5.1-gtk/src/term.c <<GLOBAL>>
172 #define DEFAULT_TERM (char_u *)"ansi"
173 8 1037 vim-5.1-gtk/src/term.c <<GLOBAL>>
174 # undef DEFAULT_TERM
175 9 1038 vim-5.1-gtk/src/term.c <<GLOBAL>>
176 #define DEFAULT_TERM (char_u *)"beos-ansi"
177 10 1042 vim-5.1-gtk/src/term.c <<GLOBAL>>
178 #define DEFAULT_TERM (char_u *)"mac-ansi"
179 11 1335 vim-5.1-gtk/src/term.c <<set_termname>>
180 term = DEFAULT_TERM;
181 12 1459 vim-5.1-gtk/src/term.c <<set_termname>>
182 if (STRCMP(term, DEFAULT_TERM))
183 13 1826 vim-5.1-gtk/src/term.c <<termcapinit>>
184 term = DEFAULT_TERM;
185 14 1833 vim-5.1-gtk/src/term.c <<termcapinit>>
186 term = DEFAULT_TERM;
187 15 3635 vim-5.1-gtk/src/term.c <<update_tcap>>
188 p = find_builtin_term(DEFAULT_TERM);
189 Enter nr of choice (<CR> to abort):
190
191 The output shows several pieces of information:
192 1. The tag number (there are 15 in this example).
193 2. The line number where the tag occurs.
194 3. The filename where the tag occurs.
195 4. The context of the tag (e.g., global, or the function name).
196 5. The line from the file itself.
197
198 help : Show a brief synopsis.
199
200 USAGE :cs help
201
Bram Moolenaar4a748032010-09-30 21:47:56 +0200202 *E261*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 kill : Kill a cscope connection (or kill all cscope connections).
204
205 USAGE :cs kill {num|partial_name}
206
207 To kill a cscope connection, the connection number or a partial
208 name must be specified. The partial name is simply any part of
209 the pathname of the cscope database. Kill a cscope connection
210 using the partial name with caution!
211
212 If the specified connection number is -1, then _ALL_ cscope
213 connections will be killed.
214
215 reset : Reinit all cscope connections.
216
217 USAGE :cs reset
218
219 show : Show cscope connections.
220
221 USAGE :cs show
222
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000223 *:lcscope* *:lcs*
224This command is same as the ":cscope" command, except when the
225'cscopequickfix' option is set, the location list for the current window is
226used instead of the quickfix list to show the cscope results.
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 *:cstag* *E257* *E562*
229If you use cscope as well as ctags, |:cstag| allows you to search one or
230the other before making a jump. For example, you can choose to first
231search your cscope database(s) for a match, and if one is not found, then
232your tags file(s) will be searched. The order in which this happens
233is determined by the value of |csto|. See |cscope-options| for more
234details.
235
236|:cstag| performs the equivalent of ":cs find g" on the identifier when
237searching through the cscope database(s).
238
239|:cstag| performs the equivalent of |:tjump| on the identifier when searching
240through your tags file(s).
241
242
243==============================================================================
2443. Cscope options *cscope-options*
245
246Use the |:set| command to set all cscope options. Ideally, you would do
247this in one of your startup files (e.g., .vimrc). Some cscope related
248variables are only valid within |.vimrc|. Setting them after vim has
249started will have no effect!
250
251 *cscopeprg* *csprg*
252'cscopeprg' specifies the command to execute cscope. The default is
253"cscope". For example: >
254 :set csprg=/usr/local/bin/cscope
255<
256 *cscopequickfix* *csqf* *E469*
257{not available when compiled without the |+quickfix| feature}
258'cscopequickfix' specifies whether to use quickfix window to show cscope
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259results. This is a list of comma-separated values. Each item consists of
Bram Moolenaar802a0d92016-06-26 16:17:58 +0200260|cscope-find| command (s, g, d, c, t, e, f, i or a) and flag (+, - or 0).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261'+' indicates that results must be appended to quickfix window,
262'-' implies previous results clearance, '0' or command absence - don't use
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000263quickfix. Search is performed from start until first command occurrence.
264The default value is "" (don't use quickfix anyway). The following value
Bram Moolenaar009b2592004-10-24 19:18:58 +0000265seems to be useful: >
Bram Moolenaar802a0d92016-06-26 16:17:58 +0200266 :set cscopequickfix=s-,c-,d-,i-,t-,e-,a-
Bram Moolenaar009b2592004-10-24 19:18:58 +0000267<
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 *cscopetag* *cst*
Bram Moolenaar251e1912011-06-19 05:09:16 +0200269If 'cscopetag' is set, the commands ":tag" and CTRL-] as well as "vim -t"
270will always use |:cstag| instead of the default :tag behavior. Effectively,
271by setting 'cst', you will always search your cscope databases as well as
272your tag files. The default is off. Examples: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 :set cst
274 :set nocst
275<
Bram Moolenaar2f982e42011-06-12 20:42:22 +0200276 *cscoperelative* *csre*
Bram Moolenaar251e1912011-06-19 05:09:16 +0200277If 'cscoperelative' is set, then in absence of a prefix given to cscope
278(prefix is the argument of -P option of cscope), basename of cscope.out
279location (usually the project root directory) will be used as the prefix
280to construct an absolute path. The default is off. Note: This option is
281only effective when cscope (cscopeprg) is initialized without a prefix
282path (-P). Examples: >
Bram Moolenaar2f982e42011-06-12 20:42:22 +0200283 :set csre
284 :set nocsre
285<
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 *cscopetagorder* *csto*
287The value of 'csto' determines the order in which |:cstag| performs a search.
288If 'csto' is set to zero, cscope database(s) are searched first, followed
289by tag file(s) if cscope did not return any matches. If 'csto' is set to
290one, tag file(s) are searched before cscope database(s). The default is zero.
291Examples: >
292 :set csto=0
293 :set csto=1
294<
295 *cscopeverbose* *csverb*
296If 'cscopeverbose' is not set (the default), messages will not be printed
297indicating success or failure when adding a cscope database. Ideally, you
298should reset this option in your |.vimrc| before adding any cscope databases,
299and after adding them, set it. From then on, when you add more databases
300within Vim, you will get a (hopefully) useful message should the database fail
301to be added. Examples: >
302 :set csverb
303 :set nocsverb
304<
305 *cscopepathcomp* *cspc*
306The value of 'cspc' determines how many components of a file's path to
307display. With the default value of zero the entire path will be displayed.
308The value one will display only the filename with no path. Other values
309display that many components. For example: >
310 :set cspc=3
311will display the last 3 components of the file's path, including the file
312name itself.
313
314==============================================================================
3154. How to use cscope in Vim *cscope-howtouse*
316
317The first thing you need to do is to build a cscope database for your
318source files. For the most basic case, simply do "cscope -b". Please
319refer to the cscope man page for more details.
320
321Assuming you have a cscope database, you need to "add" the database to Vim.
322This establishes a cscope "connection" and makes it available for Vim to use.
323You can do this in your .vimrc file, or you can do it manually after starting
324vim. For example, to add the cscope database "cscope.out", you would do:
325
326 :cs add cscope.out
327
328You can double-check the result of this by executing ":cs show". This will
329produce output which looks like this:
330
331 # pid database name prepend path
332 0 28806 cscope.out <none>
333
334Note:
335Because of the Microsoft RTL limitations, Win32 version shows 0 instead
336of the real pid.
337
338Once a cscope connection is established, you can make queries to cscope and
339the results will be printed to you. Queries are made using the command
340":cs find". For example:
341
342 :cs find g ALIGN_SIZE
343
344This can get a little cumbersome since one ends up doing a significant
345amount of typing. Fortunately, there are ways around this by mapping
346shortcut keys. See |cscope-suggestions| for suggested usage.
347
348If the results return only one match, you will automatically be taken to it.
349If there is more than one match, you will be given a selection screen to pick
350the match you want to go to. After you have jumped to the new location,
351simply hit Ctrl-T to get back to the previous one.
352
353
354==============================================================================
3555. Limitations *cscope-limitations*
356
357Cscope support for Vim is only available on systems that support these four
358system calls: fork(), pipe(), execl(), waitpid(). This means it is mostly
359limited to Unix systems.
360
361Additionally Cscope support works for Win32. For more information and a
362cscope version for Win32 see:
363
364 http://iamphet.nm.ru/cscope/index.html
365
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000366The DJGPP-built version from http://cscope.sourceforge.net is known to not
367work with Vim.
368
Bram Moolenaar9fa49da2009-07-10 13:11:26 +0000369Hard-coded limitation: doing a |:tjump| when |:cstag| searches the tag files
370is not configurable (e.g., you can't do a tselect instead).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
372==============================================================================
3736. Suggested usage *cscope-suggestions*
374
375Put these entries in your .vimrc (adjust the pathname accordingly to your
376setup): >
377
378 if has("cscope")
379 set csprg=/usr/local/bin/cscope
380 set csto=0
381 set cst
382 set nocsverb
383 " add any database in current directory
384 if filereadable("cscope.out")
385 cs add cscope.out
386 " else add database pointed to by environment
387 elseif $CSCOPE_DB != ""
388 cs add $CSCOPE_DB
389 endif
390 set csverb
391 endif
392
393By setting 'cscopetag', we have effectively replaced all instances of the :tag
394command with :cstag. This includes :tag, Ctrl-], and "vim -t". In doing
395this, the regular tag command not only searches your ctags generated tag
396files, but your cscope databases as well.
397
398Some users may want to keep the regular tag behavior and have a different
399shortcut to access :cstag. For example, one could map Ctrl-_ (underscore)
400to :cstag with the following command: >
401
402 map <C-_> :cstag <C-R>=expand("<cword>")<CR><CR>
403
404A couple of very commonly used cscope queries (using ":cs find") is to
405find all functions calling a certain function and to find all occurrences
406of a particular C symbol. To do this, you can use these mappings as an
407example: >
408
409 map g<C-]> :cs find 3 <C-R>=expand("<cword>")<CR><CR>
410 map g<C-\> :cs find 0 <C-R>=expand("<cword>")<CR><CR>
411
412These mappings for Ctrl-] (right bracket) and Ctrl-\ (backslash) allow you to
413place your cursor over the function name or C symbol and quickly query cscope
414for any matches.
415
416Or you may use the following scheme, inspired by Vim/Cscope tutorial from
417Cscope Home Page (http://cscope.sourceforge.net/): >
418
419 nmap <C-_>s :cs find s <C-R>=expand("<cword>")<CR><CR>
420 nmap <C-_>g :cs find g <C-R>=expand("<cword>")<CR><CR>
421 nmap <C-_>c :cs find c <C-R>=expand("<cword>")<CR><CR>
422 nmap <C-_>t :cs find t <C-R>=expand("<cword>")<CR><CR>
423 nmap <C-_>e :cs find e <C-R>=expand("<cword>")<CR><CR>
424 nmap <C-_>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
425 nmap <C-_>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
426 nmap <C-_>d :cs find d <C-R>=expand("<cword>")<CR><CR>
Bram Moolenaar802a0d92016-06-26 16:17:58 +0200427 nmap <C-_>a :cs find a <C-R>=expand("<cword>")<CR><CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
429 " Using 'CTRL-spacebar' then a search type makes the vim window
430 " split horizontally, with search result displayed in
431 " the new window.
432
433 nmap <C-Space>s :scs find s <C-R>=expand("<cword>")<CR><CR>
434 nmap <C-Space>g :scs find g <C-R>=expand("<cword>")<CR><CR>
435 nmap <C-Space>c :scs find c <C-R>=expand("<cword>")<CR><CR>
436 nmap <C-Space>t :scs find t <C-R>=expand("<cword>")<CR><CR>
437 nmap <C-Space>e :scs find e <C-R>=expand("<cword>")<CR><CR>
438 nmap <C-Space>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
439 nmap <C-Space>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
440 nmap <C-Space>d :scs find d <C-R>=expand("<cword>")<CR><CR>
Bram Moolenaar802a0d92016-06-26 16:17:58 +0200441 nmap <C-Space>a :scs find a <C-R>=expand("<cword>")<CR><CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443 " Hitting CTRL-space *twice* before the search type does a vertical
444 " split instead of a horizontal one
445
446 nmap <C-Space><C-Space>s
447 \:vert scs find s <C-R>=expand("<cword>")<CR><CR>
448 nmap <C-Space><C-Space>g
449 \:vert scs find g <C-R>=expand("<cword>")<CR><CR>
450 nmap <C-Space><C-Space>c
451 \:vert scs find c <C-R>=expand("<cword>")<CR><CR>
452 nmap <C-Space><C-Space>t
453 \:vert scs find t <C-R>=expand("<cword>")<CR><CR>
454 nmap <C-Space><C-Space>e
455 \:vert scs find e <C-R>=expand("<cword>")<CR><CR>
456 nmap <C-Space><C-Space>i
457 \:vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
458 nmap <C-Space><C-Space>d
459 \:vert scs find d <C-R>=expand("<cword>")<CR><CR>
Bram Moolenaar802a0d92016-06-26 16:17:58 +0200460 nmap <C-Space><C-Space>a
461 \:vert scs find a <C-R>=expand("<cword>")<CR><CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463==============================================================================
4647. Cscope availability and information *cscope-info*
465
466If you do not already have cscope (it did not come with your compiler
467license or OS distribution), then you can download it for free from:
468 http://cscope.sourceforge.net/
469This is released by SCO under the BSD license.
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471In Solaris 2.x, if you have the C compiler license, you will also have
472cscope. Both are usually located under /opt/SUNWspro/bin
473
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474There is source to an older version of a cscope clone (called "cs") available
475on the net. Due to various reasons, this is not supported with Vim.
476
477The cscope interface/support for Vim was originally written by
478Andy Kahn <ackahn@netapp.com>. The original structure (as well as a tiny
Bram Moolenaar2f058492017-11-30 20:27:52 +0100479bit of code) was adapted from the cscope interface in nvi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 *cscope-win32*
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100481For a cscope version for Win32 see (seems abandoned):
Bram Moolenaar2f058492017-11-30 20:27:52 +0100482 https://code.google.com/archive/p/cscope-win32/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000484Win32 support was added by Sergey Khorev <sergey.khorev@gmail.com>. Contact
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000485him if you have Win32-specific issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487 vim:tw=78:ts=8:ft=help:norl: