cscope best practices

382 views
Skip to first unread message

Ethan Hereth

unread,
Oct 5, 2013, 4:32:37 PM10/5/13
to vim...@googlegroups.com
Hey vim_use!

I've been a subscriber for quite a while now and thoroughly enjoy watching you experts at work. I've also been a vim user for some time and consider myself decently proficient with it although I've done very little scripting with it. I have had cscope on my TODO list for a while and finally sat down today to figure it out. I think I'll find its functionality very useful in my day to day use of vim.

I bet many of you use cscope every day and have developed nice shortcuts that make its use easy and quick. I have read the cscope page on vim.wikia.com and looked at the standard cscope_maps.vim settings that you can get from sourceforge (there seem to be mirrors of it everywhere...)

The thing is that I'm not sold on the maps/commands that I've seen so far. I've glanced on github as well but didn't find much there that tickled my fancy either. I have RTFM and think I understand everything there. I like how it works with ctags as well.

So, my question for everyone is: can you share with me the maps, habits, functions, etc. that you've developed over time to streamline your used of cscope within vim. I would love to see these. Really, I would love to get any advice you'd be willing to offer up about it.

I also was wondering if there is a easy way to make ctrl-] also jump to a source file (like stdio.h) if the cursor happened to be on a filename instead of a valid tag. (Does this even make sense to do?)


Thank you all in advance for your input!

Gary Johnson

unread,
Oct 5, 2013, 5:03:39 PM10/5/13
to vim...@googlegroups.com
On 2013-10-05, Ethan Hereth wrote:

> I also was wondering if there is a easy way to make ctrl-] also
> jump to a source file (like stdio.h) if the cursor happened to be
> on a filename instead of a valid tag. (Does this even make sense
> to do?)

That one's easy: just add the option

--extra=+f

when you run ctags.

That also lets you edit a file without specifying the full path name
to it or even knowing where it is by starting vim as

$ vim -t file.c

or from within vim with

:tag file.c

Regards,
Gary

Gary Johnson

unread,
Oct 5, 2013, 5:27:48 PM10/5/13
to vim...@googlegroups.com
On 2013-10-05, Ethan Hereth wrote:

> So, my question for everyone is: can you share with me the maps,
> habits, functions, etc. that you've developed over time to
> streamline your used of cscope within vim. I would love to see
> these. Really, I would love to get any advice you'd be willing to
> offer up about it.

The most useful feature of the mappings I use is the automatic
adding of the cscope database: it's added only when I request
information from it. Here's an example mapping and the command and
function it invokes. Typing ,s over a symbol finds all occurrences
of that symbol and puts them in the quickfix list because I have set
'cscopequickfix' to include "s-".

map <silent> ,s :Csfind s <C-R><C-W><CR>zv

command! -nargs=+ Csfind call Csfind("cscope", <f-args>)

function! Csfind(cmd, querytype, name)
try
execute a:cmd "find" a:querytype a:name
return
catch /E567/ " 'no cscope connections'
" Continue after endtry.
catch /.*/ " Any other error.
call s:EchoException()
return
endtry
try
if exists("g:cscope_prepend_path")
execute "cs add" g:cscope_database g:cscope_prepend_path
else
execute "cs add" g:cscope_database
endif
catch /.*/
" Note: The perror() message
" cs_read_prompt EOF: Error 0
" currently (2004-02-20) hides the more-informative message
" E609: Cscope error: cscope: cannot read file version from file cscope.out
"
call s:EchoException()
return
endtry
try
execute a:cmd "find" a:querytype a:name
catch /.*/ " Any error.
call s:EchoException()
endtry
endfunction

function! s:EchoException()
if &errorbells
normal \<Esc> " Generate a bell
endif
echohl ErrorMsg
echo matchstr(v:exception, ':\zs.*')
echohl None
endfunction

Regards,
Gary

Tony Mechelynck

unread,
Oct 5, 2013, 10:26:17 PM10/5/13
to vim...@googlegroups.com
I mostly use cscope in relation with the Vim source.

The cscope database must be regenerated from time to time, or the
quickfix lists generated by :cscope find will get out of step with the code.

On Unix-like platforms, I recommend doing that after compiling Vim at
least once, so that auto-generated sources have been generated.

To build the database, I run the following command in the src/ source
directory:

