使用带有 --remote-silent 的单个 vim 实例

发布于 2024-10-07 11:17:13 字数 554 浏览 6 评论 0原文

我一直是控制台 vim 用户,但最近我尝试使用 gvim。 我已经进行了一些设置,这样我的控制台习惯就不会受到影响,例如 在当前目录中使用 . 打开 urxvt 终端

然而,用我的上网本打开多个 gvim 和终端是 太可怕了,我的振动空间很快就减少到一两厘米。充其量, 窗口宽度减半,无法垂直分割 屏幕(平铺 wm)。

我通过将 vim 别名为 gvim --remote-silent 来解决这个问题,它 几天工作正常,但今天我需要使用 -c '' 和 意识到在 --remote-silent 后面有任何开关会打开这些 切换为文件。因此,执行 gvim --remote-silent --foo bar 打开两个 文件,--foo 和 bar。

我为 alias vim=gvim 添加了一个新别名,但我担心这会让我成为 再次控制台 vim 用户,比我想象的要早。

所以问题是,继续使用 gvim 的首选方式是什么, 以最小的努力(设置系统可能很费力),这样我就可以 仍然继续使用 vim 的全部功能。该解决方案不需要使用 远程 vim,它应该只是最小化 gvim 窗口。

I've always been a console vim user, but lately I've tried to use gvim.
I've made settings such that my console habits won't suffer, such as
opening an urxvt terminal at the current directory with .

However having multiple gvims and terminals open with my netbook is
horrible, quickly reducing my vim-space to a centimeter or two. At best,
the window width is halved, making it impossible to vertically split
screen (tiling wm).

I went around this problem by aliasing vim to gvim --remote-silent and it
worked fine for a couple of days, but today I needed to use -c '' and
realized that having any switches behind --remote-silent opens those
switches as files. So doing gvim --remote-silent --foo bar opens two
files, --foo and bar.

I put a new alias for alias vim=gvim, but I'm afraid this will make me a
console vim user again, sooner than I thought I would.

So the question is, what would be the preferred way to keep using gvim,
with minimal effort (setting up the system can be laborous) so that I can
still keep using vim with its full power. The solution does not need to use
remote vim, it should just minimize the gvim windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请止步禁区 2024-10-14 11:17:13

我来晚了,但我有一个建议。

首先,我同意@Herbert - 通常没有理由运行多个 Vim 实例(控制台或其他)。将事物保存在一个实例中的好处实在是太大了。但是,我认为需要混合 -c;虽然我看不出有必要自己做:D。

我认为获得此功能的最佳方法是使用 vim 的 --remote-send 功能,而不是 --remote-silent。不过,这意味着需要编写一些 shell 脚本:

#!/bin/bash

function resolveFile
{
    if [ -f "$1" ]; then
        echo $(readlink -f "$1")
    else
        echo "$1"
    fi
}

function gg
{
    local opts=$(getopt -o c: --long command: -n "gg" -- "$@")
    if [ $? != 0 ]; then return 1; fi
    eval set -- "$opts"
    cmd=""
    while :
    do
        case "$1" in
            -c|--command)
                cmd="$2"
                shift 2
                ;;
            --) shift
                break
                ;;
        esac
    done

    if [[ -n "$cmd" ]]; then
        if [[ -n "${1-}" ]]; then
            cmd=":e $(resolveFile $1)<cr>$cmd<cr>"
        else
            cmd="$cmd<cr>"
        fi
        shift 1
    fi
    files=""
    for f in $@
    do
        files="$files $(resolveFile $f)"
    done
    cmd="$cmd:args! $files<cr>"
    gvim --remote-send "$cmd"
}

我只对此进行了少量测试,但您应该能够执行以下操作:

gg -c G file1 file2 file3 file4

应该编辑 file1< /code> 并移至其底部。然后,它还应该将 file2file3file4 添加到 args 列表中,以便您可以执行诸如 :bnext.当您不提供 -c 时,它还应该恢复为与 --remote-silent 相同的功能。

真的认为为了将命令行体验与单个 Vim 实例正确集成,跳过这些障碍是值得的。将一切都集中在一处真是太甜蜜了,让人无法放弃。

I'm late coming to this party, but I have a suggestion.

First, I agree with @Herbert - there's usually very little reason to ever run more than one Vim instance (console or otherwise). The advantages of keeping things in one instance is just too great. But, I see the need to mix in -c; although I can't see the need to do it myself :D.

