Search code examples
linuxvimvi

How to make vi/vim execute "rewrite filter on write" for given file extension?


I would like to be able to write handy "short version" in configuration file.

The obvious way to achieve it would be "on save filter" executed by vi/vim.
The filter script would rewrite "short version" to "long version" expected by the app.


Solution

  • Assuming code is an ex command that does your filtering (which could be calling a vimscript function, a simple %substitute, or even a complex external filter with %!), you can do something like

    " ~/.vim/after/ftplugin/myfiletype.vim
    augroup FilterOnSave
      autocmd! * <buffer>
      autocmd BufWritePre <buffer> code
    augroup END