Mac OS X下安装与配置ctags和tagbar.vim以开发obj-c

时间:2020-11-26 02:42:11

Mac OS X下安装与配置ctags和tagbar.vim以开发obj-c

ctags原先不支持objective-c, 后来在其trunk分支上增加了对obj-c的支持(其stable版本 截止目前仍然不支持obj-c). 由于ctags早期不支持obj-c, tagbar.vim插件亦未内置obj-c 支持. 因此, 为了在obj-c文件中正常使用tagbar.vim 以及使用tag跳转功能, 需要一 些比较hack的方式处理. 以下是安装与配置 ctagstagbar.vim的详细步骤.

安装与配置

  1. 安装tagbar.vim
    • 常规方式, 无须说明
  2. 使用Homebrew安装ctags的最新版本(trunk分支)
    • 安装命令如下 brew install ctags --HEAD
    • 如果已经安装stable版本的ctags, 安装前先 brew uninstall ctags
  3. 在Xcode工程根目录下运行以下命令创建tags文件
    • ctags --exclude='.git' --exclude='*.js' -R .
  4. 配置tagbar.vim. 在.vimrc文件中加入以下配置.

" add a definition for Objective-C to tagbar
let g:tagbar_type_objc = {
\ 'ctagstype' : 'ObjectiveC',
\ 'kinds' : [
\ 'i:interface',
\ 'I:implementation',
\ 'p:Protocol',
\ 'm:Object_method',
\ 'c:Class_method',
\ 'v:Global_variable',
\ 'F:Object field',
\ 'f:function',
\ 'p:property',
\ 't:type_alias',
\ 's:type_structure',
\ 'e:enumeration',
\ 'M:preprocessor_macro',
\ ],
\ 'sro' : ' ',
\ 'kind2scope' : {
\ 'i' : 'interface',
\ 'I' : 'implementation',
\ 'p' : 'Protocol',
\ 's' : 'type_structure',
\ 'e' : 'enumeration'
\ },
\ 'scope2kind' : {
\ 'interface' : 'i',
\ 'implementation' : 'I',
\ 'Protocol' : 'p',
\ 'type_structure' : 's',
\ 'enumeration' : 'e'
\ }
\ }

按照以上步骤即可在vim使用tag跳转功能了, 也能使用tagbar显示outline了.

快捷键优化

Vim默认使用Ctrl+] 往前跳tag, 使用Ctrl + t 往回跳, 为什么不是Ctrl+[呢?

Ctrl+]配对, 好记又方便. 在.vimrc加入以下代码可使得Ctrl+[ 往回跳.

" 注意 \[
nnoremap <C-\[> <C-T>

扩展阅读

  1. How to make Tagbar work with Objective-C
  2. John Goulah » Make Browsing Code Easier with Ack and Ctags