110

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

当然我可以这样执行:

:!scriptname

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

4

13 回答 13

112

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

:set makeprg=python\ %

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

:make

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

:set autowrite

这解决了执行部分。不知道有什么方法可以将该输出放入不涉及重定向到文件的拆分窗口中。

于 2009-06-04T22:20:12.480 回答
29

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

:let f=expand("%")|vnew|execute '.!ruby "' . f . '"'

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

于 2009-06-04T22:48:57.413 回答
23

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

例如:

! node %

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

cd my_current_directory 
node my_current_file

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

于 2012-07-15T19:57:14.697 回答
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>

这将写入当前缓冲区,使当前文件可执行(仅限 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
于 2012-11-30T15:10:45.830 回答
4

对于我使用过的 Shell 脚本

:set makeprg=%

:make
于 2010-10-19T18:29:42.930 回答
4

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

:terminal bash %

或简称

:ter bash %

%展开为当前文件名。

来自: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.
于 2018-06-25T19:05:35.353 回答
1

我通过地图使用了一种稍微侵入性的机制:

map ;e :w<CR>:exe ":!python " . getreg("%") . "" <CR>

就这样我就不用存钱了,然后走。去吧。

于 2009-06-16T21:23:33.627 回答
1

您可以使用 vim 的插件bexec。据我所知,最新版本是 0.5。

然后:

$ mkdir -p ~/.vim/plugin
$ mv bexec-0.5.vba ~/.vim/plugin
$ vim ~/.vim/plugin/bexec-0.5.vba

在编辑 .vba 文件时在 vim 内部执行以下操作:

:so %

将显示一些输出,让您知道bexec.vim以及文档等已被编写。

现在,您可以通过在 vim 中打开您的(具有 #! 解释器正常工作的任何语言脚本)并运行来测试它

:Bexec 

注意:我希望拆分是垂直的而不是水平的,所以我做了:

$ grep -i -n split ~/.vim/plugin/bexec.vim | grep -i hor
102:    let bexec_splitdir = "hor" " hor|ver
261:        exec {"ver":"vsp", "hor":"sp"}[g:bexec_splitdir]

并将值从“hor”更改为“ver”..

我知道这是一个老问题,但我希望这可以帮助那里的人。我在参加 Coursera 的 Startup Engineering 课程时遇到了同样的问题,Palaji 教授使用 Emacs 而我不喜欢 Emacs。

于 2013-07-06T23:09:39.543 回答
0

基于@SethKriticos 和@Cyril 的回答,我现在使用以下内容:

function! Setup_ExecNDisplay()
  execute "w"
  execute "silent !chmod +x %:p"
  let n=expand('%:t')
  execute "silent !%:p 2>&1 | tee ~/.vim/output_".n
  " I prefer vsplit
  "execute "split ~/.vim/output_".n
  execute "vsplit ~/.vim/output_".n
  execute "redraw!"
  set autoread
endfunction

function! ExecNDisplay()
  execute "w"
  let n=expand('%:t')
  execute "silent !%:p 2>&1 | tee ~/.vim/output_".n
  " I use set autoread
  "execute "1 . 'wincmd e'"
endfunction

:nmap <F9> :call Setup_ExecNDisplay()<CR>
:nmap <F2> :call ExecNDisplay()<CR>

使用 F9 设置新窗口,使用 F2 执行脚本并打开输出文件。

我还在输出文件名中添加了脚本名称,以便您可以同时将其用于多个脚本。

于 2014-03-05T16:01:33.047 回答
0

@xorpaul

我一直在寻找这个脚本(python/Windows)。由于 Windows 中没有“三通”,我将其更改为:

function! Setup_ExecNDisplay()
  execute "w"
  let n=expand('%:t')
  execute "silent ! python % > d:\\temp\\output_".n ." 2>&1"
  execute "vsplit d:\\temp\\output_".n
  execute "redraw!"
  set autoread
endfunction

function! ExecNDisplay()
  execute "w"
  let n=expand('%:t')
  execute "silent ! python % > d:\\temp\\output_".n . " 2>&1"
endfunction

:nmap <F9> :call Setup_ExecNDisplay()<CR>
:nmap <F2> :call ExecNDisplay()<CR>
于 2014-10-10T10:51:09.920 回答
0

在你的.vimrc你可以粘贴这个功能

function! s:ExecuteInShell(command)
  let command = join(map(split(a:command), 'expand(v:val)'))
  let winnr = bufwinnr('^' . command . '$')
  silent! execute ':w'
  silent! execute  winnr < 0 ? 'vnew ' . fnameescape(command) : winnr . 'wincmd w'
  setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
  silent! execute 'silent %!'. command
  silent! redraw
  silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
  silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
  silent! execute 'wincmd w'
  " echo 'Shell command ' . command . ' executed.'
endfunction
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)
cabbrev shell Shell

之后,以vim运行命令:shell python ~/p.py为例。您将在拆分窗口中获得输出。+ 更改p.py示例后,您将再次运行相同的命令,此功能不会再次创建新窗口,它会在前一个(相同)拆分窗口中显示结果。

于 2016-06-21T16:33:09.740 回答
0

只需使用冒号和感叹号,如下所示

:!<脚本名称>

于 2020-04-16T04:41:03.000 回答
0

我推荐插件quickrun。配置快速且简单。这是一个小演示: 在此处输入图像描述

于 2020-08-15T11:27:53.663 回答