Created
March 22, 2025 11:46
-
-
Save sagax/c518d9febf475f3725dc7351987f8507 to your computer and use it in GitHub Desktop.
vim with --servername by default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SERVERNAME="DEVELOPMENT" | |
function has_server() { | |
local name="$1" | |
shopt -s nocasematch | |
for _name in $(vim --serverlist | xargs) | |
do | |
if [[ $name == "$_name" ]] | |
then | |
return 0 # true | |
fi | |
done | |
return 1 # false | |
} | |
function its_file() { | |
local arg="$1" | |
if [[ -r "$arg" ]] | |
then | |
return 0 # true | |
else | |
return 1 # false | |
fi | |
} | |
function run_gvim() { | |
local name="$1" | |
local filepaths=("${@:2}") | |
if [[ $# -eq 1 ]] | |
then | |
if has_server "$name" | |
then | |
true | |
else | |
gvim --servername "$name" | |
fi | |
else | |
for filepath in "${filepaths[@]}" | |
do | |
gvim --servername "$name" --remote-silent "$filepath" | |
done | |
fi | |
} | |
if [[ $# -eq 0 ]] | |
then | |
run_gvim $SERVERNAME | |
fi | |
if [[ $# -eq 1 ]] | |
then | |
if its_file "$1" | |
then | |
run_gvim $SERVERNAME "$1" | |
else | |
SERVERNAME="$1" | |
run_gvim "$SERVERNAME" | |
fi | |
fi | |
if [[ $# -gt 1 ]] | |
then | |
if its_file "$1" | |
then | |
run_gvim "$SERVERNAME" "$@" | |
else | |
SERVERNAME="$1" | |
run_gvim "$SERVERNAME" "${@:2}" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment