12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

保存時に勝手にclang-formatする

Last updated at Posted at 2014-06-24

表題の通り、現在編集中の*.[ch]ppファイルを保存時に自動的clang-formatで整形します

.vimrcに以下を追加します

function! s:clang_format()
  let now_line = line(".")
  exec ":%! clang-format"
  exec ":" . now_line
endfunction

if executable('clang-format')
  augroup cpp_clang_format
    autocmd!
    autocmd BufWrite,FileWritePre,FileAppendPre *.[ch]pp call s:clang_format()
  augroup END
endif

システムにclang-formatがインストールされていればvim側はプラグイン等一切必要ありません。

2014/6/25追記
不具合を修正しました。
以前のバージョンは現在の編集情報を廃棄するので注意してください。
詳しくは編集履歴を確認してください。

12
9
0

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
12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?