LoginSignup
25
27

More than 5 years have passed since last update.

C/C++の補完プラグインをclang_completeからvim-clangに乗り換えた

Last updated at Posted at 2015-12-07

以前までは clang_complete を使っていたけど、vimをpython2.7対応でビルドしないといけなかったり、設定がわりと面倒だったりしていた。そこで前々からチェックしていた後発の vim-clang に乗り換えることにした。

あとは江添さんも乗り換えたという記事を見たのもある。
本の虫: vim-clang: clangを使ったC++の静的補完Vimプラグイン

それぞれの特徴

clang_complete

  • libclangを使用する
  • libclangとの連携にpythonを使用。vimがpython2.7に対応している必要がある(python3使いたい時に不便)
  • 一時期開発が止まっていたが最近は有志の人が引き継いで更新されている
  • neocompleteと共存可能

vim-clang

  • pythonには依存していない
  • clangの実行ファイルを使用する
  • libclangには対応していない(でもそこまで遅い感じではない)
  • clang-formatの実行にも対応している
  • neocompleteと共存可能

その他

他にも有名どころとしてはYouCompleteMeなどがあるが、neocompleteと共存できないような気がする(未検証)。

設定例(Linux向け)

インストール(NeoBundleを使用、詳細は省略)

neobundle.toml
# complete
[[plugins]]
repository = 'Shougo/neocomplete.vim'
depends    = ['Shougo/neoinclude.vim', 'Shougo/context_filetype.vim']
insert     = 1

# C/C++
[[plugins]]
repository = 'justmao945/vim-clang'
filetypes  =  ['c', 'cpp']

neocompleteとvim-clangの設定例

" 'Shougo/neocomplete.vim' {{{
let g:neocomplete#enable_at_startup = 1

if !exists('g:neocomplete#force_omni_input_patterns')
  let g:neocomplete#force_omni_input_patterns = {} 
endif
let g:neocomplete#force_overwrite_completefunc = 1
let g:neocomplete#force_omni_input_patterns.c =
      \ '[^.[:digit:] *\t]\%(\.\|->\)\w*'
let g:neocomplete#force_omni_input_patterns.cpp =
      \ '[^.[:digit:] *\t]\%(\.\|->\)\w*\|\h\w*::\w*'

" }}}
"
" 'justmao945/vim-clang' {{{

" disable auto completion for vim-clang
let g:clang_auto = 0
" default 'longest' can not work with neocomplete
let g:clang_c_completeopt   = 'menuone'
let g:clang_cpp_completeopt = 'menuone'

function! s:get_latest_clang(search_path)
    let l:filelist = split(globpath(a:search_path, 'clang-*'), '\n')
    let l:clang_exec_list = []
    for l:file in l:filelist
        if l:file =~ '^.*clang-\d\.\d$'
            call add(l:clang_exec_list, l:file)
        endif
    endfor
    if len(l:clang_exec_list)
        return reverse(l:clang_exec_list)[0]
    else
        return 'clang'
    endif
endfunction

function! s:get_latest_clang_format(search_path)
    let l:filelist = split(globpath(a:search_path, 'clang-format-*'), '\n')
    let l:clang_exec_list = []
    for l:file in l:filelist
        if l:file =~ '^.*clang-format-\d\.\d$'
            call add(l:clang_exec_list, l:file)
        endif
    endfor
    if len(l:clang_exec_list)
        return reverse(l:clang_exec_list)[0]
    else
        return 'clang-format'
    endif
endfunction

let g:clang_exec = s:get_latest_clang('/usr/bin')
let g:clang_format_exec = s:get_latest_clang_format('/usr/bin')

let g:clang_c_options = '-std=c11'
let g:clang_cpp_options = '-std=c++11 -stdlib=libc++'


" }}}

その他、詳細は vim-clangのドキュメント を参照して下さい。

筆者のvimrc

dotfiles/vim at master · koara-local/dotfiles

参考

Rip-Rip/clang_complete
justmao945/vim-clang

Vim エディターのスクリプトの作成: 第 3 回 組み込みリスト
Vimスクリプト基礎文法最速マスター - 永遠に未完成

25
27
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
27