A couple of months ago I have come across this interisting tmux setup by Massimo Santini that fires up a tmux shell when logging in to a server via ssh.
The setup gives you the freedom to launch long-running commands and before you say «Darn! I should have launched that with screen!» you realize that you are in a tmux session, then you can fire a new window and detach.
As simple as it may seem, this is a nice feature for reckless people as I am, and I adpoted it immediately. I was so happy with it that I decided I wanted it also on my laptop (just in case I need to ssh into it), but the script was not working with my favourite shell zsh.
So, I dug a little into the reference pages for conditional expressions in zsh and bash and I have modified the script ao rhat it runs also with zsh
shells.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#!/bin/bash set -- "$SSH_CONNECTION" if [[ ! -z $PS1 ]]; then if [[ ! -z $SSH_TTY ]]; then if [[ -z $TMUX ]]; then if [[ $1 != $3 ]]; then if [[ ! -r ~/.notmux ]]; then if ! tmux has-session -t remote; then exec tmux new-session -s remote else exec tmux \ new-session -d -s remote_$$ -t remote \;\ new-window \;\ attach \;\ set-option destroy-unattached on fi fi fi fi fi fi alias detach='[[ ! -z $TMUX ]] && tmux has-session -t remote && tmux detach-client' alias exitmux='[[ ! -z $TMUX ]] && tmux has-session -t remote && tmux detach-client \; kill-window' alias usa='[[ ! -z $TMUX ]] && tmux has-session -t remote && eval $(tmux show-environment -t remote | grep ^SSH_AUTH_SOCK)' |
Furthermore, you can put the script in a file, say tmux.sh
and source it from your .bashrc
(for bash shells) or .zshrc
.
1 2 3 |
# Add tmux over ssh source /path/to/tmux.sh |
(I have also found this very nice bash shell script checker)