Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Last active August 29, 2015 14:15
Show Gist options
  • Save DanielFGray/221d5cb002663e349345 to your computer and use it in GitHub Desktop.
Save DanielFGray/221d5cb002663e349345 to your computer and use it in GitHub Desktop.
single vim session
#!/usr/bin/env bash
select_cmd_in_tmux() {
list_cmds_in_panes=$(IFS=$'\n'; set -f; for p in $(tmux list-windows); do
tmux list-panes -t "${p%%:*}" -F "#{window_index}.#{pane_index} #{pane_current_command}"
done)
pane_with_cmd=$(awk "/$1/{print \$1; exit}" <<< "$list_cmds_in_panes")
if [[ -n "$pane_with_cmd" ]]; then
pane=${pane_with_cmd#.*}
window=${pane_with_cmd%%.*}
tmux select-window -t "$window"
tmux select-pane -t "$pane"
fi
}
while [[ $1 = -* ]]; do
flags+=("$1")
shift
done
serv=$(command vim --serverlist)
if [[ "$serv" != '' ]]; then
command vim "${flags[@]}" --servername "$serv" --remote-silent "$@"
select_cmd_in_tmux vim
else
if [[ -t 0 || -p /dev/stdin ]]; then
command "${flags[@]}" vim --servername VIM "$@"
else
if ! pgrep tmux &>/dev/null; then
x-terminal-emulator -e 'vim' "${flags[@]}" '--servername' 'VIM' "$@"
else
tmux new-window "vim ${flags[@]} --servername VIM $@"
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment