Skip to content
Advertisement

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.

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement