Skip to content

Instantly share code, notes, and snippets.

@maljub01
Last active December 14, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maljub01/5130023 to your computer and use it in GitHub Desktop.
Save maljub01/5130023 to your computer and use it in GitHub Desktop.
A shell function that allows reusing the same screen/tmux window for editing/viewing files with vim, inspired by the following question on superuser.com: http://superuser.com/questions/563448/screen-grouping-all-vim-invokation-under-one-window/563784
v () {
if [ $STY ]; then
# screen
local VIM_SERVER_NAME="VIM-SCREEN-$STY"
if [ "`vim --serverlist | grep -i $VIM_SERVER_NAME`" = "" ]; then
# No vim server, become the server
screen -X title vim
vim --servername $VIM_SERVER_NAME "$@"
screen -X title `basename $SHELL`
else
# A vim server exists, use it, then switch to it.
vim --servername $VIM_SERVER_NAME --remote-tab "$@"
screen -X select vim
fi
elif [ $TMUX ]; then
# tmux
local VIM_SERVER_NAME="VIM-TMUX-`tmux list-windows -F '#{session_name}' | head -1`"
if [ "`vim --serverlist | grep -i $VIM_SERVER_NAME`" = "" ]; then
# No vim server, become the server
tmux rename-window vim
printf '\033]2;%s\033\\' vim # rename pane
vim --servername $VIM_SERVER_NAME "$@"
printf '\033]2;%s\033\\' `hostname` # rename pane
tmux rename-window `basename $SHELL`
else
# A vim server exists, use it, then switch to it.
vim --servername $VIM_SERVER_NAME --remote-tab "$@"
tmux select-window -t vim
local VIM_PANE="`tmux list-panes -F '#{pane_title}' | grep -n '^vim$' | cut -f1 -d :`"
tmux select-pane -t $((VIM_PANE-1))
fi
else
vim "$@"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment