6

I wish that each time I ":w" to save a .h/.cpp file in vim, vim will automatically run cpplint to check my format, and change the file if needed.

How to specify this with autocmd?

Thanks.

Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • 2
    Possible duplicate of [vim: how to execute automatically execute a shell command after saving a file?](http://stackoverflow.com/questions/4627701/vim-how-to-execute-automatically-execute-a-shell-command-after-saving-a-file) – melpomene Nov 19 '16 at 15:11

1 Answers1

11

If you want to use an autocmd for this, you can simply add this to your .vimrc:

autocmd BufWrite *.cpp :! cppcheck %

However, I would personally recommend using a syntax checking plugin for this. The very popular vim-syntastic supports cpplint out of the box. You can use the following line to set cpplint as the syntax checker for C++ files.

let g:syntastic_cpp_checkers = ['cpplint']

The advantage of using a plugin is that it will integrate with Vim and highlight where there are issues, rather than just dumping textual output to stdout.

PS: make sure cpplint is in your $PATH, without it neither approach will work.

Jonas
  • 1,021
  • 11
  • 17