by Ingo Karkat
Edited files live in an environment, and you often need to do something before or after updating the file on disk. In an efficient workflow, this should be automated. Vim's :autocmd mechanism has the BufWritePre and BufWritePost events, but setting them up correctly is cumbersome. This plugin provides :OnWritePre and :OnWritePost commands that execute the passed Vim command before / after saving the current buffer, and additional commands to list and disable.
SEE ALSO *
- The DuplicateWrite.vim plugin (vimscript #4722) is a specialization for writing a buffer to additional file system locations.
- The ModelineCommands.vim plugin (vimscript #5767) can be used to automatically set up :OnWritePre / :OnWritePost commands for a file whenever it is opened, based on extended modelines.
:OnWritePre[!] {command}
Execute the Ex {command} before saving the current
buffer.
:OnWritePost[!] {command}
Execute the Ex {command} after saving the current
buffer.
By default, a forced :write! will skip the execution
of the {command}.
With [!], this is inverted: Execution only happens
with forced :write!, not with :write. This is
useful for decoupling the persistence from the
triggering, e.g. if the {command} is costly to execute
or has side effects.
:OnWriteOff Turn off all command executions for the current
buffer.
:OnWriteList List the command(s) for the current buffer.
A ! sigil means that the commands are only executed on
a forced :write!.
A [ sigil indicates execution before saving
A ] sigil indicates execution after saving.
:OnWriteListAll List the command(s) for all open buffers
that have any. The buffers are listed with
their number followed by the name; the commands are
listed in the following, indented lines, for example:
4 "OnWrite.txt" ->
make test
!ssh user@host redeploy
The code is hosted in a Git repo at https://github.com/inkarkat/vim-OnWrite You can use your favorite plugin manager, or "git clone" into a directory used for Vim packages. Releases are on the "stable" branch, the latest unstable development snapshot on "master".
This script is also packaged as a vimball. If you have the "gunzip" decompressor in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress the archive first, e.g. using WinZip. Inside Vim, install by sourcing the vimball or via the :UseVimball command.
vim OnWrite*.vmb.gz
:so %
To uninstall, use the :RmVimball command.
- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.025 or higher.
For a permanent configuration, put the following commands into your vimrc:
The command executions themselves can also trigger |autocmd|s; this may be needed to let customizations or plugins interfere, but could also lead to undesirable side effects. Because of this, the plugin ignores certain events (via 'eventignore') during its execution:
let g:OnWrite_EventIgnore = 'BufWritePre,BufWritePost'
The commands are stored in the buffer-local List variables b:OnWritePre and b:OnWritePost. You can use its existence / number of elements to determine whether / how many commands are configured, and use this e.g. in a custom 'statusline'. To programatically add commands, use the OnWrite#Add() function.
Report any bugs, send patches, or suggest features via the issue tracker at https://github.com/inkarkat/vim-OnWrite/issues or email (address below).
First published version.
- Started development.
Copyright: (C) 2026 Ingo Karkat - The VIM LICENSE applies to this plugin.
Maintainer: Ingo Karkat <ingo@karkat.de>