Save and run at the same time in Vim

前端 未结 12 531
悲哀的现实
悲哀的现实 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:39

    Another possibility:

    au BufWriteCmd *.py write | !!
    

    Though this will run every time you save, which might not be what you want.

    0 讨论(0)
  • 2021-01-30 03:39

    In Vim, you could simply redirect any range of your current buffer to an external command (be it Bash, the Python interpreter, or you own Python script).

    # Redirect whole buffer to 'python'
    :%w !python
    

    Suppose your current buffer contains two lines as below,

    import numpy as np
    print np.arange(12).reshape(3, 4)
    

    then :%w !python will run it, be it saved or not. And print something like below on your terminal,

    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]
    

    Of course, you could make something persistent, for example, some keymaps.

    nnoremap <F8> :.w !python<CR>
    vnoremap <F8> :w !python<CR>
    

    The first one, run the current line. The second one, run the visual selection, via the Python interpreter.

    #!! Be careful, in Vim ':w!python' and ':.w !python' are very different. The
    first write (create or overwrite) a file named 'python' with contents of
    current buffer, and the second redirects the selected cmdline range (here dot .,
    which mean current line) to external command (here 'python').
    

    For cmdline range, see

    :h cmdline-ranges
    

    Not the below one, which concerning normal command, not cmdline one.

    :h command-range
    

    This was inspired by Execute current line in Bash from Vim.

    0 讨论(0)
  • 2021-01-30 03:41

    I got the following from the Vim tips wiki:

    command! -complete=file -nargs=+ shell call s:runshellcommand(<q-args>)
    function! s:runshellcommand(cmdline)
      botright vnew
      setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
      call setline(1,a:cmdline)
      call setline(2,substitute(a:cmdline,'.','=','g'))
      execute 'silent $read !'.escape(a:cmdline,'%#')
      setlocal nomodifiable
      1
    endfunction
    

    But I changed new to vnew on the third line, and then for Python I have the following:

    map <F9> :w:Shell python %<cr><c-w>
    

    Hitting F9 saves, runs, and dumps the output into a new vertically split scratch buffer, for easy yanking, saving, etc. It also hits c-w so I only have to press h/c to close it and move back to my code.

    0 讨论(0)
  • 2021-01-30 03:43

    Use the autowrite option:

    :set autowrite
    

    Write the contents of the file, if it has been modified, on each :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!, :make, CTRL-] and CTRL-^ command [...]

    0 讨论(0)
  • 2021-01-30 03:43

    Here you go:

    :nmap <F1> :w<cr>:!%<cr>
    

    Save and run (you have to be in n mode though - just add esc and a for i mode).

    0 讨论(0)
  • 2021-01-30 03:49

    This will work in insert mode too:

    " F5 => Save & Run python3 "
    nnoremap <F5> :w <CR> :!sh -c 'python3 %' <CR>
    inoremap <F5> <Esc> :w <CR> :!sh -c 'python3 %' <CR>
    

    I use it all the time to test stuff that is just too long to retype in the interpreter.

    0 讨论(0)
提交回复
热议问题