Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Cscope
Back to page
Edit
Edit source
View history
Talk (0)
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
Anti-spam check. Do
not
fill this in!
{{TipNew |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> 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) </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-]}}> 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== 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> *** 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) </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. ==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 <code>:cs reset</code> --[[User:Tonymec|Tonymec]] 04:40, November 25, 2009 (UTC)
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipNew
(
view source
)
Follow on IG
TikTok
Join Fan Lab