From: khai [mailto:[hidden email]]
Sent: Monday, October 10, 2005 3:39 PM
To: [hidden email] Subject: Automatically run a shell command when exit vim
> Is there a way to automatically run a shell command when exit
These are two entirely different things ...
To do something just before vim closes down:
au VimLeave * some_ex_command
> Or is there a way to prevent vim from saving the buffer when
> it contains syntax errors?
IIUC, once a "write" command has been invoked, there's no way to abort
it cleanly, so to stop an "incorrect" file from being written, you
either have to either:
1) check it for correctness *before* allowing it to be written, and
abort the process on an error (which means subverting any Normal and Ex
commands which would cause the file to be written using nmaps, cmaps,
:cabbrevs or :command redefinitions), or
2) create a BufWritePre autocommand which calls a function which does
the checking and [on an "error"] changes something which will cause the
write itself to fail (eg, :set readonly, or :set nomodifiable or some
other trick) -- very nasty, whatever you do here.
For approach #1, you need to trap *all* commands which write the file,
including any shorter forms of the Ex commands. You can probably ignore
the bang (!) forms, since the should never be used unless you *want* to
write in spite of everything.
Normal -- ZZ
Ex -- write, wall, wnext, wNext, wprevious, wq, wqall, xit, update,
On 2005-10-10, khai <[hidden email]> wrote:
> Is there a way to automatically run a shell command when exit vim?
Yes, but how you should run the command depends on what you want it
to do. You can, for example, use the VimLeave autocommand event to
trigger the shell command, e.g.,
au VimLeave * call system('date >> datelog')
> Or is there a way to prevent vim from saving the buffer when it
> contains syntax errors?
I think so. One way to do this would be to use the BufWriteCmd
autocommand event to trap the attempt to save the buffer and call a
function that would search for syntax highlighting that indicates an
error, something like Dr. Chip's engspchk.vim script does in the
SpchkNxt() function. If the search finds an error then echo a
message to the user and return, else write the buffer and return.
Gary Johnson | Agilent Technologies
[hidden email] | Wireless Division
| Spokane, Washington, USA
Re: Automatically run a shell command when exit vim
Tim Chase wrote:
>> I am unclear whether you'd want to do this from within vim, although:
>> vim ./somefile && my_commad_to_run_afterwards
>> ... from the CLI, would probably work just as well.
> Thomas's suggestion should work, though may need the "-f" ("--nofork")
> option if you're launching gvim to keep it from spawning in the
> background. Alternatively, you can add it to your 'guioptions'.
> :help -f
... however, this wouldn't work for the Windows GUI, where, in order to
avoid a fork, you must use "start /wait gvim.exe" -- or else use console
Vim, which doesn't fork. Also, the && separator is understood (somewhat)
by CMD.EXE but not by COMMAND.COM.