Vim:打开一个显示可执行文件输出的临时缓冲区



我发现:cwindow命令非常有用,我想知道我是否可以使用编译代码的输出获得类似的功能。我希望:!./a.out的输出出现在"快速修复"样式缓冲区中。

我还注意到,即使在采取了标准步骤来防止"Press Enter to continue"消息之后,它仍然至少在:make:!./a.out上发生一次 - 使用 :silent 来抑制这会导致我的 tmux 完全空白。我目前的解决方法涉及具有大量回车符的映射,还有其他方法吗?

当然,您可以使用 vim 的预览窗口和一个简短的函数来执行命令,请在您的 .vimrc 中尝试此操作:

fun! Runcmd(cmd)
    silent! exe "noautocmd botright pedit ".a:cmd
    noautocmd wincmd P
    set buftype=nofile
    exe "noautocmd r! ".a:cmd
    noautocmd wincmd p
endfun
com! -nargs=1 Runcmd :call Runcmd("<args>")

然后,您可以:

:Runcmd ls

并在预览窗口中查看ls的结果

我找到了这个:

" Shell ------------------------------------------------------------------- {{{
function! s:ExecuteInShell(command) " {{{
    let command = join(map(split(a:command), 'expand(v:val)'))
    let winnr = bufwinnr('^' . command . '$')
    silent! execute  winnr < 0 ? 'botright vnew ' . fnameescape(command) : winnr . 'wincmd w'
    setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap nonumber
    echo 'Execute ' . command . '...'
    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>:AnsiEsc<CR>'
    silent! execute 'nnoremap <silent> <buffer> q :q<CR>'
    silent! execute 'AnsiEsc'
    echo 'Shell command ' . command . ' executed.'
endfunction " }}}
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)
nnoremap <leader>! :Shell
" }}}

在史蒂夫·洛什的 .vimrc 中 - 看看我无耻地复制了它。

我刚刚发现了:read命令,它将 shell 命令的输出放入窗口中。

我去寻找这个是因为我经常想在当前目录中grep 中找到包含某个字符串的文件(然后将其打开到我当前的 VIM 中)。

这是我$MYVIMRC中的快捷方式:

noremap <leader>g :new<CR>:read ! grep -rn "

有了这个,当我按 g 时,我会在拆分窗口中创建一个新缓冲区并找到

:read ! grep -rn "

坐在指挥区等我。现在我只需键入我的搜索字符串,关闭双引号并按<Enter>,缓冲区就会填充命令输出。

一旦完成,一个简单的

:bw!

在那个新的缓冲区中会杀死它。

首先打开一个预览窗口,并将其设置为自动读取文件:

:botr pedit +:setl autoread /tmp/out.log

现在只需运行您的命令,并将输出发送到文件。

:!date > /tmp/out.log 2>&1

命令的结果应显示在预览窗口中。

但是,我们仍然收到"按 ENTER"提示。 避免这种情况的一种简单方法是制作一个映射,为我们按 Enter 键:

:nmap <Leader>r :exec '!date > /tmp/out.log 2>&1'<CR><CR><CR>

我以为只需要两个<CR>,但后来发现自己需要三个。

最新更新