如何映射F4在vim上运行一个没有名字的文件?

2024-04-28 21:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在uvimrc里加了一行。在

map <F4> :w<cr>:!python %<cr>

如果文件包含名称,则可以运行;如果该文件是新编辑的且不包含名称,则当我按F4时,它无法运行。
我想让我的配置更智能,而且不想保存它,给它一个名称,然后按F4,如何修改map <F4> :w<cr>:!python %<cr>使这个没有名字的python文件运行?在


Tags: 文件名称编辑map智能名字crf4
1条回答
网友
1楼 · 发布于 2024-04-28 21:09:27

使用:w !{cmd}怎么样?(这不需要在运行命令之前保存)。在

:map <F4> :w !python<cr>

根据vimhelp :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程序本身使用filename,这将无法按预期工作。(例如,__file__将生成'<stdin>'

相关问题 更多 >