Vim Tips Wiki
(→‎Generating the database: add mention: "after" compiling)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
  +
{{TipNew
Cscope is a very powerful interface allowing you to scan C-like sources for not only identifier '''definitions''', but also their '''uses''', or even '''any regular expression''' in the text, and more; and the "standard include files" of your compiler are automagically added to "your" sources (if cscope can find them, and for me it can). The output from the "{{help|prefix=no|:cscope}} help" command says it all:
 
  +
|id=1638
  +
|previous=1637
  +
|next=1639
  +
|created=2009
  +
|complexity=basic
  +
|author=Tonymec
 
|version=6.2
  +
|subpage=/200911
  +
|category1=Navigation
  +
|category2=
  +
}}
 
Cscope is a very powerful interface allowing you to easily navigate C-like code files. While Cscope comes with its own stand-alone interface, Vim provides the capability to navigate code without ever leaving the editor. Using Cscope, you can search for identifier or function '''definitions''', their '''uses''', or even '''any regular expression'''. With the proper configuration, "standard" include files of your compiler are automatically searched along with your sources. The output from {{help|:cscope}} says it all:
 
<pre>
 
<pre>
 
cscope commands:
 
cscope commands:
Line 17: Line 29:
 
show : Show connections (Usage: show)
 
show : Show connections (Usage: show)
 
</pre>
 
</pre>
  +
 
==Setting up Vim to use cscope==
 
Adding the following snippet to your .[[vimrc]] will set up Vim to use cscope more efficiently:
 
<pre>
 
if has('cscope')
  +
set cscopetag cscopeverbose
  +
 
if has('quickfix')
 
set cscopequickfix=s-,c-,d-,i-,t-,e-
 
endif
  +
 
cnoreabbrev csa cs add
 
cnoreabbrev csf cs find
 
cnoreabbrev csk cs kill
 
cnoreabbrev csr cs reset
 
cnoreabbrev css cs show
 
cnoreabbrev csh cs help
  +
 
command -nargs=0 Cscope cs add $VIMSRC/src/cscope.out $VIMSRC/src
 
endif
 
</pre>
  +
 
'''Explanation:'''
 
===if has('cscope')===
 
If Vim hasn't got support for cscope, it's no use trying to use that support, so we bracket this whole snippet in this '''<code>if</code>''' statement to avoid unnecessary errors.
  +
  +
===set cscopetag cscopeverbose===
 
