How can I set my default shell to start up tmux
@StarNamer's answer is generally accurate, though I typically include the following tests to make sure that (1) tmux exists on the system, (2) we're in an interactive shell, and (3) tmux doesn't try to run within itself:
if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
exec tmux
fi
References
- Using bash's
command
to check for existence of a command - http://man7.org/linux/man-pages/man1/bash.1.html#SHELL_BUILTIN_COMMANDS - Why to use
command
instead ofwhich
to check for the existence of commands - https://unix.stackexchange.com/a/85250 - Using
$PS1
to check for interactive shell - https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html - Expected state of
$TERM
environment variable "for all programs running inside tmux" - http://man7.org/linux/man-pages/man1/tmux.1.html#WINDOWS_AND_PANES
Start tmux on every shell login, from Arch wiki, seems to work. Simply add the following line of bash code to your .bashrc
before your aliases; the code for other shells is very similar:
[[ $TERM != "screen" ]] && exec tmux
Adding a line like
[ -z "$TMUX" ] && { tmux attach || exec tmux new-session && exit;}
in your bashrc file will probably do the job. Note this line will exit ssh and terminate the connection once you detach or exit tmux. I like this configuration as it saves key strokes to terminate the connection. But if you don't love this(which I think is very unlikely) and would rather remain in the login shell after termination, just remove the exit
part:
[ -z "$TMUX" ] && { tmux attach || exec tmux new-session;}
Also note you shouldn't wrap tmux attach
with exec
, as this would cause the connection to be closed when there are no tmux sessions to attach to.