代码之家  ›  专栏  ›  技术社区  ›  Nope

VIM:同时保存和运行?

  •  35
  • Nope  · 技术社区  · 17 年前

    也许是“保存并运行”命令。

    12 回复  |  直到 5 年前
        1
  •  79
  •   Douglas Mayle    17 年前

    好吧,你要找的最简单的形式是管道命令。它允许您在同一行上运行多个cmdline命令。在你的例子中,这两个命令是write`w`和执行当前文件`!p如果您为当前文件运行了一个特定的命令,则第二个命令将变为,例如`!python%:p`。所以,对于你的问题,最简单的答案是:

    :w | ! %:p
     ^ ^ ^
     | | |--Execute current file
     | |--Chain two commands
     |--Save current file
    

    Vim docs

        2
  •  30
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    编写一个类似于此的函数,并将其放置在启动设置中:

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

    你甚至可以将一个组合键映射到它上——看看文档。


    选项2(更好):

    :map <F2> <Esc>:w<CR>:!filename.py<CR>
    

    :imap <F2> <Esc>:w<CR>:!filename.py<CR>a
    

        3
  •  17
  •   strager    17 年前

    autowrite 选项:

    :set autowrite
    

    如果文件已被修改,请在每个:next、:reback、:last、:first、:previous、:stop、:suspend、:tag上写入文件内容, ,:make,CTRL-]和CTRL-^命令[…]

        4
  •  6
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    干得好:

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

    保存并运行(你必须在 n 不过模式-只需添加 以及a 模式)。

        5
  •  5
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    | :w|!your-command-here

        6
  •  4
  •   0x89    15 年前

    另一种可能性:

    au BufWriteCmd *.py write | !!
    

    run

        7
  •  4
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    在Vim中,您可以简单地将当前缓冲区的任何范围重定向到外部命令(无论是Bash、Python解释器还是您自己的Python脚本)。

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

    假设您当前的缓冲区包含如下两行,

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

    :%w !python

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

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

    第一个,运行当前线路。第二个,通过Python解释器运行视觉选择。

    #!! 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').
    

    :h cmdline-ranges
    

    不是下面的那个,它涉及正常命令,不是cmdline命令。

    :h command-range
    

    Execute current line in Bash from Vim .

        8
  •  2
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    .vimrc

    nnoremap <leader>r :w<CR>:!!<CR>
    

    当然,在此之前,您需要运行一次shell命令,以便它知道要执行什么命令。

    例子:

    :!node ./test.js
    
        9
  •  1
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    我从Vim技巧维基中得到了以下内容:

    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
    

    但我在第三行将new更改为vnew,然后对于Python,我有以下内容:

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

    F9

        10
  •  0
  •   Sebastián Palma    9 年前

    试着在Bash中实现它。

    t.c

    vi t.c &&  cc t.c -o t && ./t
    

    &&

    对于Python来说,这可能更容易:

    vi t.py && python t.py
    
        11
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前

    插入模式也是 :

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

        12
  •  -12
  •   Peter Mortensen Pieter Jan Bonestroo    5 年前
    1. IDLE . F5

    2. 考虑切换到 Komodo IDE 。您可以定义一个命令,以便 F5 什么都做。