Robottinosino
Robottinosino

Reputation: 10882

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

Is there a shorter shortcut for this frequent task?

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

Upvotes: 107

Views: 53741

Answers (7)

user8234870
user8234870

Reputation:

In order to execute the current file in vim if there are spaces in file name

:!./"%"

Upvotes: 0

armagedescu
armagedescu

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

nullUser
nullUser

Reputation: 1833

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

:!"%:p"

Upvotes: 25

James Dunmore
James Dunmore

Reputation: 1310

If you haven't set permissions you can run:

:! sh %

Upvotes: 31

Ingo Karkat
Ingo Karkat

Reputation: 172540

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

Upvotes: 15

romainl
romainl

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

jpmuc
jpmuc

Reputation: 1154

You can add a key mapping to your .vimrc

map <F5> :!%

Upvotes: 6

Related Questions