savevimastyle

In vim I want to run astyle on the file i'm on when I save


When I save in vim I want to be able to run 'astyle sourceCodeThatImCurrentlyIn.cpp'. So a combination of :w and :!astyle source.c ? Is this possible?

Thank you, Ejay


Solution

  • Take a look at the Vim auto-commands:

    :autocmd BufWritePost *.c execute '!astyle' shellescape(expand('%'), 1)
    

    The % is replaced by the path of the current file. BufWritePost is called after the file is saved. There is also BufWritePre for doing stuff before the file is saved.

    If you want to use this permanently then put it into your .vimrc configuration file.