I have a few hotkeys mapped in my .vimrc (see below) for compiling and running autohotkey scripts but I can either be annoyed by the quest for the any key or I can run them silent and be unaware if there are errors.

autocmd filetype autohotkey nnoremap <S-F5> :w <bar> silent exec '!"C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe" /in '.shellescape('%')<CR>
autocmd filetype autohotkey nnoremap <C-F5> :w <bar> silent exec '!"C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe" /in '.shellescape('%').' && start .\'.shellescape('%:r')<CR>
autocmd filetype autohotkey nnoremap <S-F6> :silent exec '!Taskkill /IM '.shellescape('%:r').'.exe /F'<CR>
autocmd filetype autohotkey nnoremap <C-S-F5> :silent exec '!Taskkill /IM '.shellescape('%:r').'.exe /F' <bar> silent exec '!"C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe" /in '.shellescape('%').' && start .\'.shellescape('%:r')<CR>

How do I have these exec commands keep the cmd window open only if there is an error?

(e.g. if I run the ctrl+F5 and the compiled version is already running, the compile will fail because it can't write to the .exe. (Yes, I know in that particular case there's an evil modal popup but in other cases, there's not good feedback of failure.))


If the AutoHotkey compiler properly reports its success via the exit status (I haven't checked), you can keep the :silent and just delay the closing of the command prompt by executing the pause command (which waits for a keypress) in case compilation fails:

:silent exec '!"C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe" /in '.shellescape('%') . ' || pause'<CR>

and here's a reference for the command failed redirect (double pipe ||)


You could invoke the external command via :let output = system(...), and then conditionally :echo or discard the output based on v:shell_error (or even by inspecting the output by matching "interesting" strings, if you want to ignore some compiler errors or as a solution if the exit status always is 0).

Actually, you then should go the whole round and write a proper compiler plugin (:help :compiler, :help write-compiler-plugin). With this, you can trigger via :make, and can use the quickfix list to view and navigate to compiler errors (assuming the error output is properly structured and parsable).

Additional critique

nnoremap <S-F5> ...

As these mappings only apply to AutoHotkey buffers, you should limit the scope of those mappings via :nnoremap <buffer> ...

I would recommend putting any settings, mappings, and filetype-specific autocmds into ~/.vim/ftplugin/{filetype}_whatever.vim (or {filetype}/whatever.vim; cp. :help ftplugin-name) instead of defining lots of :autocmd FileType {filetype}; it's cleaner and scales better; requires that you have :filetype plugin on, though. Settings that override stuff in default filetype plugins should go into ~/.vim/after/ftplugin/{filetype}.vim instead.

| improve this answer | |
  • Perfect. I forgot about the 'if fail' || redirect. (added link for ref in your answer) I'd love to dive into doing it properly as you suggest but I have to hack it "for now" because there's no spare time. Great additional feedback as well. I had these in the ahk.vim but they weren't being picked up in gvim; that's solved but I forgot to move them back. I'll do that now. – Still.Tony Sep 19 '18 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.