vim+cscope阅读源码

vim+cscope阅读源码

  • 前言
  • 1 vim配置文件
  • 2 使用cscope

前言

做嵌入式开发时,可以使用vim+cscope打造一个舒适的源码阅读环境。
1.添加自定义的vim配置文件使vim更易用。
2.cscope是查看代码的利器。可以定位函数、变量定义及被引用的代码位置。

1 vim配置文件

Vim 启动时,会根据配置文件(.vimrc)来设置 Vim,因此我们可以通过此文件来定制适合自己的 Vim。
Vim 配置文件分为系统配置文件和用户配置文件:

  • 系统配置文件位于 Vim 的安装目录(默认路径为 /etc/.vimrc);
  • 用户配置文件位于主目录 ~/.vimrc,即通过执行 vim ~/.vimrc 命令即可对此配置文件进行合理修改。通常情况下,Vim 用户配置文件需要自己手动创建。

注意,Vim 用户配置文件比系统配置文件的优先级高,换句话说,Vim 启动时,会优先读取 Vim 用户配置文件(位于主目录中的),所以我们只需要修改用户配置文件即可(不建议直接修改系统配置文件)。

我使用的.vimrc文件配置:

" 显示命令菜单
set wildmenu
" 打开行号
set number
" 不使用兼容模式
set nocompatible
" 自动缩进
set autoindent
" 历史记录条数
set history=50
" 在Vim窗口的右下角显示当前光标位置
set ruler
" 会在输入命令时,在屏幕底部显示出部分命令
set showcmd
" 实时匹配搜索输入
set incsearch
" 开启自动缩进
set ai
" 设置匹配模式,类似当输入一个左括号时会匹配相应的右括号
set showmatch
" 在底部显示当前模式
set showmode
" 对搜索词高亮显示
set hlsearch
" terminal Color,默认是8色
set t_Co=8
set cino=:0
"c语言缩进风格
set cindent"打开语法高亮
colo ron
syntax on"-- set tab \t space
set shiftwidth=4                
set backspace=2
set tabstop=4
set expandtab
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim           
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""set csto=0" add any cscope database in current directoryif filereadable("cscope.out")cs add cscope.out  "else add the database pointed to by environment variable elseif $CSCOPE_DB != ""cs add $CSCOPE_DBendif" show msg when any other cscope db addedset cscopeverbose  """"""""""""" My cscope/vim key mappings"" The following maps all invoke one of the following cscope search types:""   's'   symbol: find all references to the token under cursor"   'g'   global: find global definition(s) of the token under cursor"   'c'   calls:  find all calls to the function name under cursor"   't'   text:   find all instances of the text under cursor"   'e'   egrep:  egrep search for the word under cursor"   'f'   file:   open the filename under cursor"   'i'   includes: find files that include the filename under cursor"   'd'   called: find functions that function under cursor calls"" To do the first type of search, hit 'CTRL-\', followed by one of the" cscope search types above (s,g,c,t,e,f,i,d).  The result of your cscope" search will be displayed in the current window.  You can use CTRL-T to" go back to where you were before the search.  "nmap s :cs find s =expand("")	nmap g :cs find g =expand("")	nmap c :cs find c =expand("")	nmap t :cs find t =expand("")	nmap e :cs find e =expand("")	nmap f :cs find f =expand("")	nmap i :cs find i ^=expand("")$nmap d :cs find d =expand("")	

2 使用cscope

安装cscope

sudo apt-get install cscope

cscope的用法很简单,首先需要为你的代码生成一个cscope数据库:

cscope -Rbkq 

-R:为当前目录下所有子目录创建数据库
-b:生成数据库之后退出,不启动自带界面
-q:生成cscope.in.out和cscope.po.out,加快搜索速度
-k:跳过/usr/include目录

在vim可以输入以下命令:

:cs f g start_kernel  看函数start_kernel 定义的地方
:cs f c start_kernel	看哪些地方调用start_kernel函数

如果看到函数代码后需要了解定义和调用的地方:

1.光标移到该函数 
2.ctrl+\ 抬起来g   看函数定义的地方
3.ctrl+\ 抬起来c    看哪些地方调用函数


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部