Vim 如何执行文件I';m在Vi中编辑(m)

Vim 如何执行文件I';m在Vi中编辑(m),vim,exec,Vim,Exec,如何在Vi(m)中执行我正在编辑的文件,并在分割窗口中获得输出(如在SciTE中) 我当然可以这样执行: :!scriptname 但是,是否可以避免写入脚本名称以及如何在拆分窗口中获得输出,而不是仅在屏幕底部?有make命令。它运行makeprg选项中的命令集。使用%作为当前文件名的占位符。例如,如果您正在编辑python脚本: :set makeprg=python\ % 是的,你需要逃离这个空间。之后,您只需运行: :make 如果愿意,您可以设置autowrite选项,它将在运行m

如何在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
,因此我可以用引号将文件名括起来,这样如果文件名中有空格,它就可以工作。

我通过映射使用了一种更具侵入性的机制:

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

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

对于我使用的Shell脚本

:set makeprg=%

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

例如:

! node %
相当于打开命令提示窗口并启动命令:

cd my_current_directory 
node my_current_file

有关详细信息,请参阅。

我在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>
nmap:w:silent!chmod 755%:无提示!/%>。tmp.xyz
\:tabnew:r.tmp.xyz:安静!rm.tmp.xyz:重画!
这将写入当前缓冲区,使当前文件可执行(仅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
:w写入当前缓冲区
:安静!chmod 755%使文件可执行
:静默!/%>。tmp.xyz执行文件,重定向输出
:tab新建选项卡
:r.tmp.xyz在新选项卡中读取文件
:安静!rm.tmp.xyz删除文件
:重画!在终端模式下,vim被加扰
这就解决了问题
您可以使用vim的插件。据我所知,最新版本是0.5

然后:

编辑.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的创业工程课程时也遇到过同样的问题,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>
函数!安装程序\u ExecNDisplay()
执行“w”
执行“静默!chmod+x%:p”
设n=展开('%:t')
执行“静默!%:p2>&1 | tee~/.vim/output|n”
“我更喜欢vsplit
“执行”拆分~/.vim/output_uz“.n
执行“vsplit~/.vim/output”
执行“重画!”
设置自动读取
端功能
功能!ExecNDisplay()
执行“w”
设n=展开('%:t')
执行“静默!%:p2>&1 | tee~/.vim/output|n”
“我使用set autoread
“执行“1.‘wincmd e’”
端功能
:nmap:callsetup\u ExecNDisplay()
:nmap:callexecndisplay()
使用F9设置新窗口,使用F2执行脚本并连接到输出文件

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

@xorpaul

我寻找这个脚本(python/Windows)已经有一段时间了。由于Windows中没有“T”字,我将其更改为:

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>
函数!安装程序\u ExecNDisplay()
执行“w”
设n=展开('%:t')
执行“silent!python%>d:\\temp\\output\.n.”2>&1
执行“vsplit d:\\temp\\output\”。n
执行“重画!”
设置自动读取
端功能
功能!ExecNDisplay()
执行“w”
设n=展开('%:t')
执行“silent!python%>d:\\temp\\output\”.n。" 2>&1"
端功能
:nmap:callsetup\u ExecNDisplay()
:nmap:callexecndisplay()

.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
函数!s:ExecuteInShell(命令)
let command=join(映射(拆分(a:command),'expand(v:val)'))
让winnr=bufwinnr(“^.”命令“$”)
安静!执行“:w”
安静!执行winnr<0?““新”。fnamescape(命令):winnr'wincmd w'
setlocal buftype=nowrite bufhidden=wipe noswapfile nowrap编号
安静!执行“静默%!”。命令
安静!重画
安静!执行'au BufUnload execute bufwinnr('.bufnr('#')。'wincmd w''
安静!执行'nnoremap r:callexecuteinshell(''.command'
安静!执行“wincmd w”
回显“Shell命令”。命令。“已执行”
端功能
command!-complete=shellcmd-nargs=+Shell调用s:ExecuteInShell()
cabbrev壳体
然后,以
vim
run命令
:shell python~/p.py
为例,您将在拆分窗口中获得输出。 +例如,在
p.py
中进行更改后,您将运行相同的命令
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
:terminal bash %
:ter bash %
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.