如何在vim中的vim缓冲区和命令shell屏幕之间切换?

Adr*_*ian 7 vim bashrc

我发现不断按下Ctrl-Z然后fg在这些屏幕之间切换很烦人(其中命令终端是您用来调用的命令行vim)。它还会fg在我的控制台上生成不需要的行。我很确定这可以做到,因为我记得我在大学时做过,但我不记得是怎么做的。

终端信息及其与 vim 的关系

vt100 终端和其他终端有一种方法可以在更改屏幕之前保存屏幕,然后可以在需要时恢复它。 vim这样做并且可以看到如果你进入vim然后shell或Ctrl-Zout。大多数情况下,它会vim通过在绘制屏幕之前保存屏幕来向您显示输入之前vim屏幕上的内容。如果您的终端不支持此功能,它只会将命令行直接放在vim屏幕下方,向上滚动。这可以通过将功能较少的终端仿真导出到 TERM 变量或将其清除(尽管通过清除它,它可能会vim进入线路模式或可能使用最基本的终端代码,具体取决于其实现)。要查看有关保存/恢复屏幕的更多信息,请参阅终端代码 (ANSI/VT100) 介绍,在标题Save/restore screen 下

Adr*_*ian 2

好的,在 Gary Johnson 的Google 网上论坛 vim_use群组的帮助下,我已经弄清楚了如何做到这一点。他通过说明如何tput从 vim 运行命令来提供帮助。这是我的最终解决方案:

  1. vim内存中有如下函数脚本:

    function! ShowTerm()
        call system(">/dev/tty tput rmcup")
        call input("")
        call system(">/dev/tty tput smcup")
        redraw!
    endfunction
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将脚本映射到某个命令键序列,如下所示:

    map [= :call ShowTerm()<CR>
    
    Run Code Online (Sandbox Code Playgroud)

我使用了该序列[=,但你可以使用任何你想要的。

现在输入按键序列,您将看到另一个屏幕。按Enter键,您将返回到 vim 屏幕。Enter 也不向终端屏幕添加换行符,因此没有行进线。

好的!:) 请注意,这可能不是 15-20 年前的做法,但它确实有效。

编辑

为了不依赖于tput可用,我发现@MarkPlotnick 推断的以下内容也同样有效。只需将上面的函数替换ShowTerm()为:

function! ShowTerm()
    silent !read -sN 1
    redraw!
endfunction
Run Code Online (Sandbox Code Playgroud)

这取决于bash用作外壳程序(或具有类似读取调用的其他外壳程序)。

第二次编辑

此外,您可以通过将.vim文件放入 ~/.vim/plugins 文件夹来自动执行此操作,其中包含以下内容:

" Function that allows viewing command line display last time it was seen.
" Note: Will only work on terminals that allow saving/restoring the display.
function! ShowTerm()
    " This will invoke the command shell and call the read function.
    " Will exit when a key is pressed.  This is specific to bash and
    " and may have to be changed if using a different shell.
    silent !read -sN 1
    redraw!
endfunction

" Maps [= sequence to view command line display last time it was seen.
map [= :call ShowTerm()<CR>
Run Code Online (Sandbox Code Playgroud)