2012年2月5日日曜日

[工具使用] vim 代码自动补齐

http://easwy.com/blog/archives/advanced-vim-skills-omin-complete/

1. 安装 ctags
下载 : http://ctags.sourceforge.net/
$ ./configure --prefix=/usr/local/ctags-x.x
$ make && make install
$ /etc/bashrc (追加)
# ctags settings
export PATH=$PATH:/usr/local/ctags-x.x/bin

$ source /etc/bashrc
--------------------------------------------------------------------------------
2. 安装 taglist 插件
下载 : http://sourceforge.net/projects/vim-taglist/files/

解压后的文件路径
~/.vim/plugin/taglist.vim (taglist插件)
~/.vim/doc/taglist.txt    (taglist帮助文件)

在 vim 中生成帮助标签
:helptags ~/.vim/doc

在 vim 中查看 taglist 帮助
:help taglist.txt

在 taglist 窗口中的快捷键

<CR>    跳到光标下tag所定义的位置,用鼠标双击此tag功能也一样
o       在一个新打开的窗口中显示光标下tag
<Space> 显示光标下tag的原型定义
u       更新taglist窗口中的tag
s       更改排序方式,在按名字排序和按出现顺序排序间切换
x       taglist窗口放大和缩小,方便查看较长的tag
+       打开一个折叠,同zo
-       将tag折叠起来,同zc
*       打开所有的折叠,同zR
=       将所有tag折叠起来,同zM
[[      跳到前一个文件
]]      跳到后一个文件
q       关闭taglist窗口
<F1>    显示帮助
--------------------------------------------------------------------------------
3. 安装 OmniCppComplete 插件
下载 : http://www.vim.org/scripts/script.php?script_id=1520

$ unzip omnicppcomplete-x.x.zip
$ mv doc/omnicppcomplete.txt ~/.vim/doc/ -v
$ mv autoload after ~/.vim/ -v

$ cp /usr/share/vim/vim70/vimrc_example.vim ~/.vimrc
$ vim ~/.vimrc
" custom settings
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set number

" -- ctags --
" map <ctrl>+F12 to generate ctags for current folder:
" map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR>
" add tags file to available tags
set tags+=/usr/local/src/postgresql-x.x.x/src/tags

" taglist settings
map <silent> <F12> :TlistToggle<CR>
let Tlist_Show_One_File = 1
let Tlist_Exit_OnlyWindow = 1
let Tlist_Use_Right_Window = 1
let Tlist_File_Fold_Auto_Close = 1
let Tlist_GainFocus_On_ToggleOpen = 1
let Tlist_Ctags_Cmd = "/usr/local/ctags-x.x/bin/ctags"
" let Tlist_Auto_Open = 1
" let Tlist_WinHeight = 20
" let Tlist_WinWidth = 20
" let Tlist_Use_Horiz_Window = 1

" --- OmniCppComplete Start ---
" -- required --
set nocp " non vi compatible mode
" filetype plugin on " enable plugins

" -- optional --
" auto close options when exiting insert mode
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
set completeopt=menu,menuone

" -- configs --
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype (i.e. parameters) in popup window
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_DisplayMode = 1
let OmniCpp_DefaultNamespaces = ["std"]

" --- OmniCppComplete End ---

" {{{ Autocompletion using the TAB key
" This function determines, wether we are on the start of the line text (then
" tab indents) or
" if we want to try autocompletion
function! InsertTabWrapper()
    let col = col('.') - 1
    if !col || getline('.')[col - 1] !~ '\k'
        return "\<TAB>"
    else
        if pumvisible()
            return "\<C-N>"
        else
            return "\<C-N>\<C-P>"
        endif
    endif
endfunction
" Remap the tab key to select action with InsertTabWrapper
inoremap <tab> <c-r>=InsertTabWrapper()<cr>
" }}} Autocompletion using the TAB key
--------------------------------------------------------------------------------
4. 使用

4-1. 跳转到定义
Ctrl + ']'

4-2. 返回
Ctrl + 't'

$ cd /usr/local/src/postgresql-9.1.2/src

$ ctags -R --langmap=c:+.h --languages=c,php --exclude=*.in (只包含*.c和*.h和*.php 文件, 不包含 *.in 文件)
$ ctags --list-maps (查看支持语言类型)
$ ll tags
-rw-rw-r-- 1 go go 4927781  2月  6 10:21 tags
$ vim bin/psql/startup.c

0 件のコメント:

コメントを投稿