Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Last active October 14, 2023 07:46
Show Gist options
  • Save yukpiz/fe42c931375630fdbbe8 to your computer and use it in GitHub Desktop.
Save yukpiz/fe42c931375630fdbbe8 to your computer and use it in GitHub Desktop.
Write C++ in Vim.

VimでC++を書く設定や操作

1. 設定の書き方

Vimでは特定のファイルを開いた時にfiletypeが設定されます。
filetypeはset filetype?で確認でき、 filetype毎の設定を以下のように設定できます。

autocmd FileType cpp {設定項目}

ただ、C++ファイルを開いた時の設定を全てautocmdでやると、
非常に長くなり読みづらいので以下のようにします。

augroup CppConfigurations
    autocmd! CppConfigurations
    autocmd FileType cpp call s:CppConfigurations()
augroup END

function! s:CppConfigurations()
    {ここにC++ファイルを開いた時の設定を書きまくる}
endfunction

2. includeディレクトリを設定する

includeディレクトリをpathに設定する事で、
ヘッダファイルをgfで開く事ができるようになります。

setlocal path=/usr/include,/usr/local/include

3. インデント文字を設定する

setlocal tabstop=4
setlocal shiftwidth=4
setlocal noexpandtab

4. includeを補助する

<Space>iiでinclude行の末尾にジャンプして、インサートモードに入ります。

nnoremap <buffer><silent> <Space>ii :execute "?".&include<CR> :noh<CR> o

5. boostライブラリのハイライトを有効にする

BOOST_PP_*のキーワードに対してハイライトを有効にします。

syntax match boost_pp /BOOST_PP_[A-z0-9_]*/
highlight link boost_pp cppStatement

6. コメント操作

NeoBundle 'tyru/caw.vim'
nmap \c <Plug>(caw:I:toggle)
vmap \c <Plug>(caw:I:toggle)
nmap \C <Plug>(caw:I:uncomment)
vmap \C <Plug>(caw:I:uncomment)

7. アウトラインを表示

NeoBundle 'Shougo/unite.vim'
NeoBundle 'Shougo/unite-outline'
nmap ,out :Unite outline -winheight=10<CR>

8. インクルードファイルの一覧を表示

nmap ,inc :Unite file_include -winheight=10<CR>

9. ハイライトする括弧ペアに<>を追加

setlocal matchpairs+=<:>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment