Tmux is a program that lets me use a terminal more efficiently, with it, I am able to quickly switch between and organize multiple terminal windows and even sessions with ease. But just what is tmux?
Here is an excerpt from its man page:
tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.
Every command in tmux that you will use is specified by a modifier key, this is customize-able, but by default it is Ctrl-B
. From here on out, this will be referred to as Mod
.
# All about panes
Once you’ve installed tmux with your preferred package manager, you can create a new session with the command tmux
. From here, the most common thing to do is splitting a window. You can perform a vertical split with Mod+%
, whereas to perform a horizontal split you use Mod+"
. To switch between panes, use Mod+ARROW_KEY
in the direction you want to move.
These newly created panes can be resized, and even zoomed. To resize a pane, press your Mod
key and hold Ctrl
while tapping the arrow key of the direction you’d like to move the pane in. Additionally, new tabs can be created with Mod+C
, these new tabs can be switched to with Mod+[0-9]
where the number is the respective tab. And finally, you can zoom into the current pane with Mod+Z
.
To adjust panes, such as rotating where the currently selected pane is located, use Mod+{
or Mod+}
depending on the direction you want to move your pane in the current tab. You can also switch up the layout of the pane with Mod+SPACE
, but I haven’t found this to be all that useful in practice.
One way of quickly switching back to where I was is Mod+l
which will take you to the last used tab. To gain a quick overview of all your current windows, Mod+w
will take you to an overview of all current sessions/tabs.
# Organization
An easy way to keep organized is by use of tmux named sessions and named tabs. In order to create a named session run tmux with the the following line tmux new-session -s ${session_name}
from this new session, if you want to leave and create a new session press Mod+:
which will open the tmux command line for you to interact with, and you can enter the command detach
to detach from your current session. Now, you can run tmux list-sessions
to view all the sessions which you’ve created. The sessions will persist in the background until reboot. Alternatively, you can rename the current session with Mod+$
.
To rename a tab, press Mod+,
and then enter the name you wish. I like to keep a few tabs for notes, coding, and general purpose working environments. If you feel like the current pane has enough going on that it should get its own tab, Mod+!
will send the pane to a new tab.
# History searching
In order to quickly search through the current tabs scroll-back buffer, enter command mode with Mod+:
and type set mode-keys vi
, this is my personal preference, but gives you vi style keybindings in order to navigate now. If you enter copy mode with Mod+[
, you will now be able to navigate the current history. You can use page-up/page-down to paginate, while if you type /
you will be given the opportunity to search for text. If you wish to copy this text while in copy mode, you can use the vi keybinding to highlight a line V
, and selecting all lines you like. Once the selection is made, press Enter
to put all currently selected text into your paste buffer. Now, when you would like to paste this data, use Mod+]
and it will be pasted from your current location.
To search for text across all current tabs (not including text in scroll-back buffers), you can use Mod+f
to quickly find where some string is located across your tabs. While this is a useful feature, I find my self almost never using it for one reason or another.
#
My .tmux-config
As is tradition with many things, tmux has its own dot-file which allows you to specify particular configurations you would like to persist across restarts. Below is mine, followed by an explanation of each setting.
# Set base tab index to 1
set -g base-index 1
set -g default-terminal screen-256color
set -g status-keys vi
set -g history-limit 10000
setw -g mode-keys vi
setw -g monitor-activity on
# Use Alt-arrow keys without prefix key to switch panes
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
# Shift arrow to switch windows
bind -n S-Left previous-window
bind -n S-Right next-window
# No delay for escape key press
set -sg escape-time 0
# Reload tmux config
bind r source-file ~/.tmux.conf
# My bindings
bind p attach -c "#{pane_current_path}"
# THEME
set -g status-bg black
set -g status-fg white
set -g status-interval 60
set -g status-left-length 30
set -g status-left ' '
set -g status-right ''
set -g pane-border-style 'fg=white'
set -g pane-active-border-style 'fg=red'
set -g default-terminal screen-256color
lets tmux use more colors for its display. set -g status-keys vi
/set -g mode-keys
sets the default modes to vi, which is what I am more familiar with. set -g history-limit 10000
sets the scroll-back history to 10000. bind p attach -c "#{pane_current_path}"
will set newly created tabs/panes to start in the current working directory. All other settings should be pretty self explanatory.