cscope -bv ./*.[ch] ./*.cpp ./if_perl.xs auto/*.h auto/pathdef.c proto/*.pro


In Vim, I have the following "aids" in my vimrc for cscope (some of the
lines are quite long; I hope your mailer or mine won't mess them up):


if has('cscope')
set cst
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 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> css ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs
show' : 'css')
cnoreabbrev <expr> csh ((getcmdtype() == ':' && getcmdpos() <= 4)? 'cs
help' : 'csh')
endif
command -bar Cscope cs add $VIMSRC/src/cscope.out $VIMSRC/src
set csverb
endif

where $VIMSRC has been defined earlier in my vimrc as the top-level
directory of my Vim repository clone (the parent of .hg, src, runtime, etc.)


Best regards,
Tony.
--
Screw up your courage! You've screwed up everything else.


Ethan Hereth

unread,
Oct 8, 2013, 9:35:05 PM10/8/13
to vim...@googlegroups.com
Thank you gentlemen, 

I've been busy but I should have an excuse to really use/implement this stuff soon. Gary, thanks for that ctags tip

I will play around with these maps/abbreviations to see which of them feel right.

Thanks again

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ethan Hereth

unread,
Oct 8, 2013, 10:44:07 PM10/8/13
to vim...@googlegroups.com
Gary, I tried the --extra=+f option to ctags and it seemed to have no effect in vim. For example, I rerun ctags with this option, I verify that the tags file has changed so the option is being used. Then I have the cursor over, for example, main.cpp in my Makefile and I enter ctrl-] and nothing happens. There is an entry in the tags file that looks like

main.cpp main.cpp 1;" F

but I am not switched to the file main.cpp. I am aware I can use 'gf' to get the same result, I guess I was mainly just curious to see if it could be done using the ctrl-] tags shortcut. I would expect it to work but I get nothing.

Any ideas?

Gary Johnson

unread,
Oct 9, 2013, 7:22:03 AM10/9/13
to vim...@googlegroups.com
On 2013-10-08, Ethan Hereth wrote:

> On 05/10/13 22:32, Ethan Hereth wrote:

> I also was wondering if there is a easy way to make ctrl-] �also
> jump to a source file (like stdio.h) if the cursor happened to be
> on a filename instead of a valid tag. (Does this even make sense to
> do?)

> Gary, I tried the --extra=+f option to ctags and it seemed to have no effect in
> vim. For example, I rerun ctags with this option, I verify that the tags file
> has changed so the option is being used. Then I have the cursor over, for
> example, main.cpp in my Makefile and I enter ctrl-] and nothing happens. There
> is an entry in the tags file that looks like
>
> main.cpp main.cpp 1;" F
>
> but I am not switched to the file main.cpp. I am aware I can use 'gf' to get
> the same result, I guess I was mainly just curious to see if it could be done
> using the ctrl-] tags shortcut. I would expect it to work but I get nothing.
>
> Any ideas?

Yes. I was wrong about how easy it would be to use Ctrl-] on a file
name. I use various commands to jump to files and I thought that
Ctrl-] was among them, but it turns out it's not.

From ":help Ctrl-]":

CTRL-] Jump to the definition of the keyword under the
cursor.

A "keyword" is not a "file name". A "keyword" usually contains only
alphanumeric characters and underscore, whereas a "file name"
usually may contains those characters plus others such as a period.
See

:help iskeyword
:help isfname

So when you put the cursor over the "main" in "main.cpp" and hit
Ctrl-], that command picked only "main". You could execute

:set isk+=.

and try again and it should work. That's not very practical,
though, since it would prevent Ctrl-] and other commands dealing
with "words" from working as expected in other contexts.

You could instead visually-select the file name and hit Ctrl-] and
it would jump to that file. See

:help v_CTRL-]

Something else you can do is use an approach posted to this list in
2009 by Henrik �hman: Create a mapping for gf that searches the
current tags file for the file name before searching 'path'. I've
modified his original mappings over the years so that the one for gf
now looks like this.

nnoremap <expr> gf empty(taglist('^'.expand('<cfile>').'$')) ? "gf" : ":tj <C-R><C-F><CR>"

So, if the file name under the cursor (<cfile>) is not in the
current taglist, then jump to it with the normal gf command.
Otherwise, jump to it with the :tjump command.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages