Quantcast
Channel: Vimタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 5608

C言語用の.vimrcをつくりたい

$
0
0

Vim Advent Calendar 2013 の 21 日目の記事になります。
20 日目は @s_of_pさんによる、「おさらい autocmd/augroup」でした。

Advent Calendar も Qiita も初めてとなりますが、よろしくお願いします。

P.S.
というか19日めに @osyo-manga さんがC++での環境構築について書かれているので、
[C++][vim][clang]C++ Advent Calendar 2013 前編 - Vim で C++ のコーディングを行う
この記事ただの初心者.vimrc晒しになっている感がありますが、お許しください(´・ω・`)

C言語用の.vimrcをつくりたい

最近、仕事でVimを本格的に使いはじめ、主に使っているC言語で快適にコーディングしたいなぁと考えています。
Vimmer界隈ではC++erは多いようですが、C言語erは少ないようなそうでもないような・・・?

どうもclangをつかって静的解析するのが最近の流行っぽい?

ひとまず補完補助として以下のプラグインを使わせていただいてます
clang_completeはあんまり思った感じに補完できなくて、結局neocompleteの補完に頼ってる感が・・・
あとはアウトライン解析があるとコード移動が楽でいいですね

補完系
  • Shougo/neocomplete.vim
  • Shougo/neosnippet
  • Rip-Rip/clang_complete
アウトライン解析
  • Shougo/unite.vim
  • Shougo/unite-outline

とりあえず現状の.vimrc

ぼくの.vimrcをくらえっ!
会社の昼休みにちょこちょこ改造しているのですが、こぴぺ三昧で汚いで(ry

使用環境がころころ変わるので、
"apt-get か yum でvim持ってきてすぐ使える" 状態を保つのがコンセプト
余裕があるときにVimビルドして+lua してます

.vimrc
" -----------------------------------------------------------------------".vimrc" -----------------------------------------------------------------------" -----------------------------------------------------------------------" syntax hilightsyntax enable

" scriptencodingscriptencoding utf-8setencoding=utf-8" タブと行末スペースを可視化setlistsetlistchars=tab:^\ ,trail:~" TABsetexpandtabsettabstop=4setshiftwidth=4setsofttabstop=4" INDENTsetautoindentsetsmartindent" BACKSPACEsetbackspace=indent,eol,start" SCROLLsetscrolloff=5setlazyredraw" 更新時自動再読込みsetautoread" 編集中でも他のファイルを開けるようにするsethidden" スワップファイルを作らないsetnoswapfile" SEARCHsetincsearchsethlsearch" CURSOR LINE
augroup vimrc-auto-cursorline
  autocmd!
  autocmd CursorMoved,CursorMovedI * calls:auto_cursorline('CursorMoved')
  autocmd CursorHold,CursorHoldI * calls:auto_cursorline('CursorHold')
  autocmd WinEnter * calls:auto_cursorline('WinEnter')
  autocmd WinLeave * calls:auto_cursorline('WinLeave')lets:cursorline_lock =0function!s:auto_cursorline(event)ifa:event==# 'WinEnter'setlocalcursorlinelets:cursorline_lock =2elseifa:event==# 'WinLeave'setlocalnocursorlineelseifa:event==# 'CursorMoved'ifs:cursorline_lock
        if1<s:cursorline_lock
          lets:cursorline_lock =1elsesetlocalnocursorlinelets:cursorline_lock =0endifendifelseifa:event==# 'CursorHold'setlocalcursorlinelets:cursorline_lock =1endifendfunction
augroup END

" Status Linesetlaststatus=2" Space prefixnnoremap [space] <Nop>
nmap     <Space> [space]
xmap     <Space> [space]

" KEYMAPnnoremap<silent> [b :bprevious<CR>nnoremap<silent> ]b :bnext<CR>nnoremap<silent> [B :bfirst<CR>nnoremap<silent> ]B :blast<CR>nnoremap<silent> [space]p :bprevious<CR>nnoremap<silent> [space]n :bnext<CR>nnoremap<silent> [t :tabprevious<CR>nnoremap<silent> ]t :tabnext<CR>nnoremap<silent> [q :cprevious<CR>nnoremap<silent> ]q :cnext<CR>nnoremap<silent> [Q :cfirst<CR>nnoremap<silent> ]Q :clast<CR>noremap<Up><Nop>noremap<Down><Nop>noremap<Left><Nop>noremap<Right><Nop>" Edit vimrc
nmap ,v :edit $MYVIMRC<CR>"nmap [space]g :edit $MYGVIMRC<CR>" 横幅と縦幅を見て縦分割か横分割か決める" http://qiita.com/Linda_pp/items/392be95a195067d84fd8
command!-nargs=? -complete=command SmartSplit call<SID>smart_split(<q-args>)function!s:smart_split(cmd)ifwinwidth(0)>winheight(0) * 2vsplitelse
        split
    endifif!empty(a:cmd)
        execute a:cmd
    endifendfunctionnnoremap<silent><C-w><Space> :<C-u>SmartSplit<CR>" Ctrl + j, k, h, lでウインドウを移動できるようにnnoremap<C-j><C-w>jnnoremap<C-k><C-w>knnoremap<C-h><C-w>hnnoremap<C-l><C-w>l" CLIP BOARDsetclipboard=unnamed,autoselect

" set pastennoremap<silent>,p :<C-U>setpaste!<CR>:<C-U>echo("Toggle PasteMode => " . (&paste==0 ? "Off" : "On"))<CR>" command modesetwildmenusetwildmode=longest:full,full

" Vimを終了してもUndoif has('persistent_undo')setundofilesetundodir=./.vimundo,~/.vim/undo
endif" :vimgrepでの検索後QuickFixウインドウを自動的に開く
augroup grepopen
  autocmd!
  autocmd QuickfixCmdPost vimgrepcw
augroup END

" ファイルを開いた時にカーソルを前回編集時の位置に移動
augroup previousline
  autocmd!
  autocmd BufReadPost * if line("'\"")>1&& line("'\"")<= line("$")| exe "normal! g`\""|endif
augroup END

" Strip trailing whitespace" http://rails-bestpractices.com/posts/60-remove-trailing-whitespacefunction!<SID>StripTrailingWhitespaces()" Preparation: save last search, and cursor position.let _s=@/
    letl= line(".")letc=col(".")" Do the business:
    %s/\s\+$//e" Clean up: restore previous search history, and cursor positionlet @/=_s
    call cursor(l,c)endfunction

augroup trailingwhitespace
  autocmd BufWritePre * :call<SID>StripTrailingWhitespaces()
augroup END

" -----------------------------------------------------------------------"   neobundle" -----------------------------------------------------------------------setnocompatible" Be iMprovedif has('vim_starting')setruntimepath+=~/.vim/bundle/neobundle.vim/endifcall neobundle#rc(expand('~/.vim/bundle/'))" Let NeoBundle manage NeoBundle
NeoBundleFetch 'Shougo/neobundle.vim'" Recommended to install" After install, turn shell ~/.vim/bundle/vimproc, (n,g)make -f your_machines_makefile
NeoBundle 'Shougo/vimproc.vim', {
    \ 'build' : {
        \ 'windows' : 'make -f make_mingw32.mak',
        \ 'cygwin'  : 'make -f make_cygwin.mak ',
        \ 'mac'     : 'make -f make_mac.mak    ',
        \ 'unix'    : 'make -f make_unix.mak   ',
    \ },
\ }

filetype plugin indent on" Required!" Brief help" :NeoBundleList          - list configured bundles" :NeoBundleInstall(!)    - install(update) bundles" :NeoBundleClean(!)      - confirm(or auto-approve) removal of unused bundles" Bundlefunction!s:meet_neocomplete_requirements()return has('lua')&& (v:version>703||(v:version==703&& has('patch885')))endfunctionifs:meet_neocomplete_requirements()
    NeoBundle 'Shougo/neocomplete.vim'else
    NeoBundle 'Shougo/neocomplcache.vim'endif

NeoBundle 'Shougo/neosnippet'
NeoBundle 'Shougo/unite.vim'
NeoBundle 'Shougo/unite-outline'
NeoBundleLazy 'Shougo/vimshell.vim', {
    \ 'depends' : [ 'Shougo/vimproc.vim' ]
    \ }
NeoBundle 'Shougo/vimfiler'
NeoBundleLazy 'Rip-Rip/clang_complete', {
    \ 'autoload' : {
    \     'filetypes' : ['c','cpp'],
    \    },
    \ }
NeoBundle 'tpope/vim-fugitive'
NeoBundle 'kana/vim-smartchr'
NeoBundle 'sakuraiyuta/commentout.vim'
NeoBundle 'itchyny/lightline.vim'"NeoBundle 'cohama/vim-hier'
NeoBundle 'dannyob/quickfixstatus'"NeoBundle 'thinca/vim-quickrun'"NeoBundle 'thinca/vim-qfreplace'
NeoBundle 'thinca/vim-visualstar'
NeoBundle 'rhysd/clever-f.vim'" NeoBundle Setup" -----------------------------------------------------------------------"    'Shougo/neocomplete.vim'" -----------------------------------------------------------------------"    'Shougo/neocomplcashe'" -----------------------------------------------------------------------ifs:meet_neocomplete_requirements()" --------------------------------------------------------------------" neocomplete の設定" --------------------------------------------------------------------"Note: This option must set it in .vimrc(_vimrc).  NOT IN .gvimrc(_gvimrc)!" Disable AutoComplPop.letg:acp_enableAtStartup =0" Use neocomplete.letg:neocomplete#enable_at_startup =1" Use smartcase.letg:neocomplete#enable_smart_case =1" Set minimum syntax keyword length.letg:neocomplete#sources#syntax#min_keyword_length =3letg:neocomplete#lock_buffer_name_pattern ='\*ku\*'" Define dictionary.letg:neocomplete#sources#dictionary#dictionaries = {
        \ 'default' : '',
        \ 'vimshell' : $HOME.'/.vimshell_hist',
        \ 'scheme' : $HOME.'/.gosh_completions'
        \ }

" Define keyword.if!exists('g:neocomplete#keyword_patterns')letg:neocomplete#keyword_patterns = {}
    endifletg:neocomplete#keyword_patterns['default'] ='\h\w*'" Plugin key-mappings.inoremap<expr><C-g>     neocomplete#undo_completion()inoremap<expr><C-l>     neocomplete#complete_common_string()" Recommended key-mappings." <CR>: close popup and save indent.inoremap<silent><CR><C-r>=<SID>my_cr_function()<CR>function!s:my_cr_function()return neocomplete#close_popup() . "\<CR>"" For no inserting <CR> key."return pumvisible() ? neocomplete#close_popup() : "\<CR>"endfunction" <TAB>: completion.inoremap<expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"" <C-h>, <BS>: close popup and delete backword char.inoremap<expr><C-h> neocomplete#smart_close_popup()."\<C-h>"inoremap<expr><BS> neocomplete#smart_close_popup()."\<C-h>"inoremap<expr><C-y>  neocomplete#close_popup()inoremap<expr><C-e>  neocomplete#cancel_popup()" Enable omni completion.
    autocmd FileType css setlocalomnifunc=csscomplete#CompleteCSS
    autocmd FileType html,markdown setlocalomnifunc=htmlcomplete#CompleteTags
    autocmd FileType javascript setlocalomnifunc=javascriptcomplete#CompleteJS
    autocmd FileType python setlocalomnifunc=pythoncomplete#Complete
    autocmd FileType xml setlocalomnifunc=xmlcomplete#CompleteTags

    letg:neocomplete#force_overwrite_completefunc=1" Enable heavy omni completion.if!exists('g:neocomplete#sources#omni#input_patterns')letg:neocomplete#sources#omni#input_patterns = {}
    endifletg:neocomplete#sources#omni#input_patterns.php ='[^. \t]->\h\w*\|\h\w*::'letg:neocomplete#sources#omni#input_patterns.c='[^.[:digit:] *\t]\%(\.\|->\)'letg:neocomplete#sources#omni#input_patterns.cpp ='[^.[:digit:] *\t]\%(\.\|->\)\|\h\w*::'" For perlomni.vim setting." https://github.com/c9s/perlomni.vimletg:neocomplete#sources#omni#input_patterns.perl ='\h\w*->\h\w*\|\h\w*::'else" --------------------------------------------------------------------" neocomplcache の設定" --------------------------------------------------------------------"Note: This option must set it in .vimrc(_vimrc).  NOT IN .gvimrc(_gvimrc)!" Disable AutoComplPop.letg:acp_enableAtStartup =0" Use neocomplcache.letg:neocomplcache_enable_at_startup =1" Use smartcase.letg:neocomplcache_enable_smart_case =1" Set minimum syntax keyword length.letg:neocomplcache_min_syntax_length =3letg:neocomplcache_lock_buffer_name_pattern ='\*ku\*'" Enable heavy features." Use camel case completion."let g:neocomplcache_enable_camel_case_completion = 1" Use underbar completion."let g:neocomplcache_enable_underbar_completion = 1" Define dictionary.letg:neocomplcache_dictionary_filetype_lists = {
        \ 'default' : '',
        \ 'vimshell' : $HOME.'/.vimshell_hist',
        \ 'scheme' : $HOME.'/.gosh_completions'
            \ }

