if_cscop.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. *if_cscop.txt* Nvim
  2. VIM REFERENCE MANUAL by Andy Kahn
  3. *cscope* *Cscope*
  4. Cscope is a "code intelligence" tool that helps you navigate C programs. It
  5. can also perform some refactoring tasks, such as renaming a global variable in
  6. all source files. Think of it as "ctags on steroids".
  7. See |cscope-usage| for a quickstart.
  8. Type |gO| to see the table of contents.
  9. ==============================================================================
  10. Cscope introduction *cscope-intro*
  11. Cscope is designed to answer questions like:
  12. Where is this symbol used?
  13. Where is it defined?
  14. Where did this variable get its value?
  15. What is this global symbol's definition?
  16. Where is this function in the source files?
  17. What functions call this function?
  18. What functions are called by this function?
  19. Where does the message "out of space" come from?
  20. Where is this source file in the directory structure?
  21. What files include this header file?
  22. Cscope answers these questions from a symbol database that it builds the first
  23. time it is used on the source files. On a subsequent call, cscope rebuilds
  24. the database only if a source file has changed or the list of source files is
  25. different. When the database is rebuilt the data for the unchanged files is
  26. copied from the old database, which makes rebuilding much faster than the
  27. initial build.
  28. See |cscope-usage| to get started.
  29. ==============================================================================
  30. Cscope commands *cscope-commands*
  31. *:cscope* *:cs* *:scs* *:scscope* *E259* *E262* *E560* *E561*
  32. All cscope commands are accessed through suboptions to the cscope commands.
  33. `:cscope` or `:cs` is the main command
  34. `:scscope` or `:scs` does the same and splits the window
  35. `:lcscope` or `:lcs` uses the location list, see |:lcscope|
  36. The available subcommands are:
  37. *E563* *E564* *E566* *E568* *E622* *E623* *E625*
  38. *E626* *E609*
  39. add : Add a new cscope database/connection.
  40. USAGE :cs add {file|dir} [pre-path] [flags]
  41. [pre-path] is the pathname used with the -P command to cscope.
  42. [flags] are any additional flags you want to pass to cscope.
  43. EXAMPLES >
  44. :cscope add /usr/local/cdb/cscope.out
  45. :cscope add /projects/vim/cscope.out /usr/local/vim
  46. :cscope add cscope.out /usr/local/vim -C
  47. <
  48. *cscope-find* *cs-find* *E567*
  49. find : Query cscope. All cscope query options are available
  50. except option #5 ("Change this grep pattern").
  51. USAGE :cs find {querytype} {name}
  52. {querytype} corresponds to the actual cscope line
  53. interface numbers as well as default nvi commands:
  54. 0 or s: Find this C symbol
  55. 1 or g: Find this definition
  56. 2 or d: Find functions called by this function
  57. 3 or c: Find functions calling this function
  58. 4 or t: Find this text string
  59. 6 or e: Find this egrep pattern
  60. 7 or f: Find this file
  61. 8 or i: Find files #including this file
  62. 9 or a: Find places where this symbol is assigned a value
  63. For all types, except 4 and 6, leading white space for {name} is
  64. removed. For 4 and 6 there is exactly one space between {querytype}
  65. and {name}. Further white space is included in {name}.
  66. EXAMPLES >
  67. :cscope find c vim_free
  68. :cscope find 3 vim_free
  69. <
  70. These two examples perform the same query: functions calling
  71. "vim_free". >
  72. :cscope find t initOnce
  73. :cscope find t initOnce
  74. <
  75. The first one searches for the text "initOnce", the second one for
  76. " initOnce". >
  77. :cscope find 0 DEFAULT_TERM
  78. <
  79. Executing this example on the source code for Vim 5.1 produces the
  80. following output:
  81. Cscope tag: DEFAULT_TERM
  82. # line filename / context / line
  83. 1 1009 vim-5.1-gtk/src/term.c <<GLOBAL>>
  84. #define DEFAULT_TERM (char_u *)"amiga"
  85. 2 1013 vim-5.1-gtk/src/term.c <<GLOBAL>>
  86. #define DEFAULT_TERM (char_u *)"win32"
  87. 3 1017 vim-5.1-gtk/src/term.c <<GLOBAL>>
  88. #define DEFAULT_TERM (char_u *)"pcterm"
  89. 4 1021 vim-5.1-gtk/src/term.c <<GLOBAL>>
  90. #define DEFAULT_TERM (char_u *)"ansi"
  91. 5 1025 vim-5.1-gtk/src/term.c <<GLOBAL>>
  92. #define DEFAULT_TERM (char_u *)"vt52"
  93. 6 1029 vim-5.1-gtk/src/term.c <<GLOBAL>>
  94. #define DEFAULT_TERM (char_u *)"os2ansi"
  95. 7 1033 vim-5.1-gtk/src/term.c <<GLOBAL>>
  96. #define DEFAULT_TERM (char_u *)"ansi"
  97. 8 1037 vim-5.1-gtk/src/term.c <<GLOBAL>>
  98. # undef DEFAULT_TERM
  99. 9 1038 vim-5.1-gtk/src/term.c <<GLOBAL>>
  100. #define DEFAULT_TERM (char_u *)"beos-ansi"
  101. 10 1042 vim-5.1-gtk/src/term.c <<GLOBAL>>
  102. #define DEFAULT_TERM (char_u *)"mac-ansi"
  103. 11 1335 vim-5.1-gtk/src/term.c <<set_termname>>
  104. term = DEFAULT_TERM;
  105. 12 1459 vim-5.1-gtk/src/term.c <<set_termname>>
  106. if (STRCMP(term, DEFAULT_TERM))
  107. 13 1826 vim-5.1-gtk/src/term.c <<termcapinit>>
  108. term = DEFAULT_TERM;
  109. 14 1833 vim-5.1-gtk/src/term.c <<termcapinit>>
  110. term = DEFAULT_TERM;
  111. 15 3635 vim-5.1-gtk/src/term.c <<update_tcap>>
  112. p = find_builtin_term(DEFAULT_TERM);
  113. Enter nr of choice (<CR> to abort):
  114. The output shows several pieces of information:
  115. 1. The tag number (there are 15 in this example).
  116. 2. The line number where the tag occurs.
  117. 3. The filename where the tag occurs.
  118. 4. The context of the tag (e.g., global, or the function name).
  119. 5. The line from the file itself.
  120. help : Show a brief synopsis.
  121. USAGE :cs help
  122. *E261*
  123. kill : Kill a cscope connection (or kill all cscope connections).
  124. USAGE :cs kill {num|partial_name}
  125. To kill a cscope connection, the connection number or a partial
  126. name must be specified. The partial name is simply any part of
  127. the pathname of the cscope database. Kill a cscope connection
  128. using the partial name with caution!
  129. If the specified connection number is -1, then _ALL_ cscope
  130. connections will be killed.
  131. reset : Reinit all cscope connections.
  132. USAGE :cs reset
  133. show : Show cscope connections.
  134. USAGE :cs show
  135. *:lcscope* *:lcs*
  136. This command is same as the ":cscope" command, except when the
  137. 'cscopequickfix' option is set, the location list for the current window is
  138. used instead of the quickfix list to show the cscope results.
  139. *:cstag* *E257* *E562*
  140. If you use cscope as well as ctags, |:cstag| allows you to search one or
  141. the other before making a jump. For example, you can choose to first
  142. search your cscope database(s) for a match, and if one is not found, then
  143. your tags file(s) will be searched. The order in which this happens
  144. is determined by the value of |csto|. See |cscope-options| for more
  145. details.
  146. |:cstag| performs the equivalent of ":cs find g" on the identifier when
  147. searching through the cscope database(s).
  148. |:cstag| performs the equivalent of |:tjump| on the identifier when searching
  149. through your tags file(s).
  150. ==============================================================================
  151. Cscope options *cscope-options*
  152. Use the |:set| command to set all cscope options. Ideally, you would do
  153. this in one of your startup files (e.g., vimrc). Some cscope related
  154. variables are only valid within |init.vim|. Setting them after vim has
  155. started will have no effect!
  156. *cscopeprg* *csprg*
  157. 'cscopeprg' specifies the command to execute cscope. The default is
  158. "cscope". For example: >
  159. :set csprg=/usr/local/bin/cscope
  160. <
  161. *cscopequickfix* *csqf* *E469*
  162. 'cscopequickfix' specifies whether to use quickfix window to show cscope
  163. results. This is a list of comma-separated values. Each item consists of
  164. |cscope-find| command (s, g, d, c, t, e, f, i or a) and flag (+, - or 0).
  165. '+' indicates that results must be appended to quickfix window,
  166. '-' implies previous results clearance, '0' or command absence - don't use
  167. quickfix. Search is performed from start until first command occurrence.
  168. The default value is "" (don't use quickfix anyway). The following value
  169. seems to be useful: >
  170. :set cscopequickfix=s-,c-,d-,i-,t-,e-,a-
  171. <
  172. *cscopetag* *cst*
  173. If 'cscopetag' is set, the commands ":tag" and CTRL-] as well as "vim -t"
  174. will always use |:cstag| instead of the default :tag behavior. Effectively,
  175. by setting 'cst', you will always search your cscope databases as well as
  176. your tag files. The default is off.
  177. *cscoperelative* *csre*
  178. If 'cscoperelative' is set, then in absence of a prefix given to cscope
  179. (prefix is the argument of -P option of cscope), basename of cscope.out
  180. location (usually the project root directory) will be used as the prefix
  181. to construct an absolute path. The default is off. Note: This option is
  182. only effective when cscope (cscopeprg) is initialized without a prefix
  183. path (-P).
  184. *cscopetagorder* *csto*
  185. The value of 'csto' determines the order in which |:cstag| performs a search.
  186. If 'csto' is set to zero, cscope database(s) are searched first, followed
  187. by tag file(s) if cscope did not return any matches. If 'csto' is set to
  188. one, tag file(s) are searched before cscope database(s). The default is zero.
  189. *cscopepathcomp* *cspc*
  190. 'cscopepathcomp' determines how many components of a file's path to display.
  191. With the default value of zero the entire path will be displayed.
  192. The value one will display only the filename with no path. Other values
  193. display that many components. For example: >
  194. :set cscopepathcomp=3
  195. will display the last 3 components of the file's path, including the file
  196. name itself.
  197. ==============================================================================
  198. Using cscope in Nvim *cscope-usage* *cscope-howtouse*
  199. To get started, build the cscope database in your project root directory: >
  200. cscope -bcqR
  201. See the cscope manpage for details: >
  202. :Man cscope
  203. By default the cscope database file is named "cscope.out". After building the
  204. database, connect to it from Nvim: >
  205. :cscope add cscope.out
  206. That establishes a cscope connection for Nvim to use. You can check the
  207. result with ":cs show". It will show something like:
  208. # pid database name prepend path
  209. 0 28806 cscope.out <none>
  210. Once a cscope connection is established, you can make queries to cscope and
  211. the results will be printed. Queries are made using the command ":cs find".
  212. For example: >
  213. :cs find g ALIGN_SIZE
  214. To make this easier you can configure mappings, see |cscope-suggestions|.
  215. If the results return only one match, you will automatically be taken to it.
  216. If there is more than one match, you will be given a selection screen to pick
  217. the match you want to go to. After you have jumped to the new location,
  218. simply hit Ctrl-T to get back to the previous one.
  219. ==============================================================================
  220. Limitations *cscope-limitations*
  221. Hard-coded limitation: doing a |:tjump| when |:cstag| searches the tag files
  222. is not configurable (e.g., you can't do a tselect instead).
  223. ==============================================================================
  224. Sample config *cscope-suggestions*
  225. Copy this into your init.vim (adjust paths for your system): >
  226. if has("cscope")
  227. set csprg=/usr/local/bin/cscope
  228. set csto=0
  229. set cst
  230. " add any database in current directory
  231. if filereadable("cscope.out")
  232. silent cs add cscope.out
  233. " else add database pointed to by environment
  234. elseif $CSCOPE_DB != ""
  235. silent cs add $CSCOPE_DB
  236. endif
  237. endif
  238. By setting 'cscopetag', we have effectively replaced all instances of the :tag
  239. command with :cstag. This includes :tag, Ctrl-], and "vim -t". In doing
  240. this, the regular tag command not only searches your ctags generated tag
  241. files, but your cscope databases as well.
  242. Some users may want to keep the regular tag behavior and have a different
  243. shortcut to access :cstag. For example, one could map Ctrl-_ (underscore)
  244. to :cstag with the following command: >
  245. map <C-_> :cstag <C-R>=expand("<cword>")<CR><CR>
  246. A couple of very commonly used cscope queries (using ":cs find") is to
  247. find all functions calling a certain function and to find all occurrences
  248. of a particular C symbol. To do this, you can use these mappings as an
  249. example: >
  250. map g<C-]> :cs find 3 <C-R>=expand("<cword>")<CR><CR>
  251. map g<C-\> :cs find 0 <C-R>=expand("<cword>")<CR><CR>
  252. These mappings for Ctrl-] (right bracket) and Ctrl-\ (backslash) allow you to
  253. place your cursor over the function name or C symbol and quickly query cscope
  254. for any matches.
  255. Or you may use the following scheme, inspired by Vim/Cscope tutorial from
  256. Cscope Home Page (https://cscope.sourceforge.net/): >
  257. nmap <C-_>s :cs find s <C-R>=expand("<cword>")<CR><CR>
  258. nmap <C-_>g :cs find g <C-R>=expand("<cword>")<CR><CR>
  259. nmap <C-_>c :cs find c <C-R>=expand("<cword>")<CR><CR>
  260. nmap <C-_>t :cs find t <C-R>=expand("<cword>")<CR><CR>
  261. nmap <C-_>e :cs find e <C-R>=expand("<cword>")<CR><CR>
  262. nmap <C-_>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
  263. nmap <C-_>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  264. nmap <C-_>d :cs find d <C-R>=expand("<cword>")<CR><CR>
  265. nmap <C-_>a :cs find a <C-R>=expand("<cword>")<CR><CR>
  266. " Using 'CTRL-spacebar' then a search type makes the vim window
  267. " split horizontally, with search result displayed in
  268. " the new window.
  269. nmap <C-Space>s :scs find s <C-R>=expand("<cword>")<CR><CR>
  270. nmap <C-Space>g :scs find g <C-R>=expand("<cword>")<CR><CR>
  271. nmap <C-Space>c :scs find c <C-R>=expand("<cword>")<CR><CR>
  272. nmap <C-Space>t :scs find t <C-R>=expand("<cword>")<CR><CR>
  273. nmap <C-Space>e :scs find e <C-R>=expand("<cword>")<CR><CR>
  274. nmap <C-Space>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
  275. nmap <C-Space>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  276. nmap <C-Space>d :scs find d <C-R>=expand("<cword>")<CR><CR>
  277. nmap <C-Space>a :scs find a <C-R>=expand("<cword>")<CR><CR>
  278. " Hitting CTRL-space *twice* before the search type does a vertical
  279. " split instead of a horizontal one
  280. nmap <C-Space><C-Space>s
  281. \:vert scs find s <C-R>=expand("<cword>")<CR><CR>
  282. nmap <C-Space><C-Space>g
  283. \:vert scs find g <C-R>=expand("<cword>")<CR><CR>
  284. nmap <C-Space><C-Space>c
  285. \:vert scs find c <C-R>=expand("<cword>")<CR><CR>
  286. nmap <C-Space><C-Space>t
  287. \:vert scs find t <C-R>=expand("<cword>")<CR><CR>
  288. nmap <C-Space><C-Space>e
  289. \:vert scs find e <C-R>=expand("<cword>")<CR><CR>
  290. nmap <C-Space><C-Space>i
  291. \:vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  292. nmap <C-Space><C-Space>d
  293. \:vert scs find d <C-R>=expand("<cword>")<CR><CR>
  294. nmap <C-Space><C-Space>a
  295. \:vert scs find a <C-R>=expand("<cword>")<CR><CR>
  296. <
  297. vim:tw=78:ts=8:noet:ft=help:norl: