tmux

Tmux (and screen) is useful when you want to run a command on a remote machine that takes a long time to complete where you don't want to keep the connection open during the whole command.

For instance you could start a torrent download on a remote machine, disconnect and then reconnect when the download is finished.

ssh user@machine
tmux
rtorrent <torrent file>   # Hit ^S to start, ^K to stop download, ^Q to exit
<C-b d>     # to disconnect from tmux session
exit        # from ssh connection

When you want to check the download you can attach to the same session again.

ssh user@machine
tmux attach [-t session]

When the torrent download is completed you can quit rtorrent and the ssh connection.

You can do a lot of other things with tmux. It support multiple sessions, windows and panes. If you want to enter a tmux command you first need to enter tmux command prompt. This is done by issuing : after the tmux prefix key C-b. In general you control tmux via single letter keybindings (e.g. C-b d) or via commands, by first entering tmux command prompt.

C-b :       # Enter tmux command prompt

In the command prompt you may for instance list all supported commands.

C-b : list-commands     # or lscm
q                       # To exit from list view

To exit from the current command enter q.

Other useful commands are.

Window managment ...

Ctrl-b c # Create new window
Ctrl-b x # Delete current pane
Ctrl-b n # Move to the next window
Ctrl-b p # Move to the previous window
Ctrl-b l # Move to the previously selected window
Ctrl-b w # List all windows / window numbers
Ctrl-b <window number> # Move to the specified window number, the default bindings are from 0 – 9

Session management (note the :) ...

C-b : new-session     # Create new session (alias new)
C-b : kill-session    # Kill current session
C-b : list-session    # List all sessions (alias ls)
C-b : detach-client   # Detach client from sessions (alias detach). Also available via keybinding "C-b d"

copy_mode

I realized that, when using the terminal a lot, I often do a lot of copy and paste ...

Copy and paste is quite efficient by selecting output with mouse (left button) and copying (selected text) with center button (scroll wheel).

But I realized that it would be even more efficient if the terminal would act more as regular text editor for previous output. The idea was that you would be able to enter a "special mode" where you can leave the current command line and move up in the terminal output to select text just as efficiently as you would in a regular text editor (like vim).

You should be able to navigate the output (even search) quickly, select text and copy the selected text to the command line or system clipboard.

It turns out that this exact feature is already supported perfectly by tmux.

To make the solution a little more intuative some basic tmux configuration is required.

First install xsel (assumes OpenBSD).

pkg_add xsel

Then make a (this is system wide) configuration of tmux.

cat /etc/tmux.conf                                                             

# vim keys in copy or choice mode
set-window-option -g mode-keys vi

# copying selection vim style
unbind [
bind-key Escape copy-mode                       # enter copy mode; default [
bind-key -t vi-copy Escape cancel               # exit copy mode; or hit q
unbind p
bind-key p paste-buffer                         # paste; default ]
bind-key -t vi-copy v begin-selection           # begin visual mode
bind-key -t vi-copy V select-line               # visual line
bind-key -t vi-copy y copy-selection            # yank
bind-key -t vi-copy r rectangle-toggle          # visual block toggle

# read and write and delete paste buffer (xsel method)
bind-key < command-prompt -p "send to tmux:" "run-shell 'tmux set-buffer -- \"$(xsel -o -b)\"'"
bind-key > command-prompt -p "send to xsel:" "run-shell 'tmux show-buffer | xsel -i -b'"
bind-key + command-prompt "delete-buffer"

After this you can easily navigate the terminal output using copy-mode. Assuming Ctrl-b is the tmux bind key.

  1. C-b Esc - enter copy mode
  2. Navigate using regular vim commands h,j,k,l, search (/), arrow keys, w,b etc.
  3. Select using v, V
  4. Yank with y
  5. Exit copy mode with q
  6. Paste on command line using C-b p

To copy text to/from system clipboard do

  1. C-b > + Enter - to copy selection to system clipboard
  2. C-b < + Enter - to copy from system clipboard to tmux selection

Turns out that my idea wasn't new after all ...

References