Yishu Fang
Yishu Fang

Reputation: 9978

In vim, can I combine ":w" and running current script together?

Every time I edit my python script, I have to type :w and then :!./myscript.py (run current script).

Can I combine those two commands together?

Upvotes: 4

Views: 82

Answers (3)

perreal
perreal

Reputation: 98108

Write this in your .vimrc:

function! SaveAndRun()
    w
    !%:p
endfunction

nmap <F2> :call SaveAndRun()<cr>

and it will execute the current file when you press f2 in normal mode.

Upvotes: 6

merlin2011
merlin2011

Reputation: 75619

Define a function in your .vimrc and then define a command to invoke it.

function DoMyStuff()
    :w
    :!./myscript.py
endfunction

command W exec DoMyStuff()

Then, you can call it with :W.

If I am interpret the title of your question literally, and you just want to execute the last executed command, you can use !! in command mode to execute the last external command. Combined with the pipe, this looks like the following.

:w | !!

Upvotes: 5

CDahn
CDahn

Reputation: 1876

Yes you can using a pipe character between the commands:

:w | !./myscript.py

Upvotes: 4

Related Questions