I would take advantage of the shell's ||
operator here:
map <F5> :w<CR>:exe printf('!poetry run %s || python %s', shellescape(@%), shellescape(@%))<CR>
The way this works in the shell is that the ||
or "or" operator only runs what is on the right-hand side if whatever is on the left-hand side returns with an exit code of 0 (indicating success). It's the same as in ||
and &&
in most programming languages:
if False || never_actually_tested:
If you don't want to do that then you need to use some more complex logic and check v:shell_error
; I would put that in a function:
fun! s:poetry()
exe '!poetry' shellescape(@%)
if v:shell_error > 0
exe '!python' shellescape(@%)
" Or: call feedkeys("\<F6>") to run whatever "F6" is.
endif
endfun
map <F5> :w<CR>:call <SID>poetry()<CR>