4

Suppose I have several gvim windows open, with possible several files opened in each of these.

In the command line I want to have a function or alias, say gvimUniq that if the file is opened in one of the windows will bring that window to the front, otherwise create a new window with that file opened.

skeept
  • 12,077
  • 7
  • 41
  • 52

1 Answers1

0

I've gotten used to running with a single gvim window and using tabs for open documents using this:

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
}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352