如何执行我在 Vi(m) 中编辑的文件并在拆分窗口中获取输出(如在 SciTE 中)?
当然我可以这样执行:
:!scriptname
但是是否可以避免编写脚本名称以及如何在拆分窗口而不是屏幕底部获取输出?
有make
命令。它运行makeprg
选项中设置的命令。用作%
当前文件名的占位符。例如,如果您正在编辑 python 脚本:
:set makeprg=python\ %
是的,你需要逃离这个空间。在此之后,您可以简单地运行:
:make
如果您愿意,您可以设置该autowrite
选项,它会在运行之前自动保存makeprg
:
:set autowrite
这解决了执行部分。不知道有什么方法可以将该输出放入不涉及重定向到文件的拆分窗口中。
要访问当前缓冲区的文件名,请使用%
. 要将其放入变量中,您可以使用该expand()
函数。要使用新缓冲区打开新窗口,请使用:new
或:vnew
。要将命令的输出通过管道传输到当前缓冲区,请使用:.!
. 把它们放在一起:
:let f=expand("%")|vnew|execute '.!ruby "' . f . '"'
显然用ruby
你想要的任何命令替换。我使用execute
了所以我可以用引号将文件名括起来,所以如果文件名中有空格,它会起作用。
Vim 有!
("bang") 命令,它直接从 VIM 窗口执行 shell 命令。此外,它允许启动与管道连接并读取标准输出的命令序列。
例如:
! node %
相当于打开命令提示符窗口并启动命令:
cd my_current_directory
node my_current_file
有关详细信息,请参阅“Vim 提示:使用外部命令”。
我的 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
对于我使用过的 Shell 脚本
:set makeprg=%
:make
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.
我通过地图使用了一种稍微侵入性的机制:
map ;e :w<CR>:exe ":!python " . getreg("%") . "" <CR>
就这样我就不用存钱了,然后走。去吧。
您可以使用 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。
基于@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 执行脚本并打开输出文件。
我还在输出文件名中添加了脚本名称,以便您可以同时将其用于多个脚本。
@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>
在你的.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
示例后,您将再次运行相同的命令,此功能不会再次创建新窗口,它会在前一个(相同)拆分窗口中显示结果。
只需使用冒号和感叹号,如下所示
:!<脚本名称>
我推荐插件quickrun。配置快速且简单。这是一个小演示: