vim+cscope阅读源码

vim+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
" 不使用兼容模式,去掉有关vi一致性模式
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

" 设置总是显示状态行,方便看到当前文件名
set laststatus=2
set statusline=%F%r\ [HEX=%B][%l,%v,%P]\ %{strftime(\"%H:%M\")}
" 设置List模式
set list
set listchars=tab:>-,trail:-
" 设置切换窗口的快捷键
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim           
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    set csto=0
    " 解决重复的 cscope 数据库未被加入” 这样的错误
    set nocsverb
    " add any cscope database in current directory
    if filereadable("cscope.out")
    	cs add cscope.out  
    "else add the database pointed to by environment variable 
    elseif $CSCOPE_DB != ""
       cs add $CSCOPE_DB
    endif

    " show msg when any other cscope db added
    set 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.  
    "
    " 设置cscope命令快捷键
    nmap <C-g> :cs find g<Space>
    nmap <leader>s :cs find s <C-R>=expand("<cword>")<CR><CR>>--
    nmap <leader>g :cs find g <C-R>=expand("<cword>")<CR><CR>
    "nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>>
    "nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>>
    "nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>>
    "nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>>
    "nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
     "nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>

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 定义的地方(快捷键:同时按CTRL键加g键)
:cs f s start_kernel	看哪些地方调用start_kernel函数

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

1.光标移到该函数 
2.\ 抬起来g   看函数定义的地方
3.\ 抬起来s    看哪些地方调用函数
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值