Vim: execute current file?

If I have a file with a shebang line (e.g. #!/bin/bash) open in Vim and the file has execute permissions (i.e. chmod +x) I know I can type this to execute it without leaving the editor:

:! %:p
  • : for command mode
  • ! to run a shell command
  • % to refer to the file in the current buffer
  • :p to use the full path of the current file

Is there a shorter shortcut for this frequent task?

e.g. there is a ZZ shortcut for :wq, etc.


Solution 1:

:!%:p

,without the spaces, is shorter.

If you want an even shorter shortcut, you can create a custom mapping:

nnoremap <F9> :!%:p

or the more "mnemonic":

nnoremap <leader>r :!%:p

Solution 2:

If you haven't set permissions you can run:

:! sh %

Solution 3:

None of the previous answers work if your filename/directory path has spaces in it. Simple fix.

:!"%:p"

Solution 4:

After you've executed that once, a short :!! will repeat it.