5

I am trying to do something like this, from a function:

nmap <F5> :!python %

Pressing F5 in this case causes the command :!python % to be left in the command line. This is preferred for me, because occasionally I may want to add extra command arguments or stuff to the end of the command.

However, I'm not sure how to create this behavior from a vim function. The execute() function simply executes the command directly, with no chance of editing, while the normal command doesn't seem to work.

nmap <F5> :call MyDebugFunction()<cr>

function! MyDebugFunction()
  " This just executes right away with no chance of editing
  execute("python %")
  " Neither of these work
  normal! :!python %
  execute "normal! :!python%"
endfunction
5

This is one of the very rare cases in which feedkeys() is actually called for. Doing :call feedkeys(':!python %') should work, since feedkeys puts stuff directly on vim's input buffer.

| improve this answer | |
6

while the normal command doesn't seem to work

normal must always return to Normal. It is considered normal in Vim.

I am trying to do something like this, from a function

Not really useful for this simple example, but, in general, there are many situations one may want it. Simply make use of :h map-expr (or, alternatively, the expression register), like this:

nnoremap <expr>F5 MyFunction()

function! MyFunction() abort
    " foo, bar, baz..
    return ":!python %"
endfunction
| improve this answer | |

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.