* {{help|prefix=no|'cscopetag'}} on means Vim will include the cscope database whenever we search for a tag (e.g. by hitting <{{help|prefix=no|tag=CTRL-%5D|label=CTRL-&#93;}}> in a C program).
 
* {{help|prefix=no|'cscopeverbose'}} on (optional) gives us a success/failure message when trying to add a cscope database (including the one near the end of this snippet).
  +
 
==={{help|prefix=no|'cscopequickfix'}}===
 
''(only if {{help|prefix=no|+quickfix}} compiled-in)'' specifies when to use {{help|prefix=no|quickfix}} for the output of cscope commands. We use the value given as example in the Vim help.
  +
 
===cnoreabbrev===
  +
Here we set up a number of command-line-mode {{help|prefix=no|abbreviations}} to make cscope commands easier to type. These abbreviations can be made less intrusive by making sure they [[Replace_a_built-in_command_using_cabbrev|only trigger at the start of the command line]], so that you can still type them normally in most cases where you don't actually want to use Cscope (for example, you may want to use css as a file extension sometimes). In Vim 7.0 or higher, this is easily done as follows:
  +
  +
<pre>
 
cnoreabbrev <expr> csa
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs add' : 'csa')
 
cnoreabbrev <expr> csf
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs find' : 'csf')
 
cnoreabbrev <expr> csk
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs kill' : 'csk')
 
cnoreabbrev <expr> csr
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs reset' : 'csr')
 
cnoreabbrev <expr> css
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs show' : 'css')
 
cnoreabbrev <expr> csh
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs help' : 'csh')
  +
</pre>
  +
 
===cs add===
 
We define a command <code>:Cscope</code> which will try to open the cscope database for the Vim source, and tell cscope that the relative paths in it are relative to the src directory containing the database itself ''(this assumes that the $VIMSRC variable has been set to the top directory of the Vim source.
  +
 
==Using cscope commands==
 
This is the simplest: if you've forgotten the fine points, '''<code>:cs help</code>''' (or, with the above abbreviations, '''<code>:csh</code>''') will tell you.
  +
 
==Generating the database==
 
==Generating the database==
Before you can use cscope on a set of source files, you must have a cscope database applying to them. For instance, the following patch to the Vim <tt>src/Makefile</tt> adds a few targets related to generating a cscope database for the Vim source ('''and''' the "usual" include files, which cscope is clever enough to find):
+
Before you can use cscope on a set of source files, you must have a cscope database applying to them. You could set up commands in Vim to generate one (or more), or you can set up a script to do it outside of Vim. Another nice option would be to generate a new database from your makefile, to allow you to easily use all the same files that get compiled into your project. For instance, the following patch to the Vim <code>src/Makefile</code> adds a few targets related to generating a cscope database for the Vim source ('''and''' the "usual" include files, which cscope is clever enough to find):
 
<pre>
 
<pre>
 
*** src/Makefile 2009-06-17 23:31:27.000000000 +0200
 
*** src/Makefile 2009-06-17 23:31:27.000000000 +0200
Line 27: Line 96:
 
tags TAGS: notags
 
tags TAGS: notags
 
$(TAGPRG) $(TAGS_SRC) $(TAGS_INCL)
 
$(TAGPRG) $(TAGS_SRC) $(TAGS_INCL)
  +
&nbsp;&nbsp;
 
 
+ # Build the cscope database.
 
+ # Build the cscope database.
 
+ # This may search more files than necessary.
 
+ # This may search more files than necessary.
Line 36: Line 105:
 
+ cscope -bv ./*.[ch] ./*.cpp ./if_perl.xs auto/*.h auto/pathdef.c proto/*.pro
 
+ cscope -bv ./*.[ch] ./*.cpp ./if_perl.xs auto/*.h auto/pathdef.c proto/*.pro
 
+ cscope: csclean cscope.out ;
 
+ cscope: csclean cscope.out ;
  +
+&nbsp;
+
 
 
# Make a highlight file for types. Requires Exuberant ctags and awk
 
# Make a highlight file for types. Requires Exuberant ctags and awk
 
types: types.vim
 
types: types.vim
Line 42: Line 111:
 
</pre>
 
</pre>
 
Using these targets works best <u>after</u> compiling Vim, since it also scans files in auto/ which are generated by the [configure+]make run.
 
Using these targets works best <u>after</u> compiling Vim, since it also scans files in auto/ which are generated by the [configure+]make run.
 
==Setting up Vim to use cscope==
 
The following snippet from my .vimrc sets up Vim to use cscope:
 
<pre>
 
if has('cscope')
 
set cst csverb
 
if has('quickfix')
 
set csqf=s-,c-,d-,i-,t-,e-
 
endif
 
if version < 700
 
cnoreabbrev csa cs add
 
cnoreabbrev csf cs find
 
cnoreabbrev csk cs kill
 
cnoreabbrev csr cs reset
 
cnoreabbrev css cs show
 
cnoreabbrev csh cs help
 
else
 
cnoreabbrev <expr> csa
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs add' : 'csa')
 
cnoreabbrev <expr> csf
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs find' : 'csf')
 
cnoreabbrev <expr> csk
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs kill' : 'csk')
 
cnoreabbrev <expr> csr
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs reset' : 'csr')
 
cnoreabbrev <expr> css
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs show' : 'css')
 
cnoreabbrev <expr> csh
 
\ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs help' : 'csh')
 
endif
 
if filereadable(expand('$VIMSRC/src/cscope.out'))
 
cs add $VIMSRC/src/cscope.out $VIMSRC/src
 
endif
 
if has('autocmd')
 
au VimLeave * cs kill -1
 
endif
 
endif
 
</pre>
 
'''Explanation:'''
 
===if has('cscope')===
 
If Vim hasn't got support for cscope, it's no use trying to use that support, so we bracket this whole snippet in this '''<tt>if</tt>''' statement to avoid unnecessary errors.
 
 
===set cst csverb===
 
* '{{help|prefix=no|cscopetag}}' on means Vim will include the cscope database whenever we search for a tag (e.g. by hitting <{{help|prefix=no|tag=CTRL-%5D|label=CTRL-&#93;}}> in a C program).
 
* '{{help|prefix=no|cscopeverbose}}' on (optional) gives us a success/failure message when trying to add a cscope database (including the one near the end of this snippet).
 
 
==='{{help|prefix=no|cscopequickfix}}'===
 
''(only if {{help|prefix=no|+quickfix}} compiled-in)'' specifies when to use {{help|prefix=no|quickfix}} for the output of cscope commands. We use the value given as example in the Vim help.
 
 
===cnoreabbrev===
 
Here we set up a number of command-line-mode {{help|prefix=no|abbreviations}} to make cscope commands easier to type. In Vim 7.0 or higher, we take advantage of the new <expr> modifier to only expand these abbreviations at the start of the command-line.<br />
 
''The use of continuation lines assumes that'' {{help|prefix=no|'nocompatible'}} ''is set, which is the default anyway if your vimrc is named'' <tt>.vimrc</tt> ''or'' <tt>_vimrc</tt> ''(but not'' <tt>.exrc</tt> ''or'' <tt>_exrc</tt>'', or anything with a'' <tt>-u</tt> ''command-line switch).''
 
 
===cs add===
 
If the cscope database for the Vim source has already been generated, we open it, and tell cscope that the relative paths in it are relative to the src directory containing the database itself ''(this assumes that the $VIMSRC variable has been set to the top directory of the Vim source: I use'' <tt>~/.build/vim/vim72</tt> ''for Vim 7.2, but of course YMMV).''
 
 
===cs kill===
 
Finally, we set up an autocommand to close any cscope database(s) when exiting Vim.
 
 
==Using cscope commands==
 
This is the simplest: if you've forgotten the fine points, '''<tt>:cs help</tt>''' (or, with the above abbreviations, '''<tt>:csh</tt>''') will tell you.
 
   
 
==See also==
 
==See also==
Line 108: Line 116:
   
 
==Comments==
 
==Comments==
Please sign your comments with <tt><nowiki>~~~~</nowiki></tt>, and separate unrelated comments with four dashes on their own line. [[User:Tonymec|Tonymec]] 05:36, November 2, 2009 (UTC)
+
If you rebuild the cscope database while Vim has a cscope connection open, the new database won't be used until either (a) you kill and re-add the database, or (b) you use <code>:cs reset</code> --[[User:Tonymec|Tonymec]] 04:40, November 25, 2009 (UTC)
----
 

Latest revision as of 06:42, 13 July 2012

Tip 1638 Printable Monobook Previous Next

created 2009 · complexity basic · author Tonymec · version 6.2


Cscope is a very powerful interface allowing you to easily navigate C-like code files. While Cscope comes with its own stand-alone interface, Vim provides the capability to navigate code without ever leaving the editor. Using Cscope, you can search for identifier or function definitions, their uses, or even any regular expression. With the proper configuration, "standard" include files of your compiler are automatically searched along with your sources. The output from :help :cscope says it all:

cscope commands:
add  : Add a new database             (Usage: add file|dir [pre-path] [flags])
find : Query for a pattern            (Usage: find c|d|e|f|g|i|s|t name)
       c: Find functions calling this function
       d: Find functions called by this function
       e: Find this egrep pattern
       f: Find this file
       g: Find this definition
       i: Find files #including this file
       s: Find this C symbol
       t: Find assignments to
help : Show this message              (Usage: help)
kill : Kill a connection              (Usage: kill #)
reset: Reinit all connections         (Usage: reset)
show : Show connections               (Usage: show)

Setting up Vim to use cscope[]

Adding the following snippet to your .vimrc will set up Vim to use cscope more efficiently:

if has('cscope')
  set cscopetag cscopeverbose

  if has('quickfix')
    set cscopequickfix=s-,c-,d-,i-,t-,e-
  endif

  cnoreabbrev csa cs add
  cnoreabbrev csf cs find
  cnoreabbrev csk cs kill
  cnoreabbrev csr cs reset
  cnoreabbrev css cs show
  cnoreabbrev csh cs help

  command -nargs=0 Cscope cs add $VIMSRC/src/cscope.out $VIMSRC/src
endif

Explanation:

if has('cscope')[]

If Vim hasn't got support for cscope, it's no use trying to use that support, so we bracket this whole snippet in this if statement to avoid unnecessary errors.

set cscopetag cscopeverbose[]

  • 'cscopetag' on means Vim will include the cscope database whenever we search for a tag (e.g. by hitting <CTRL-]> in a C program).
  • 'cscopeverbose' on (optional) gives us a success/failure message when trying to add a cscope database (including the one near the end of this snippet).

'cscopequickfix'[]

(only if +quickfix compiled-in) specifies when to use quickfix for the output of cscope commands. We use the value given as example in the Vim help.

cnoreabbrev[]

Here we set up a number of command-line-mode abbreviations to make cscope commands easier to type. These abbreviations can be made less intrusive by making sure they only trigger at the start of the command line, so that you can still type them normally in most cases where you don't actually want to use Cscope (for example, you may want to use css as a file extension sometimes). In Vim 7.0 or higher, this is easily done as follows:

    cnoreabbrev <expr> csa
          \ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs add'  : 'csa')
    cnoreabbrev <expr> csf
          \ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs find' : 'csf')
    cnoreabbrev <expr> csk
          \ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs kill' : 'csk')
    cnoreabbrev <expr> csr
          \ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs reset' : 'csr')
    cnoreabbrev <expr> css
          \ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs show' : 'css')
    cnoreabbrev <expr> csh
          \ ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs help' : 'csh')

cs add[]

We define a command :Cscope which will try to open the cscope database for the Vim source, and tell cscope that the relative paths in it are relative to the src directory containing the database itself (this assumes that the $VIMSRC variable has been set to the top directory of the Vim source.

Using cscope commands[]

This is the simplest: if you've forgotten the fine points, :cs help (or, with the above abbreviations, :csh) will tell you.

Generating the database[]

Before you can use cscope on a set of source files, you must have a cscope database applying to them. You could set up commands in Vim to generate one (or more), or you can set up a script to do it outside of Vim. Another nice option would be to generate a new database from your makefile, to allow you to easily use all the same files that get compiled into your project. For instance, the following patch to the Vim src/Makefile adds a few targets related to generating a cscope database for the Vim source (and the "usual" include files, which cscope is clever enough to find):

*** src/Makefile	2009-06-17 23:31:27.000000000 +0200
--- ../vim72/src/Makefile	2009-06-18 02:01:45.000000000 +0200
***************
*** 1734,1739 ****
--- 1734,1748 ----
  tags TAGS: notags
  	$(TAGPRG) $(TAGS_SRC) $(TAGS_INCL)
  
+ # Build the cscope database.
+ # This may search more files than necessary.
+ .PHONY: cscope csclean
+ csclean:
+ 	-rm -vf cscope.out
+ cscope.out:
+ 	cscope -bv ./*.[ch] ./*.cpp ./if_perl.xs auto/*.h auto/pathdef.c proto/*.pro
+ cscope: csclean cscope.out  ;
+ 
  # Make a highlight file for types.  Requires Exuberant ctags and awk
  types: types.vim
  types.vim: $(TAGS_SRC) $(TAGS_INCL)

Using these targets works best after compiling Vim, since it also scans files in auto/ which are generated by the [configure+]make run.

See also[]

:help if_cscop.txt

Comments[]

If you rebuild the cscope database while Vim has a cscope connection open, the new database won't be used until either (a) you kill and re-add the database, or (b) you use :cs reset --Tonymec 04:40, November 25, 2009 (UTC)