" Define keyword.if!exists('g:neocomplcache_keyword_patterns')letg:neocomplcache_keyword_patterns = {}
    endifletg:neocomplcache_keyword_patterns['default'] ='\h\w*'" Plugin key-mappings.inoremap<expr><C-g>     neocomplcache#undo_completion()inoremap<expr><C-l>     neocomplcache#complete_common_string()" Recommended key-mappings." <CR>: close popup and save indent.inoremap<silent><CR><C-r>=<SID>my_cr_function()<CR>function!s:my_cr_function()return neocomplcache#smart_close_popup() . "\<CR>"" For no inserting <CR> key."return pumvisible() ? neocomplcache#close_popup() : "\<CR>"endfunction" <TAB>: completion.inoremap<expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"" <C-h>, <BS>: close popup and delete backword char.inoremap<expr><C-h> neocomplcache#smart_close_popup()."\<C-h>"inoremap<expr><BS> neocomplcache#smart_close_popup()."\<C-h>"inoremap<expr><C-y>  neocomplcache#close_popup()inoremap<expr><C-e>  neocomplcache#cancel_popup()" Enable omni completion.
    autocmd FileType css setlocalomnifunc=csscomplete#CompleteCSS
    autocmd FileType html,markdown setlocalomnifunc=htmlcomplete#CompleteTags
    autocmd FileType javascript setlocalomnifunc=javascriptcomplete#CompleteJS
    autocmd FileType python setlocalomnifunc=pythoncomplete#Complete
    autocmd FileType xml setlocalomnifunc=xmlcomplete#CompleteTags

    letg:neocomplcache_force_overwrite_completefunc=1" Enable heavy omni completion.if!exists('g:neocomplcache_omni_patterns')letg:neocomplcache_omni_patterns = {}
    endifletg:neocomplcache_omni_patterns.php ='[^. \t]->\h\w*\|\h\w*::'letg:neocomplcache_omni_patterns.c='[^.[:digit:] *\t]\%(\.\|->\)'letg:neocomplcache_omni_patterns.cpp ='[^.[:digit:] *\t]\%(\.\|->\)\|\h\w*::'" For perlomni.vim setting." https://github.com/c9s/perlomni.vimletg:neocomplcache_omni_patterns.perl ='\h\w*->\h\w*\|\h\w*::'endif" -----------------------------------------------------------------------"    'Shougo/neosnippet'" -----------------------------------------------------------------------" Plugin key-mappings.
