A Gentle Introduction on tmux

tmux, short for terminal multiplexer, can retain the status of a remote server. And you do not have to lose your progress when the connection is lost. Rather, you can just reattach and pick up where you left. Additionally, as the name suggests, you can open multiple terminal windows at the same time, which is extremely handy when working with vim.

Sessions, windows and panes

There are three layers when it comes tmux. It is session, window and panes, in an increasingly more granular order.

The last two levels are fairly interchangeable, and windows can be understood as a workspace on MacOS or simply a tab, while the panes split one window into several parts.

There are two ways of operating inside tmux: using triggers or command lines. For instance, one can simply use trigger + c to create a new window inside a session instance, and the command line equivalence would be tmux new-window. My recommendation is to use triggers in the windows and panes level since they are more efficient and you need to use them a lot. And on the session level, you just cannot use triggers sometime.

The triggers are the key bindings that you got to press before a certain command to tell tmux that you are going to type a command as opposed to normal inputs. The default is Ctrl + b and I prefer Ctrl + a since it’s definitely faster. To do this, copy the following script to ~/.tmux.conf.

unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

Note that this might not work right away. Just like you need to restart the terminal after changing the bash_profile, a restart is also required to make it work. The quickest way to do this is to kill all the open sessions by tmux kill-server and tmux is going to restart, and the new key binding should kick in after a new session is created.

What it pertains to us the most, as far as I am concerned, is how to create, get rid of an instance, whether it is a session, a window or a pane. as well as navigation.

Useful code cheat sheet

Session

Like I said, using the command line when you are operating on the session level.

  • create a session
    tmux new-session -s new_session_name_here (dash s means session name)
  • rename a session
    tmux rename-session -t oldname newname. More often than not, the default name is a number. We usually want to change it to something more memorable and thus easier to refer to. (dash t means target)
  • attach a session
    tmux attach-session -t session_name. Arguably mostly useful thing when you are connecting to a server and want to reconnect without losing your progress.
  • kill a session
    tmux kill-session -t session-name

Several notes about these commands:

  • all of the commands for example attach-session, will also work by removing -session part.
  • Here’s the twist. You can be in the session, and rename or even kill that session while you in it. However, if you try tmux kill-session -a, it will not kill the current session, but all the other sessions.
  • Getting out of a session is probably a frequently used operation. The shortcut is trigger key (Ctrl B if it has been remapped as suggested) plus d key. d for detach obviously.

Windows

  • trigger + c to create a window
  • trigger + & to kill a window.
  • trigger + , to rename the current window.
  • trigger + 0-9 to select a window based on index. Tmux can only display one window at a time, but one can easily split one window into multiple panes.
  • trigger + w to list all the windows. The default window you got when the session is created is indexed 0.
  • trigger + n/p to list the next or the previous window.

Panes

  • trigger + " to vertically split the current window into two.
  • trigger + % to horizontally split the current window into two
  • trigger + arrow key to select which pane you want to be active.
  • trigger + {\} to swap panes.
  • trigger + Ctrl D to close an active pane. Note that this is different from detach from a session, which is trigger + d. Or you can just type exit and the pane is going to be closed.

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close