Reputation: 10882
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 fileIs there a shorter shortcut for this frequent task?
e.g. there is a ZZ
shortcut for :wq
, etc.
Upvotes: 107
Views: 53741
Reputation:
In order to execute the current file in vim if there are spaces in file name
:!./"%"
Upvotes: 0
Reputation: 2160
When starting vi, specify file path explicitly, like this "vi ./blablabla"
vi ./yourscript.pl
Then start with !%
The other variant is to invoke the vi command like this
!./%
Upvotes: 11
Reputation: 1833
None of the previous answers work if your filename/directory path has spaces in it. Simple fix.
:!"%:p"
Upvotes: 25
Reputation: 172540
After you've executed that once, a short :!!
will repeat it.
Upvotes: 15
Reputation: 196546
:!%: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
Upvotes: 109