14

私の vimrc では、次のコマンドで Uncrustify を呼び出します。

%!uncrustify -l CPP -c D:\uncrustify\default.cfg

その後、一部のコードで Windows Fatal エラーが発生します。

しかし、コンソールで -f オプションを使用して同じコードで uncrustify を呼び出すと、エラーは発生しません。

将来このようなエラーを回避するために vimrc を変更するにはどうすればよいですか? このエラーを引き起こす原因は何ですか?

4

3 に答える 3

18

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')

あなたは要点を得ました。

: ここに記載されているものはすべて十分にテストされており、私が毎日使用しています。

于 2013-03-20T01:44:18.620 に答える