imap <C-k><Plug>(neosnippet_expand_or_jump)smap<C-k><Plug>(neosnippet_expand_or_jump)
xmap <C-k><Plug>(neosnippet_expand_target)" For snippet_complete marker.if has('conceal')setconceallevel=2concealcursor=iendif" -----------------------------------------------------------------------"    'Shougo/unite.vim'" -----------------------------------------------------------------------letg:unite_source_history_yank_enable =1letg:unite_source_file_mru_limit =200nnoremap<silent> [space]y :<C-u>Unite history/yank<CR>nnoremap<silent> [space]b :<C-u>Unite buffer<CR>nnoremap<silent> [space]f :<C-u>UniteWithBufferDir -buffer-name=filesfile<CR>nnoremap<silent> [space]r :<C-u>Unite -buffer-name=registerregister<CR>nnoremap<silent> [space]u :<C-u>Unite file_mru buffer<CR>" -----------------------------------------------------------------------"    'Shougo/unite-outline'" -----------------------------------------------------------------------letg:unite_split_rule ='botright'noremap [space]o<ESC>:Unite -vertical-winwidth=60 outline<Return>" -----------------------------------------------------------------------"    'Shougo/vimshell'" -----------------------------------------------------------------------" ,is: シェルを起動nnoremap<silent>,is :VimShell<CR>" ,ipy: pythonを非同期で起動"nnoremap <silent> ,ipy :VimShellInteractive python<CR>" ,irb: irbを非同期で起動"nnoremap <silent> ,irb :VimShellInteractive irb<CR>" ,ss: 非同期で開いたインタプリタに現在の行を評価させる"vmap <silent> ,ss :VimShellSendString<CR>" 選択中に,ss: 非同期で開いたインタプリタに選択行を評価させる"nnoremap <silent> ,ss <S-v>:VimShellSendString<CR>" -----------------------------------------------------------------------"    'Shougo/vimfiler'" -----------------------------------------------------------------------letg:vimfiler_as_default_explorer =1" -----------------------------------------------------------------------"    'Rip-Rip/clang_complete'" -----------------------------------------------------------------------setcompleteopt=menuone
letg:clang_complete_auto =0letg:clang_auto_select =0letg:clang_complete_copen =0letg:clang_use_library =1letg:clang_library_path ='/usr/lib/llvm'letg:clang_debug =0letg:clang_user_options ='-std= c++11'" -----------------------------------------------------------------------"    'kana/vim-smartchr'" -----------------------------------------------------------------------inoremap<buffer><expr>= smartchr#loop(' = ',' == ','=')" -----------------------------------------------------------------------"    'sakuraiyuta/commentout.vim'" -----------------------------------------------------------------------" -----------------------------------------------------------------------"    'itchyny/lightline.vim'" -----------------------------------------------------------------------letg:lightline = {
    \ 'active': {
    \   'left': [ [ 'mode','paste' ],
    \             [ 'fugitive','filename' ] ]
    \ },
    \ 'component_function': {
    \   'fugitive': 'MyFugitive',
    \   'readonly': 'MyReadonly',
    \   'modified': 'MyModified',
    \   'filename': 'MyFilename'
    \ },
    \ }

