Save and run at the same time in Vim

前端 未结 12 532
悲哀的现实
悲哀的现实 2021-01-30 03:13

I do a lot of Python quick simulation stuff and I\'m constantly saving (:w) and then running (:!!). Is there a way to combine these actions?

Maybe a "save and run&qu

12条回答
  •  走了就别回头了
    2021-01-30 03:58

    Option 1:

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

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

    You could even map a key combination to it -- look at the documentation.


    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 "filename.py" hardcoded. Perhaps you can dig in and figure out how to replace that with the current file?

    :map  :w:!filename.py
    

    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  :w:!filename.pya
    

    Section 40.1 of the Vim manual is very helpful.

提交回复
热议问题