python VIM:同时保存和运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/601039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:26:40  来源:igfitidea点击:

VIM: Save and Run at the same time?

pythonvim

提问by Nope

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.

我做了很多 Python 快速模拟的东西,我不断地保存 (:w) 然后运行 ​​(:!!)。我想知道,有没有办法结合这些动作。也许是“保存并运行”命令。

Thanks for your help.

谢谢你的帮助。

采纳答案by gahooa

Option 1:

选项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 combo to it-- look a the docs.

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



Option 2 (better):

选项 2(更好):

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?

查看重新映射击键的文档 - 您可以通过简单的键重新映射来完成它。以下工作,但有“filename.py”硬编码。也许您可以深入研究并弄清楚如何用当前文件替换它?

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

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

映射后,您只需在命令模式下按 F2 即可。

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

imap、vmap 等...是不同模式下的映射。以上仅适用于命令模式。以下内容也应在插入模式下工作:

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

Section 40.1 of the VIM manual is very helpful.

VIM 手册的第 40.1 节非常有帮助。

回答by Douglas Mayle

Okay, the simplest form of what you're looking for is the pipe command. It allows you to run multiple cmdline commands on the same line. In your case, the two commands are write `w` and execute current file `! %:p`. If you have a specific command you run for you current file, the second command becomes, e.g. `!python %:p`. So, the simplest answer to you question becomes:

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

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

One last thing to note is that not all commands can be chained. According to the Vim docs, certain commands accept a pipe as an argument, and thus break the chain...

最后要注意的是,并非所有命令都可以链接。根据Vim 文档,某些命令接受管道作为参数,从而打破链......

回答by strager

Use the autowriteoption:

使用自动写入选项:

: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 [...]

写入文件的内容,如果它已被修改,在每个 :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :! , :make, CTRL-] 和 CTRL-^ 命令 [...]

回答by Rook

Here you go:

干得好:

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

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

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

保存并运行(尽管您必须处于 n 模式 - 只需添加 esc 和 for i 模式)

回答by Dimitri Tcaciuc

Command combination seems to work through |character, so perhaps something like aliasing :w|!your-command-hereto a distinct key combination?

命令组合似乎通过|字符起作用,所以也许类似于:w|!your-command-here对不同组合键的别名 ?

回答by 0x89

Another possibility:

另一种可能:

au BufWriteCmd *.py write | !!

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

尽管run每次保存时都会这样做,但这可能不是您想要的。

回答by qeatzy

In vim, you could simply redirect any range of your current buffer to an external command (be it bash, python, or you own python script).

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

# redirect whole buffer to python
:%w !python

suppose your current buffer contain two lines as below,

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

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

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

然后:%w !python将运行它,无论是否保存。并在您的终端上打印如下内容,

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

Of course, you could make something persistent, eg, some keymaps.

当然,您可以制作一些持久的东西,例如,一些键盘映射。

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

first one run current line, second one run visual selection, via python interpreter.

第一个运行当前行,第二个运行视觉选择,通过 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, the second redirect the selected cmdline range (here dot .,
which mean current line) to external command (here 'python').

for cmdline range, see

对于 cmdline 范围,请参阅

:h cmdline-ranges

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

不低于一,这与正常命令有关,而不是 cmdline 一。

:h command-range

inspired by https://stackoverflow.com/a/19883963/3625404

灵感来自https://stackoverflow.com/a/19883963/3625404

回答by Alex Cory

This is what I put in my .vimrcand works like a charm

这就是我放进去的东西.vimrc,就像一个魅力

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

Of course you need to run your shell command once before this so it knows what command to execute.

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

Ex:

前任:

:!node ./test.js

回答by sjh

I got the following from the vim tips wiki:

我从 vim 提示 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 changed new to vnew on the third line, then for python i have the following

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

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 ... also hits c-w so i only have to press h/c to close it / move back to my code.

点击 f9 保存、运行并将输出转储到新的垂直拆分暂存缓冲区中,以便于轻松拉动/保存等......也点击 cw,所以我只需要按 h/c 关闭它/移回我的代码。

回答by user1634530

Try making it inside the Bash.

尝试在 Bash 中制作它。

In case of a C file called t.c, this is very convenient:

对于名为 的 C 文件t.c,这非常方便:

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

The and symbols (&&) ensure that one error message breaks the command chain.

和符号 ( &&) 确保一条错误消息会中断命令链。

For Python this might be even easier:

对于 Python,这可能更容易:

vi t.py && python t.py