I started using tmux the past few days. While I have briefly used tmux in the past, and screen before that while in Uni, I didn’t have any config files from back (would be pointless anyways as things have changed) so I set things up from scratch.
I don’t really need something like tmux now. I am not interested in a terminal multiplexor as all I need is session management, so what I should really be using is abduco… but, I was lazy.
Note: This isn’t a very well thought out post as I don’t have much to say, and there are way better posts on the Internet that explain all this better. I just wanted to make a post nevertheless to make a note of these, but to be honest you are better off checking any other blog for the same. For example, this one or the Arch Linux wiki.
Here are a few important settings for your .tmux.conf
.
1 |
set-option -g default-terminal "screen-256color" |
It’s important that the $TERM
variable in a tmux session is set to “screen-256color” for things like scrolling within apps to work properly. So I set that as the default in .tmux.conf. This is not enough though, if you are overriding the $TERM
in your startup scripts (e.g. .bash_profile) you must make an exception for tmux too. Here’s what I have in my .bash_profile:
1 2 3 4 5 6 7 8 |
if [[ -z "$TMUX" ]]; then if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then export TERM=gnome-256color elif infocmp xterm-256color >/dev/null 2>&1; then export TERM=xterm-256color fi else export TERM=screen-256color |
If the $TMUX
variable is empty it means I am not in a tmux session; so I use that to set my non-tmux terminal preference.
Another important setting is to turn on mouse support. Without this turned on when you scroll up or down your terminal scrolls out of the tmux session. With this turned on scrolling happens within the tmux session.
1 |
set-option -g mouse on |
A lot of people do more fancy stuff here like bind the scroll wheel up and down to other things but I couldn’t get it working. With the basic mouse settings turned on scrolling is limited to the tmux window and that’s all I wanted. At some point I’d also like to get scrolling work with the app I am.
Here’s my status bar settings:
1 2 3 4 |
set-option -g status-bg colour236 set-option -g status-fg colour202 set-option -g status-left "" set-option -g status-right " #[fg=red,bright,bg=colour236]\#{host_short} #[fg=default,bg=colour236]%H:%M" |
This sets the background and foreground colors. I don’t want the left side indicator so I set it to blank; and all I want on the right is the short hostname and the time.
The tmux status bar is made up of three components: the left & right sides (defined above) and a window status bit in the center (which is aligned to the left if the left side is empty). Here are my window status settings:
1 2 3 4 5 6 7 8 9 10 |
set-window-option -g window-status-style "dim" set-window-option -g window-status-current-style "bright" set-option -g window-status-format "#[fg=colour172,bg=colour236]#I:#W#F" set-option -g window-status-current-format "#[fg=colour196,bg=colour236]#I:#W#F" set-option -g window-status-activity-style fg=colour196 set-option -g window-status-bell-style fg=colour172 set-option -g window-status-separator " " set-option -g window-style default set-option -g window-status-style bg=colour236,fg=colour4 |
I want the status to be the “<window index>:<window name><flags>
” where <window name>
defaults to the currently running process.
Lastly, since I mostly connect using some terminal window application I also wanted the terminal title to be set correctly. I already set a title via my login profile so I want tmux to simply take its title from there. Hence I have the following:
1 2 3 |
set-option -g set-titles on set-option -g terminal-overrides "xterm*:smcup@:rmcup@" set-option -g allow-rename on |
For this to work the $PROMPT_COMMAND
variable must be set so the window is dynamically updated. I have something like this in my login scripts (the prompt is made up for various functions, that’s why it looks odd).
1 |
export PROMPT_COMMAND='echo -e "\[\e]0;\$(windowprompt_git_component)\w | $(prompt_user_component)@$(windowprompt_device_component)\a\]"' |
Here’s how my tmux session looks at the end of all this. The window title still has the tmux window & process info, in addition to my shell generated title. I couldn’t figure out a way to get rid of the tmux window & process info and I left it after a while.
I also have the following in my .bash_profile to ensure I always start a tmux session (or connect to a detached one) when I login to something. If I am not SSHing into something then it doesn’t launch tmux as I don’t want to be in a tmux session if I am opening the terminal in my Mac or laptop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Start tmux only if I am SSHing into something # -z checks if the variable is empty. If $SSH_TTY is not empty it means we are in an SSH session. if [[ ! -z "$SSH_TTY" ]]; then if command -v tmux &> /dev/null; then # setting an alias so I can detach with an exit code 1 if [[ -z "$TMUX" ]]; then (tmux ls | grep -vq attached && tmux at) || tmux fi else echo -e "${MAGENTA}tmux missing!${RESET}" echo "" fi fi |
On the tmux side I made two binding key changes:
1 2 3 4 5 |
# Remap d to detach-client and send a SIGHUP to the parent process. This way when I detach tmux the parent shell quits. bind-key d detach-client -P # Add C-b as a binding to detach-client but not send a SIGHUP (this is what d used to do) bind-key C-d detach-client |
That’s all for now!