Tmux is a terminal multiplexer. A terminal multiplexer enables to run multiple clients inside a single terminal in a tiling manner. Another famous terminal multiplexer is GNU ‘screen’, but Tmux is prettier and easier to configure in my opinion. Here’s a comparison between them http://dominik.honnef.co/posts/2010/10/why_you_should_try_tmux_instead_of_screen/
Introductory Keybinds in Tmux
So, what does Tmux do? After installation, open up a terminal emulator and type tmux. You will have a new emulator screen with a cute status bar. Open an app there or leave a process running.
Next, type Ctrl+b. This is the prefix for all the commands given to Tmux rather than to the app or process (called “client”) you are running inside of it. The prefix done, press c (i.e. prefix+c). A next screen opens up. The status bar will show you how many screens (called “windows”) are opened and you can determine in which one your current focus is.
To switch between windows, press prefix+n (for next window) or prefix+p (for previous window). By default, these keys should cycle through all, i.e. prefix+n should take you to the first window, when you are currently in the last window.
Prefix+” will split the current window into two tiles (called “panes”) horizontally. Prefix+% will split it further vertically.
Prefix+o will move focus to another pane in the current window. Prefix+space will switch to a different layout. There are several layouts, so try prefix+space multiple times to see them all.
Prefix+z will zoom the focused pane over the entire window. Prefix+o (i.e. move focus on another pane in the same window) shrinks the zoomed pane.
Resizing the panes should be possible by dragging the border of the pane with mouse. The rest of the keyboard shortcuts are under prefix+?.
Copying in Tmux
There are enough basic intros to Tmux on the internet already, so I’ll better focus on the awesome copy-paste function, which is otherwise rather difficult to achieve in terminals.
There are two modes for copy-paste, Vim and Emacs. The more likely default is Emacs and I like it better, so here’s a list of its basic routines.
Prefix+[ or Prefix+PageUp enters the copy mode. An indicator lights up in the upper right corner.
Use the arrow keys (without prefix) to get to the point you want and Ctrl+space (without prefix) to begin selecting. Use arrow keys again to draw out the selection and Alt+w (without prefix) to copy the selection.
Upon copying, the copy mode indicator vanishes from the upper right corner, i.e. the copy mode terminates at Alt+w. If you want to exit the copy mode any time when it’s active, press Escape or Ctrl+c.
To paste, move with arrow keys (and switch the panes and windows if necessary) to the spot where you want to paste the copied content (called “buffer”). On the spot, press prefix+] to paste or prefix+= to see the buffer and then Enter to paste.
Copying and pasting in Tmux is rather important, so let’s try again. There are some nice pages in Tmux, such as the internal help page, which are in copy mode even without entering the copy mode, and this is good for exercise.
A note: Tmux copy-paste buffer is available only inside Tmux, not in other apps and not on the clipboard.
A Small Exercise of Copying and Pasting in Tmux
- In Tmux, open up a new clean window; prefix+c
- Open up the list of keybinds; prefix+?
- Move to the line you want with the arrow keys
- Ctrl+a goes to the beginning of the line
- Ctrl+space begins selection
- Ctrl+e goes to the end of the line; the line becomes selected
- Alt+w copies the line and closes the list of keybinds
- Prefix+? to open the list of keybinds again
- Now move to a different line to select it
- Select and copy the line the way you just did a moment earlier; the list should close by the end of this operation
- Now, facing the console, press prefix+= which should bring up at least two buffers to select from, if you did everything right
- Press Enter to paste the selected buffer
- Repeat until you have mastered the procedure
Attach and Detach Tmux Sessions
Attaching and detaching sessions is an advanced feature, but worth some practising too. First let’s go over some terminology again.
Clients (apps or processes) run inside panes and there can be multiple panes in a window (a single screen view of the terminal emulator or console). All these windows make up a session.
The new session starts as soon as Tmux is started. Prefix+s provides a list of running sessions and an opportunity to switch between them. Selecting a session this way is called attach.
Prefix+d detaches the session. This means that even when you close the terminal emulator where you had Tmux running, all the processes in the session remain active as long as the user session in the computer remains active (i.e. don’t log out, don’t shut down the computer).
To test the attach/detach feature, press prefix+d to detach from the running session in the terminal emulator. Close the terminal emulator and open up the console (tty, normally Ctrl+Alt+F2 through F6 in Linux).
In console, log in as the same user and type tmux. In Tmux, press prefix+s. You should see at least two sessions running. That’s the idea of attaching and detaching.
There should be a configuration file located at /etc/tmux.conf, but if not, create an empty file at ~/.tmux.conf. As long as it’s empty, Tmux uses its defaults. If there’s something in ~/.tmux.conf, Tmux uses that. My ~/.tmux.conf looks as follows.
set -g default-terminal "screen-256color" ##useful info on the status bar (if I use it): temperature, load, battery, and clock set -g status off set -g status-position top set -g status-right "[#[fg=red,bright] #(cat /sys/class/hwmon/hwmon0/temp1_input | colrm 3) °C#[default] ][#[fg=cyan,bright] #(cat /proc/loadavg | colrm 5) #[default]]#[fg=green]#[bg=black] #(cat /sys/class/power_supply/BAT0/capacity)% #(cat /sys/class/power_supply/BAT0/status) #[default]#[fg=yellow,bright] %a %R #[default]" set -g display-time 3000 set -g mouse-resize-pane on set -g visual-bell on set -g bell-action none set -g visual-activity on set -g lock-after-time 3000 ##useful titles: window number, process name, and its tmux status set -g set-titles on set -g set-titles-string "tmux.#I.#W.#F" setw -g aggressive-resize on ##unbind the default keys so they can be reassigned unbind-key - unbind-key s unbind-key p unbind-key f unbind-key q ##to see the status bar when needed bind-key b set-option status ##get the focused pane out of the way into a new background window bind-key - break-pane -d ##expand the sessions list to select a window interactively bind-key s choose-tree -suw ##a useful list of all panes to see something to fetch ##the last item with % included is most useful for fetching bind-key p list-panes -asF '#I.#P.#W.#D' ##fetch a background pane into the current window bind-key j command-prompt "join-pane -ds '%%'" ##reload the configuration file bind-key f source-file ~/.tmux.conf \; display-message "configged!" ##monitor activity in the focused window bind-key a set-window-option monitor-activity \; display-message "monitoring this window" ##kill all sessions except the named one bind-key q command-prompt "kill-session -at '%%'"
Byobu: More than Tmux
If Tmux seems hard, try Byobu that is gleefully configured out of the box and pretty much explains itself as you use it. The keybinds are totally different from Tmux though, no relation at all.