function! MyModified()if&filetype=="help"return""elseif&modifiedreturn"+"elseif&modifiablereturn""elsereturn""endifendfunctionfunction! MyReadonly()if&filetype=="help"return""elseif&readonlyreturn"RO"elsereturn""endifendfunctionfunction! MyFugitive()return exists('*fugitive#head') ? fugitive#head() : ''endfunctionfunction! MyFilename()return(''!= MyReadonly() ? MyReadonly() . ' ' : '') .
        \ (''!= expand('%:t') ? expand('%:t') : '[No Name]') .
        \ (''!= MyModified() ? ' ' . MyModified() : '')endfunction" -----------------------------------------------------------------------"   'cohama/vim-hier'" -----------------------------------------------------------------------" -----------------------------------------------------------------------"   'dannyob/quickfixstatus'" -----------------------------------------------------------------------" -----------------------------------------------------------------------" -----------------------------------------------------------------------" Installation check.
NeoBundleCheck

課題

NeoBundleの仕様がよくわかってない

遅延読み込み・・・? あっ、はい。
わりかし起動が遅くなってるので、なんとかしたいところ

clang_completeごにょごにょ

clang_completeで、補完候補がウィンドウとして表示されるんですが、
これは無効にできないんでしょうか・・・
aaa.png
あとは、neocompleteで絞込みができればいいんですけども、うーん。
というかclang_complete最近ぜんぜんメンテされてないんですねこれ

P.S.
この記事かいてる途中で@osyo-manga さんのmarching.vimがC言語対応してることに気がついた・・・・(遅い
http://d.hatena.ne.jp/osyo-manga/20131111/1384162596
ひとまず自分の環境で試してみて、記事修正します

P.P.S
どうやら

.vimrc
setcompleteopt=menuone

でウィンドウを表示させなくできるようです
Vim - 補完時に表示されるプレビューウィンドウを消す
@osyo-manga さん情報ありがとうございました!

quickrun.vimの活用

quickrun.vimを活用すれば、裏でビルドを走らせつつ、エラー箇所をハイライトとかできるらしいんですが、
そこまで手をつけれてない現状です・・・

terminal+Vim か GVim か

個人的にはtorminator 上でVimを使うのが好きなんですが、
VimFilter とか VimShell をもっと使いこなせれば、Gvim のほうが快適だったりするんでしょうか?


Viewing all articles
Browse latest Browse all 5608

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>