Let gVim always run a single instance

后端 未结 12 691
时光说笑
时光说笑 2020-12-12 19:14

Is there a way to let gVim only run a single instance, so that when a new file is opened with it it\'s automatically opened in a new tab in the currently running instance?

12条回答
  •  情话喂你
    2020-12-12 19:39

    Portable Wrapper Script:

    I got a little tired of working around this in cygwin + windows so I finally did something about it. I started with the wrapper script defined above but I wound up making it a lot more robust and multi-env capable for *nix and Win.


    #!/bin/bash
    #bash wrapper for windows/cygwin gvim
    
    #####################################################################
    ## Cygwin/*nix and Windows gvim wrapper script, alias friendly, path friendly
    ## Author: Matt Gregory (skyleach (AT) geemale (dot) com)
    ## Version: 1.5
    ## Date: Thu Jun 12 10:02:54 2014
    ## Known Bugs:
    ## Changes:
    ##      Thu Jun 12 10:04:08 2014 : Initital posting to StackOverflow
    #####################################################################
    
    [[ -z ${WINVIM} ]] && WINVIM=true
    [[ -z ${VIMRUN} ]] && export VIMRUN='' #scoping
    if [[ ${WINVIM} = false ]]; then
        [[ ! ${VIMRUN} ]] && VIMRUN='/bin/gvim'
        ANS=$("${VIMRUN}" --serverlist | grep GVIM)
    else
        [[ ! "${VIMRUN}" ]] && VIMRUN='/cygdrive/c/Program Files/vim/vim74/gvim'
        ANS=$(ps -Wsl | grep "${VIMRUN}" | sed -e "s/\s\+\([0-9]\+\).*/\1/g")
    fi
    [[ ! -z ${VIM} && ${WINVIM} = true ]] && export VIM=$(cygpath -wal "${VIM}")
    RT="--remote-tab"
    [[ $ANS ]] || unset RT
    if [ ! -z ${DEBUG} ]; then
        echo "WINVIM: ${WINVIM}"
        echo "VIMRUN: ${VIMRUN}"
        echo "ANS: ${ANS}"
        echo "VIM: ${VIM}"
    fi
    #process arguments or stdin
    if [ ${#} -ne 0 ]; then
        [[ ! -z ${DEBUG} ]] && echo "Got arguments [${#}]:'${@}'"
        for OFILE in "${@}"; do # if [ -f "${OFILE}" ] || [ -d "${OFILE}" ]; then
            [[ -h ${OFILE} ]] && OFILE="$(readlink -f "${OFILE}")"
            [[ ${WINVIM} == true ]] && OFILE=$(cygpath -wal "${OFILE}")
            echo "\"${VIMRUN}\" --servername GVIM $RT \"${OFILE}\""
            "${VIMRUN}" --servername GVIM $RT "${OFILE}" &
            if [[ -z ${RT} ]] || [[ ! "${RT}" ]]; then
                RT="--remote-tab"
                sleep 5 #give gvim time to start up, running too fast messes things up
            else
                sleep .3 #shorter sleep for tabs
            fi
            #fi
        done
    else
        while read OFILE; do
            [[ -h ${OFILE} ]] && OFILE="$(readlink -f "${OFILE}")"
            [[ ${WINVIM} == true ]] && OFILE=$(cygpath -wal "${OFILE}")
            echo "\"${VIMRUN}\" --servername GVIM $RT \"${OFILE}\""
            "${VIMRUN}" --servername GVIM $RT "${OFILE}" &
            if [[ -z ${RT} ]] || [[ ! "${RT}" ]]; then
                RT="--remote-tab"
                sleep 3 #give gvim time to start up, running too fast messes things up
            else
                sleep .3 #shorter sleep for tabs
            fi
        done
    fi
    # vim: set filetype=sh:
    

    How to use it effectively:

    • Install the above code into a script file on yor path somewhere
    • Add a WINVIM environment variable to windows or your ~/.bashrc file in order to set the default script behavior. true/use windows. false/use x11
    • alias some command to cygwin and/or windows gvim like so:

      echo "alias gwrap='WINVIM=false ~/src/localscripts/wgwrap'" >> ~/.bashrc echo "alias wgvim='wgwrap'" >> ~/.bashrc

    • NOTE: If the hard-coded paths to gvim are incorrect for your system you can edit the script directly, the alias(s) and/or add the environment variables WINVIM and/or VIMRUN. You can set them in the alias as I do above for gwrap or you can add them to your .bashrc or Windows system environment.

提交回复
热议问题