bash - 如果文件已经打开,gvim 打开实例,否则创建新窗口

标签 bash vim

假设我打开了几个 gvim 窗口,每个窗口中可能打开了几个文件。

在命令行中我想要一个函数或别名,比如 gvimUniq 如果文件在其中一个窗口中打开,将把那个窗口带到前面,否则创建一个新窗口该文件已打开。

最佳答案

我已经习惯了使用单个 gvim 窗口运行并使用选项卡打开文档:

gvim() { 
    local -u servername="${HOSTNAME}_${LOGNAME}"
    local -u currentservers=$(command gvim --serverlist)
    if (( $# == 0 )); then
        if [[ $currentservers == *${servername}* ]]; then
            command gvim --servername "$servername" --remote-send '<Esc>:tabe<CR>' || command gvim
        else
            command gvim --servername "$servername" || command gvim
        fi
    else
        # http://stackoverflow.com/questions/936501/let-gvim-always-run-a-single-instance 
        local vi_options=()
        while [[ $1 == -* ]]; do
            [[ $1 != -- ]] && vi_options+=("$1")
            shift
        done
        if [[ $currentservers == *${servername}* ]]; then
            command gvim "${vi_options[@]}" --servername "$servername" --remote-tab-silent "$@" || command gvim "$@"
        else
            command gvim "${vi_options[@]}" --servername "$servername" "$@" || command gvim "$@"
        fi
    fi
}

关于bash - 如果文件已经打开,gvim 打开实例,否则创建新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23614480/

相关文章:

Bash 和变量

linux - VIM ( gvim ) 在 Linux 桌面上为 gVIm 存储的颜色设置在哪里?

python - 在 Python 中将 Linux 命令作为原始字符串

vim - 如何在neovim中安装vim-translate插件

vim - 带有shift-tab的vim中的omnicomplete不起作用?

macos - Vim 使用 +clipboard 编译,但 + 和 * 寄存器都无法存储 yanks

bash - 使用该行的一部分索引到 file2 并查找值后,如何将值附加到 file1 中的每一行?

bash - 如何使用 bash 找到给定进程的顶级父 PID?

python - 如何停止在 vim 中调试 python

vim - vim 文件管理器