Uncrustify を Vim と適切に統合するには、以下を に追加します.vimrc
:
" Restore cursor position, window position, and last search after running a
" command.
function! Preserve(command)
" Save the last search.
let search = @/
" Save the current cursor position.
let cursor_position = getpos('.')
" Save the current window position.
normal! H
let window_position = getpos('.')
call setpos('.', cursor_position)
" Execute the command.
execute a:command
" Restore the last search.
let @/ = search
" Restore the previous window position.
call setpos('.', window_position)
normal! zt
" Restore the previous cursor position.
call setpos('.', cursor_position)
endfunction
" Specify path to your Uncrustify configuration file.
let g:uncrustify_cfg_file_path =
\ shellescape(fnamemodify('~/.uncrustify.cfg', ':p'))
" Don't forget to add Uncrustify executable to $PATH (on Unix) or
" %PATH% (on Windows) for this command to work.
function! Uncrustify(language)
call Preserve(':silent %!uncrustify'
\ . ' -q '
\ . ' -l ' . a:language
\ . ' -c ' . g:uncrustify_cfg_file_path)
endfunction
これで、この関数 ( Uncrustify
) をキーの組み合わせにマップするか、私が使用する便利なトリックを実行できます。特に C++ の Vim 設定をオーバーライドできるファイルを作成し、~/.vim/after/ftplugin/cpp.vim
そこに次の行を追加します。
autocmd BufWritePre <buffer> :call Uncrustify('cpp')
これは基本的に事前保存フックを追加します。ファイルを C++ コードで保存すると、以前に提供した構成ファイルを使用して Uncrustify によって自動的にフォーマットされます。
たとえば、Java に対しても同じことができます~/.vim/after/ftplugin/java.vim
。
autocmd BufWritePre <buffer> :call Uncrustify('java')
あなたは要点を得ました。
注: ここに記載されているものはすべて十分にテストされており、私が毎日使用しています。