我在 _vimrc 中添加了一行。
map <F4> :w<cr>:!python %<cr>
如果文件包含名称,则可以运行,如果文件是新编辑的,不包含名称,则当我按F4时,它无法运行。
我想让我的配置更智能,又不想保存,给它起个名字然后按F4,我该如何修改map <F4> :w<cr>:!python %<cr>
使无名 python 文件运行?
最佳答案
使用 :w !{cmd}
怎么样? ? (这不需要您在运行命令之前保存)。
:map <F4> :w !python<cr>
根据vim help :w_c
:
:[range]w[rite] [++opt] !{cmd}
Execute {cmd} with [range] lines as standard input (note the space in front of the '!'). {cmd} is executed like with ":!{cmd}", any '!' is replaced with the previous command |:!|.
注意如果 Python 程序本身使用文件名,则这将无法按预期工作。 (例如, __file__
将产生 '<stdin>'
)
关于python - 如何映射 F4 在 vim 上运行无名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931143/