The best way I can see to get this functionality is to use the --remote-send feature of vim instead of --remote-silent. This means a bit of shell scripting, however:

#!/bin/bash

function resolveFile
{
    if [ -f "$1" ]; then
        echo $(readlink -f "$1")
    else
        echo "$1"
    fi
}

function gg
{
    local opts=$(getopt -o c: --long command: -n "gg" -- "$@")
    if [ $? != 0 ]; then return 1; fi
    eval set -- "$opts"
    cmd=""
    while :
    do
        case "$1" in
            -c|--command)
                cmd="$2"
                shift 2
                ;;
            --) shift
                break
                ;;
        esac
    done

    if [[ -n "$cmd" ]]; then
        if [[ -n "${1-}" ]]; then
            cmd=":e $(resolveFile $1)<cr>$cmd<cr>"
        else
            cmd="$cmd<cr>"
        fi
        shift 1
    fi
    files=""
    for f in $@
    do
        files="$files $(resolveFile $f)"
    done
    cmd="$cmd:args! $files<cr>"
    gvim --remote-send "$cmd"
}

I've only marginally tested that, but you should be able to do things like:

gg -c G file1 file2 file3 file4

That should edit file1 and move to the bottom of it. It should then also add file2, file3 and file4 to the args list so that you can perform commands such as :bnext. It should also revert to the same sort of functionality as --remote-silent when you don't supply a -c.

I really think it's worth jumping through these hoops in order to properly integrate your command-line experience with a single Vim instance. Having everything in one place is just too sweet to give up.

眼睛会笑 2024-10-14 11:17:13

我不认为运行多个 gvim 实例是一个不错的解决方案。为什么不只为 gvim --remote-silent 提供一个别名,当您不需要额外的开关(大概是大多数时间)时使用它,并为 gvim< 提供另一个别名/code> 当你想添加命令行开关时?对我来说似乎很好,没有太多需要记住的。

运行多个 gvim 实例的主要缺点是,除了获得许多难以跟踪的窗口之外,每个 gvim 都有自己的运行时环境,更难在不同 gvim 实例的缓冲区之间共享和复制数据。

Derek Wyatt 在 --remote-silent 上有有趣的视频;我认为它不能解决您的问题,但您可能有兴趣观看:
OneVimToRuleThemAll

另外,如果问题是gvim 中的窗口分割会占用屏幕空间,更好地利用选项卡功能(您甚至可以禁用实际的选项卡标签,但保留打开的选项卡页的隐喻)和/或只是在缓冲区之间导航,同时只有一个缓冲区可见一次。

I don't think having multiple gvim instances running is a decent solution. Why not just have one alias to gvim --remote-silent that you use when you you don't need extra switches (presumably most of the time), and another alias to just gvim for when you want to add command-line switches? Seems fine to me, not much extra to remember.

The main unpleasantness from having multiple gvim instances running, aside from getting many windows that are hard to keep track of, is that each gvim has its own runtime envioronment, harder to share and copy data between buffers in different gvim instances.

Derek Wyatt has interesting video on --remote-silent; I don't think it addresses your issue but you may be interested in watching it:
OneVimToRuleThemAll

Also, if problem is that window splits within gvim are taking screen estate away, make better use of the tab functionality (you can even disable the actual tab labels but keep metaphor of open tab pages) and/or alternatively just navigate between buffers while having only one buffer visible at a time.

请别遗忘我 2024-10-14 11:17:13

Derek Wyatt 的脚本很棒,在 MacOS 上做了一些修改:

  1. brew install gnu-getopt
  2. getopt 替换为 /usr/local/opt/gnu-getopt/ bin/getopt
  3. 删除了函数 gg() 包装器并将其放入主脚本中,删除了
  4. 末尾的“local”关键字 add,将编辑器放在前面:
    gvim --remote-expr 'remote_foreground("VIM")'

现在您可以将其放入 /usr/ local/bin 并在任何地方使用它

Derek Wyatt's script is awesome, made a couple of modifications on MacOS:

  1. brew install gnu-getopt
  2. replace getopt with /usr/local/opt/gnu-getopt/bin/getopt
  3. Removed the function gg() wrapper and just put it in the main script, removed the "local" keyword
  4. add at the end, to bring the editor in front:
    gvim --remote-expr 'remote_foreground("VIM")'

Now you can put this in /usr/local/bin and use it anywhere

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文