Using Tmux

A long time ago I discovered screen. It made it possible to start applications in Linux and then disconnecting the SSH session, without stopping the applications. It was magic: otherwise the the applications just terminated if the SSH connection was lost.

Then I heard about tmux. It was like screen on steroids! I could split the windows any way I liked! Like in this easy example:

However, the Ctrl-a command key (as used in screen) was already deep in my muscle memory, and tmux defaulted using Ctrl-b. That didn’t feel right for me. Thus, one of my first tasks back then was reconfiguring tmux to use the familiar Ctrl-a based keystrokes.

tmux reads .tmux.conf configuration file from the user’s home directory when starting, if the file is there. I made my .tmux.conf available here:

Here are some keystroke commands I regularly use within a tmux session (which is started by just running “tmux”, provided that you have the package installed, use something like “sudo apt install tmux” (or equivalent on your system) to install it):

  • (Note: saying “Ctrl-a x” means that you first press Ctrl and a, you release them, and then press x, all this within a reasonably short time)
  • Ctrl-a d = detach from the current session, meaning that the session (and all the windows and panes within the session) keeps running in the background
    • Use command “tmux attach” (or just “tmux a”) to return (attach) to an existing session that is running in the background, for example when returning to the server after changing to another laptop
  • Ctrl-a c = create new window
  • Ctrl-a space = go to next window
  • Ctrl-a backspace = go to previous window
  • Ctrl-a a = send Ctrl-a to the window (otherwise plain Ctrl-a is just caught by tmux itself)
  • Ctrl-a Ctrl-a = go to the window that was last active (easy to switch back and forth between two windows by just keeping Ctrl down and pressing double-a as many times as you like)
  • Ctrl-a <number> = go to the window number N
  • Ctrl-a – = split the current pane (or window) in two panes (dash = creates a horizontal bar between the panes, thus making a vertical split)
  • Ctrl-a | = split the current pane (or window) in two panes (pipe = creates a vertical bar between the panes, thus making a horizontal split)
  • Ctrl-a o = go to next pane
  • Ctrl-a z = zoom the current pane in full window (use again to return the layout)
  • Ctrl-a arrow (up/down/left/right) = go to the pane in that direction
  • Ctrl-a pageup = open backscrolling window (browse around, use q to quit browsing)
  • Ctrl-a ? = show key bindings (use q to quit browsing)
  • Ctrl-a r = reload tmux configuration (handy when editing .tmux.conf within tmux session)
  • Ctrl-a , = rename the current window (the window name shows in the status bar, it shows the running executable (or shell) name by default)
  • … and many more commands and functions exist …

You can start several independent tmux sessions, either in purpose or by accident, as each plain “tmux” command in the shell starts a new session. Running “tmux attach” will try to attach to the latest one, or it will say “no sessions” if none exists. You can also use various command-line options to directly attach to a specific session, and list the sessions with “tmux list-sessions” command, see the tmux manual for more details.

Remember that the windows and the panes within the windows are just normal Linux shells, so there is no specific tmux command that you need to use when closing the windows (or panes): just exit from the shell (using “exit” command or Ctrl-d) and the window/pane will be closed.

Closing the last window in the session closes the tmux session as well.

With my configurations it is possible to also use mouse:

  • You can click the panes or the window names in the status bar to change to the specific pane or window
  • When splitting the window in panes you can drag the pane borders to resize the panes (you can also use Ctrl-a Ctrl-arrow to do that using keyboard)
  • You can use the scrollwheel of the mouse to browse the backscroll buffer (use q to quit scrolling)
  • Use Shift key to select text using the mouse (otherwise it just changes to the pane)

Final note: If you are using PuTTY as your SSH client, be sure to also read my post about PuTTY settings to ensure you get the most out of the tmux sessions.

Happy tmuxing!

Leave a Reply