如何在Vi(m)中执行我正在编辑的文件

Ale*_*tov 95 vim exec

如何执行我在Vi(m)中编辑的文件并在分割窗口中输出(如在SciTE中)?

当然我可以这样执行:

:!scriptname
Run Code Online (Sandbox Code Playgroud)

但是,是否可以避免编写脚本名称以及如何在分割窗口中获取输出而不是屏幕底部?

R. *_*des 107

make命令.它运行makeprg选项中的命令集.使用%作为当前文件名的占位符.例如,如果您正在编辑python脚本:

:set makeprg=python\ %
Run Code Online (Sandbox Code Playgroud)

是的,你需要逃离这个空间.在此之后你可以简单地运行:

:make
Run Code Online (Sandbox Code Playgroud)

如果您愿意,可以设置autowrite选项,它会在运行之前自动保存makeprg:

:set autowrite
Run Code Online (Sandbox Code Playgroud)

这解决了执行部分.不知道如何将该输出转换为不涉及重定向到文件的拆分窗口.

  • 实际上,您的解决方案确实将输出转换为拆分窗口.使用`:copen`打开在自己的窗口中运行`:make`.生成的"错误列表".不幸的是,为了使输出格式正确,有必要对'errorformat`选项进行一些处理.否则输出将被假定为gcc输出的格式. (5认同)
  • 你真的很关心"finagling"错误格式.它看起来像我见过的一些perl代码...... (2认同)

Bri*_*per 28

要访问当前缓冲区的文件名,请使用%.要将它变为变量,您可以使用该expand()函数.要使用新缓冲区打开新窗口,请使用:new:vnew.要将命令的输出传递到当前缓冲区,请使用:.!.把它们放在一起:

:let f=expand("%")|vnew|execute '.!ruby "' . f . '"'
Run Code Online (Sandbox Code Playgroud)

显然用ruby你想要的任何命令替换.我用过,execute所以我可以用引号括起文件名,所以如果文件名中有空格,它就会起作用.

  • @baboonWorksFine使用`:命令!R let f = expand("%")| vnew | execute'.!ruby"'.f.'''`能够只用`:R`来执行他的命令 (2认同)

小智 22

Vim有!("bang")命令直接从VIM窗口执行shell命令.此外,它允许启动与管道连接并读取标准输出的命令序列.

例如:

! node %
Run Code Online (Sandbox Code Playgroud)

相当于打开命令提示符窗口和启动命令:

cd my_current_directory 
node my_current_file
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅"Vim提示:使用外部命令".


Seb*_*tos 11

我的vimrc中有一个快捷方式:

nmap <F6> :w<CR>:silent !chmod 755 %<CR>:silent !./% > .tmp.xyz<CR>
     \ :tabnew<CR>:r .tmp.xyz<CR>:silent !rm .tmp.xyz<CR>:redraw!<CR>
Run Code Online (Sandbox Code Playgroud)

这将写入当前缓冲区,使当前文件可执行(仅限unix),执行它(仅限unix)并将输出重定向到.tmp.xyz,然后创建新选项卡,读取文件然后将其删除.

打破它:

:w<CR>                             write current buffer
:silent !chmod 755 %<CR>           make file executable
:silent !./% > .tmp.xyz<CR>        execute file, redirect output
:tabnew<CR>                        new tab
:r .tmp.xyz<CR>                    read file in new tab
:silent !rm .tmp.xyz<CR>           remove file
:redraw!<CR>                       in terminal mode, vim get scrambled
                                   this fixes it
Run Code Online (Sandbox Code Playgroud)

  • 我很喜欢你的解决方案,经过一段时间的使用后,我觉得它可以改进.所以这是我的看法:`:w <CR>:沉默!chmod + x%:p <CR>:沉默!%:p 2>&1 | tee~/.vim/output <CR>:split~/.vim/output <CR>:redraw!<CR>` - 这会将stdout和stderr重定向到临时文件,在此之前,它会将所有内容打印到stdout,所以如果脚本需要很长时间才能运行,你实际上看到它正常工作.此外,它与目录无关(我在按绝对路径打开文件时遇到错误).但是,它不会以交互方式运行脚本,而且我还没有找到一种方法来实现它. (2认同)

apo*_*pol 7

Vim 8 内置了一个交互式终端。要在分割窗格中运行当前的 bash 脚本:

:terminal bash %
Run Code Online (Sandbox Code Playgroud)

或者简称

:ter bash %
Run Code Online (Sandbox Code Playgroud)

%扩展为当前文件名。

:help terminal

The terminal feature is optional, use this to check if your Vim has it:
    echo has('terminal')
If the result is "1" you have it.
Run Code Online (Sandbox Code Playgroud)