使用vim + ctags + cscope在Linux中阅读代码


为了方便在Linux下,快速阅读修改代码,可以使用vim+ctags+cscope的方式

一、安装ctags与cscope

Cscope 是一款开源免费的 C/C++浏览工具,自带一个基于文本的用户界面,通过cscope可以很方便地找到某个函数或变量的定义位置、被调用的位置等信息。Cscope对 C /C++支持较好,也可以自己定制来支持Java和Perl、Python等脚本语言。Vim和gvim都提供了cscope接口,通过适当的配置,可以在Unix/Linux下实现变量、函数、文件等之间跳转,就像Windows下的Source Insight一样灵活易用。由于cscope是开源免费的,而且配合vim可以脱离鼠标,实现全键盘操作,方便快捷地浏览源代码。

ctags(Generate tag files for source code)是vim下方便代码阅读的工具。尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM。并且VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码。

#安装ctags
sudo apt install exuberant-ctags -y
#安装cscope
sudo apt install cscope

在内核顶层目录中建立索引

make tags
make cscope

二、cscope用法

Usage: cscope [-bcCdehklLqRTuUvV] [-f file] [-F file] [-i file] [-I dir] [-s dir]
              [-p number] [-P path] [-[0-8] pattern] [source files]
-b            Build the cross-reference only.
-C            Ignore letter case when searching.
-c            Use only ASCII characters in the cross-ref file (don't compress).
-d            Do not update the cross-reference.
-e            Suppress the <Ctrl>-e command prompt between files.
-F symfile    Read symbol reference lines from symfile.
-f reffile    Use reffile as cross-ref file name instead of cscope.out.
-h            This help screen.
-I incdir     Look in incdir for any #include files.
-i namefile   Browse through files listed in namefile, instead of cscope.files
-k            Kernel Mode - don't use /usr/include for #include files.
-L            Do a single search with line-oriented output.
-l            Line-oriented interface.
-num pattern  Go to input field num (counting from 0) and find pattern.
-P path       Prepend path to relative file names in pre-built cross-ref file.
-p n          Display the last n file path components.
-q            Build an inverted index for quick symbol searching.
-R            Recurse directories for files.
-s dir        Look in dir for additional source  files.
-T            Use only the first eight characters to match against C symbols.
-U            Check file time stamps.
-u            Unconditionally build the cross-reference file.
-v            Be more verbose in line mode.
-V            Print the version number.

#当前目录下递归建立索引
cscope -Rbkq

#在文件中的用法 打开一个文件
:cs help

cscope commands:
add  : Add a new database             (Usage: add file|dir [pre-path] [flags])
find : Query for a pattern            (Usage: find a|c|d|e|f|g|i|s|t name)
       a: Find assignments to this symbol
       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 this text string
help : Show this message              (Usage: help)
kill : Kill a connection              (Usage: kill #)
reset: Reinit all connections         (Usage: reset)
show : Show connections               (Usage: show)

#添加一个database
:cs add cscope.out

#找到register_chrdev这个函数
:cs find d register_chrdev

三、ctags用法

#当前目录下所有文件生成tags
ctags -R *

#跳转到函数
:tag function
#添加tags
:set tags=./tags
#分割窗口
:stag

Ctrl + ] :跳转到函数定义

Ctrl + t :跳转到上一次的位置

Ctrl + w + ] :保留当前窗口 跳转到新的函数定义并打开新的窗口

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦成大佬的第N天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值