Nope Nope - 10 days ago 7x
Python Question

VIM: Save and Run at the same time?

I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.

Thanks for your help.


Option 1:

Write a function similar to this and place it in your startup settings:

function myex()
   execute ':w'
   execute ':!!'

You could even map a key combo to it-- look a the docs.

Option 2 (better):

Look at the documentation for remapping keystrokes - you may be able to accomplish it through a simple key remap. The following works, but has "" hardcoded. Perhaps you can dig in and figure out how to replace that with the current file?

:map <F2> <Esc>:w<CR>:!<CR>

After mapping that, you can just press F2 in command mode.

imap, vmap, etc... are mappings in different modes. The above only applies to command mode. The following should work in insert mode also:

:imap <F2> <Esc>:w<CR>:!<CR>a

Section 40.1 of the VIM manual is very helpful.