Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 723
Yishu Fang Avatar asked Dec 07 '22 01:12

Yishu Fang


2 Answers

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.

like image 93
perreal Avatar answered Feb 15 '23 12:02

perreal


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 | !!
like image 20
merlin2011 Avatar answered Feb 15 '23 12:02

merlin2011