14

In my vimrc I call Uncrustify by this command:

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

After that on some code I get a Windows Fatal error:

But when I call uncrustify on the same code in the console using the -f option, there is no error.

How can I change my vimrc to avoid such errors in the future? What can invoke this error?

1
  • 3
    What is the error? Anyway, my money is on PATH environment variable being different in the console.
    – sehe
    Sep 11, 2012 at 18:15

3 Answers 3

18

In order to integrate Uncrustify with Vim properly, add the following to your .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

Now you can either map this function (Uncrustify) to a combination of keys or you could do the convenient trick that I use. Create a file ~/.vim/after/ftplugin/cpp.vim where you can override any Vim settings particularly for C++ and add the following line there:

autocmd BufWritePre <buffer> :call Uncrustify('cpp')

This basically adds a pre-save hook. Now when you save the file with C++ code it will be automatically formatted by Uncrustify utilizing the configuration file you supplied earlier.

For example, the same could be done for Java: in ~/.vim/after/ftplugin/java.vim add:

autocmd BufWritePre <buffer> :call Uncrustify('java')

You got the point.

NOTE: Everything presented here is well-tested and used every day by me.

1
  • 1
    The implementation here doesn't seem to properly handle when a syntax error is present in the uncrustify.cfg.
    – theorifice
    Jan 27, 2021 at 19:03
1

I have found the placing the following code into your .vimrc to be sufficient:

let g:uncrustifyCfgFile = '~/.uncrustify.cfg'
function! UncrustifyFunc(options) range
    exec a:firstline.','.a:lastline.'!uncrustify '.a:options
                \.' -c '.g:uncrustifyCfgFile.' -q -l '.&filetype
endfunction

command! -range=% UncrustifyRange <line1>,<line2>call UncrustifyFunc('--frag')
command! Uncrustify  let s:save_cursor = getcurpos() 
                  \| %call UncrustifyFunc('') 
                  \| call setpos('.', s:save_cursor)

Note this does assume that you have "uncrustify" binary in your $PATH.

It also assumes your configure file is ~/.uncrustify.cfg however you can change that by modifiying the g:uncrustifyCfgFile variable.

To call run

:Uncrustify

It also works on ranges (which was what promoted me to make this function). Visual selection example:

:'<,'>UncrustifyRange

I have only tired it with C, CPP and JAVA (I assume others will work as well)

0
0

In addition to @Alexander Shukaev's answer, adding the following will perform a check for uncrustify config correctness and not auto format if error is detected:

  let output = system('uncrustify -q -c ' . a:cfgfile)

  if v:shell_error != 0
      echo output
  endif

  return v:shell_error
endfunction

" Don't forget to add Uncrustify executable to $PATH (on Unix) or 
" %PATH% (on Windows) for this command to work.
function! Uncrustify(language)
  if CheckUncrustifyCfg(g:uncrustify_cfg_file_path)
      echo "Config file" g:uncrustify_cfg_file_path "has errors"
      echo "No formatting will be performed"
      return
  endif

  call Preserve(':silent %!uncrustify'
      \ . ' -q '
      \ . ' -l ' . a:language
      \ . ' -c ' . g:uncrustify_cfg_file_path)
endfunction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.