vim 中使用 cscope 的方法。 - yanbin's Blog
vim 中使用 cscope 的方法。
yanbin
posted @ 2015年3月22日 22:35
in Linux-使用
, 26273 阅读
0. cscope 依赖 $EDITOR, $CSCOPE_DB 环境变量。
使用 vim 打开 cscope 的搜索结果,需要设置 $EDITOR 环境变量为 vim.
$ vim ~/.bashrc
# add: export EDITOR=vim
$ source ~/.bashrc
NOTE: 很多工具都依赖 EDITOR 这个环境变量,修改这个环境变量的值时需考虑其影响范围。
vim 启动时默认加载当前工作目录下的 cscope database 文件,并且与 database 建立连接。
在 ~/.bashrc 或者类似的文件中设置 $CSCOPE_DB 环境变量,vim 启动时也会自动加载 $CSCOPE_DB
指定路径的 database 文件,并与 database 建立连接。
$ vim ~/.basrch
# add: export CSCOPE_DB=/path/to/cscope_db_file
$ source ~/.bashrc
如果这个 database 文件用在系统中的所有工作目录,其中存储的文件路径应该是绝对路径,
不然只能用在特定的工作目录了。
如何做到这一点呢?下面 1.2 给出方法。
1. cscope 的一些用法与 ctags 相同。
1.0 cscope 也支持 -R 参数,在不加 -b 参数时,cscope 运行一个curses-based GUI.
CTRL-d 退出该 GUI, 实际上也退出了 cscope.
Find this C symbol:
Find this global definition:
Find functions called by this function:
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
Find assignments to this symbol:
Find this global definition:
Find functions called by this function:
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
Find assignments to this symbol:
debug 一些问题时,经常出现(a)找到某个 error message 是在哪里打印的;
(b)某个打印 error message 并且退出程序的函数是如何实现/定义的;
(c)某个宏定义的值是什么; (d) 包含某个头文件的其他文件;
(e)函数的定义以及调用的地方; 诸如此类的需求,却无需关注全部代码,用这个工具非常方便。
cscope 还支持: (f) 使用 egrep 格式搜索字符串; (g) 根据文件名 find 文件; 实在方便。
1.1 cscope -R -b 直接搜索/遍历整个源码树,生成 cscope.out 数据库文件并退出程序。
cscope.out 类似 ctags -R 生成的 tags 文件。
内置 cscope support 的 vim 程序自动加载当前目录下的 cscope.out 文件。
FIXME: 不支持功能的 vim 又该如何设置才能自动化的加载 cscope.out 文件?
可以考在 .vimrc 中添加如下代码:
" add any cscope database in current directory
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
1.2 根据多个目录中文件的绝对路径生成 database 文件。
获得所有需要的文件的绝对路径,存放在一个文件中。
$ find /path/to/myproject -name "*.c" -o -name "*.h" > /path/to/foo/cscope.files
$ cd /path/to/foo # 保证 foo 中没有其他文件
$ cscope -b
# 在 ~/.bashrc 中添加: export CSCOPE_DB=/path/to/foo/cscope.out
2. vim 中使用 cscope 的方法。 vim 支持短命令 :cs; cs 即 cscope 的缩写。
2.0 :cs 命令基础用法。
0) cscope find 操作, 支持如下选项:
USAGE :cs find {querytype} {name}
0 or s: Find this C symbol
1 or g: Find this definition
2 or d: Find functions called by this function
3 or c: Find functions calling this function
4 or t: Find this text string
6 or e: Find this egrep pattern
7 or f: Find this file
8 or i: Find files #including this file
1 or g: Find this definition
2 or d: Find functions called by this function
3 or c: Find functions calling this function
4 or t: Find this text string
6 or e: Find this egrep pattern
7 or f: Find this file
8 or i: Find files #including this file
这些选项对应不同的搜索方式。
数字用来代替字母,作为一种记忆方式,方便不同的个人习惯选择。
1) find 选项使用举例。
:cs find c vim_free
:cs find 3 vim_free
:cs find 3 vim_free
这两个例子都执行 Find functions calling this function;
NOTE: 'vim_free' 和 '3' 之间有个'空格', 这种搜索方式会乎略此类‘空格’。
:cscope find t initOnce
:cscope find 4 initOnce
:cscope find 4 initOnce
这两个例子都执行: Find this text string;
NOTE: 与其他搜索方式不同, t or 4 以及 e or 6 方式会处理字符串的前后'空格'。
FIXEME: find t or find 4 是如何对待 ' 和 " 这两个字符的?
其他搜索方式又是如何对待这两个字符呢?
所有的搜索方式都将 ' or " 视为 name(字符串) 的一部分,
无论 ' or " 在字符串的开始、结尾或中间。
NOTE: since Cscope by default parses all C header files it finds in /usr/include,
you can open up most standard include files with this
:cscope find s oping_receive_one
结果如下:
Cscope tag: ping_receive_one
# line filename / context / line
1 445 src/liboping.c <<ping_receive_one>>
static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
2 706 src/liboping.c <<ping_receive_all>>
if (ping_receive_one (obj, ptr, &nowtime) == 0)
# line filename / context / line
1 445 src/liboping.c <<ping_receive_one>>
static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
2 706 src/liboping.c <<ping_receive_all>>
if (ping_receive_one (obj, ptr, &nowtime) == 0)
Type number and <Enter> (empty cancels):
从左至右依次为:
(a)搜索结果编号;(b)结果所在文件行号;(c)结果所在文件名;(d)搜索内容;(e)结果所在行文本;
输入「结果编号」后「回车」,vim 跳转到至一行;使用 CTRL-t 回退。
什么也不输入直接按「回车」可退出本次搜索。
如果只搜索到一个结果,会直接跳转过去。
2) vim 中使用 cscope 需要注意的一点。
保证 ~/.vimrc 或者 /etc/vim/vimrc 以及其他 plugin 里没有这一行:
set cscopequickfix=s-,c-,d-,i-,t-,e-
这样会导致使用 scscope quick window 功能,这个功能实在难用。
执行完 :cs find s token 之类的操作,不显示结果列表窗口,而是只有一行,既不能选中,
也不能翻页,叫人着急的想骂娘。
3) 进阶用法:
a) 更加简略的命令格式
:cs f s foo
b) 水平分割当前 vim 窗口显示某个搜索结果
:scs f s foo # 前面的 s 即为 split
c) 垂直分割当前 vim 窗口显示某个搜索结果
:vert scs f s foo # vert 是 vim 命令
2.1 使用插件, 映射 find 操作的快捷键。
cscope 官方教程提供了 cscope_maps.vim 插件。
将这个插件放到 /etc/viim/plugin 目录或者 ~/.vim/plugin 目录。
0) cscope_maps.vim 提供了如下快捷方式, 直接搜索光标(cursor)所在位置的 token or text.
<CTRL-\>s # symbol: find all references to the token under cursor
<CTRL-\>g # global: find global definition(s) of the token under cursor
<CTRL-\>c # calls: find all calls to the function name under cursor
<CTRL-\>t # text: find all instances of the text under cursor
<CTRL-\>e # egrep: egrep search for the word under cursor
<CTRL-\>f # file: open the filename under cursor
<CTRL-\>i # includes: find files that include the filename under cursor
<CRTL-\>d # called: find functions that function under cursor calls
<CTRL-\>g # global: find global definition(s) of the token under cursor
<CTRL-\>c # calls: find all calls to the function name under cursor
<CTRL-\>t # text: find all instances of the text under cursor
<CTRL-\>e # egrep: egrep search for the word under cursor
<CTRL-\>f # file: open the filename under cursor
<CTRL-\>i # includes: find files that include the filename under cursor
<CRTL-\>d # called: find functions that function under cursor calls
1) 使用举例:
(a) 光标移动到要搜索的 token or text;
(b) 同时按下 CTRL 和 \ 「反斜线」键,松开后立即按下 s 键,即执行 :cs find s token 操作;
(c) 搜索结果如同 :cs find;
2) 高级用法,显示某个搜索结果而又不影响当前窗口:
vim 提过了 split 功能,支持水平和垂直分割窗口,可以在一个新的分割窗口中显示结果。
使用 cscope_maps.vim plugin 中提供的快捷键可以方便的做到这一点。
(a) 使用 <CTRL-@> 代替 <CTRL-\>, 搜索方式相同而打开结果时会从一个水平窗口打开,
<CTRL-@> 在 vim 中用 <CTRL-spacebar>代替;
(b) 光标移动要搜索的 token or text;
(c) 同时按下 CTRL 和「空格」键,松开后立即按下 s 键,即执行 :cs find s token 操作;
(d) 搜索结果等同于 :cs find, 显示某个结果时水平分割当前窗口,显示在新窗口中。
(e) 同样是 :cs find s token, 垂直显示结果,则是 <CTRL-@><CTRL-@>s。
在 vim 中需要连续两次键入 CTRL-空格,然后再按下 s 键。
3) 使用 cscope_maps.vim plugin 需要注意的地方:
cscope_maps.vim plugin 多次一举的包含了如下代码:
" add any cscope database in current directory
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
这些代码可能与 vimrc 中代码相冲突,或者与 vim 内置功能相冲突,因为它们都执行了
加载当前目录下 cscope.out 数据库操作。vim 会启动时会提示如下错误:
Error detected while processing /home/username/.vim/plugin/cscope_maps.vim:
line 45: E568: duplicate cscope database not added
Hit ENTER or type command to continue
line 45: E568: duplicate cscope database not added
Hit ENTER or type command to continue
私以为直接注释掉 scsope_maps.vim plugin 中的这几行代码就好了。
也有人说在 ~/.vimrc 里加上: set nocscopeversbose 之类的,这完全不治本嘛。
2018年3月26日 16:48
Thank you for sharing this very nice post awesome keep sharing.
2019年5月13日 22:33
It will be great pleasure when you download the wonderful new whatsapp status. I'm sure you will get more and more likes
2019年9月22日 20:09
Oftentimes you just need to have the home clean at once. Whether it’s your post-party maintaining, unexpected guests on the way or several different other explanations, you’ll be in your pinch which includes a dirty, messy home. Numerous Maids offices have staff measurements, equipment plus proven idea to handle any last-minute home cleaning quickly plus thoroughly. Let us make your place vivid and clean for you to handle a person's other assignments.
2019年11月06日 14:55
Thank you for your post, I look for such article along time, today I find it finally. this post gives me lots of advise it is very useful for me.
2019年11月16日 18:06
There are various courses obtainable online and at the local college that will help you turn out to be an amusement agent. Some courses might be specifically directed at entertainment brokers, while additional courses generally business.
2020年3月26日 23:13
All the occupational health and wellness nurse will probably develop pro-active ways of help all the workforce take care of or get back their succeed ability. Cutting edge workers, elder workers, women time for work sticking with pregnancy or possibly workers who've been unemployed in a prolonged space of time may all profit from health advice or even planned regimen of succeed hardening exercises to assist you to maintain or possibly restore most of the work proficiency even prior to when any health concerns arise. <a href="http://www.siberyanhealth.com">www.siberyanhealth.com</a>
2020年3月26日 23:13
It is advisable to important to address oneself using lots of the principles of great health. Regular check-ups, proper eating and sensible living all of the enable person to keep an eye on their health within the careful at this point reasonable solution. Often a becomes overly associated with health situations and involved in a fabulous cycle about health considerations. <a href="http://www.healthynbalanced.com">www.healthynbalanced.com</a>
2020年3月26日 23:13
It is advisable to important to address oneself using lots of the principles of great health. Regular check-ups, proper eating and sensible living all of the enable person to keep an eye on their health within the careful at this point reasonable solution. Often a becomes overly associated with health situations and involved in a fabulous cycle about health considerations. <a href="http://www.sikkimhealth.org">www.sikkimhealth.org</a>
2020年3月26日 23:14
Legislation of The law of gravity is certainly one of the a Common Law. So certainly is the Law about Conservation of one's, that is normally, energy is not to be created or possibly destroyed, only transformed in one form to another one. However, nothing like human law regulations, which are slightly different from countryside to countryside, Universal Law regulations are continual and predetermined. You connect to these law regulations with just about every single breath you will take. Individuals govern any existence. <a href="http://www.cleantechlaws.com">www.cleantechlaws.com</a>
2020年3月26日 23:14
Legislation of The law of gravity is certainly one of the a Common Law. So certainly is the Law about Conservation of one's, that is normally, energy is not to be created or possibly destroyed, only transformed in one form to another one. However, nothing like human law regulations, which are slightly different from countryside to countryside, Universal Law regulations are continual and predetermined. You connect to these law regulations with just about every single breath you will take. Individuals govern any existence. <a href="http://www.bankruptcylawmerced.com">www.bankruptcylawmerced.com</a>
2020年4月30日 19:32
Using these pressures of modern-day life you may need help and you no longer need to always be very rich to acquire help. You could get a maid for a few days in a very week to help you with your home chores and get back some of your time. Having a new maid to help you savings around your house will not simply free up your time and efforts, it is usually a wonderful approach to relieve strain and minimize depression.
2020年4月30日 19:32
Should you have vaulted ceilings, you could be wondering how we will coloring the partitions. You could most likely barely reach which has a long corporate, assuming you need one, but you do not necessarily be capable of do an outstanding job. Impress your current guests by simply keeping paint for the walls and off of the ceiling, even in high partitions that look impossible to arrive at.
2021年6月07日 18:12
Do you know of a bath tub and wash facility in the bathroom? In that case, don’t forget to clean up them. What's more, the qualified cleaning company highly suggests opting for frequent profound clean of your bathtubs, showers, and various surfaces of your bathroom. For the reason that, these spots are mainly the proliferation ground for any harmful micro organism, germs, and perhaps pests.
2022年9月04日 19:26
Gujarat Board Model Paper 2023 Class 3 Pdf Download with Answers for Gujarati Medium, English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. GSEB Question Paper Class 3 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.
2022年12月19日 16:16
To use cscope with Vim, you will first need to install cscope on your system. Once cscope is installed, you can follow Lab grown diamonds the above steps to set it up in Vim: the step-by-step process is clearly mentioned over here and you just need to follow that. Keep sharing more such details here.
2023年1月11日 15:03
"To use cscope in vim, first make sure you have cscope installed. Then, create a file called .vimrc in your home directory and add the following line: set nocscopeverbose Now, you can use the :cscope command to bring up a cscope menu. Lab grown diamonds From there, you can search for symbols, functions, filenames, or grep patterns."
2023年4月23日 15:22
crediblebh