tmux
tmux is a terminal multiplexer that allows easily running multiple applications in the same terminal and keeping everything as a detached process.
Very similar to GNU Screen if you have used that before.
What I do is I simply have tmux starting command in .bashrc. This has added benefit that most IDEs that offer terminals will also start tmux automatically.
# automagical tmux if not already in a tmux session
if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then
tmux attach-session -t default || tmux new-session -s default
fi
tmux is especially useful when connecting to remote hosts and want to keep processes running after disconnecting.
cltr+b, d # detach from the tmux session, leaving it running
tmux attach # attach back to tmux session
You start, attach to and detach from tmux sessions. A single machine can have many tmux sessions running.
Sessions (current session shown bottom left in [brackets])
> Windows (windows are shown in bottom bar, current marked with *)
> Panes (tiled sections of a window, active highlighted with green)
I mostly vanilla tmux keybindings for easier cross-system usage, but most people like to change the default prefix key from ctrl+b to ctrl+a as ctrl+b is tedious to press.
My ~/.tmux.conf config is rather minimalistic:
# the default is emacs, but if $EDITOR is set to vim, it will default to vi
# set -g mode-keys vim
# start window and pane numbering at 1, not 0
set -g base-index 1
setw -g pane-base-index 1
# disable delay that tmux waits for "escape keys" to determine if it's
# a tmux keybinding, defaults to 500 (ms) but then pressing esc to
# change modes on vim takes forever...
set -sg escape-time 0
Remember to reload the config after changes with:
# ctrl+b, :
source-file ~/.tmux.conf
Basic keybindings:
tmuxStart defaulttmuxsession.ctrl+b, cCreate a new window with a single pane.ctrl+b, 0-9Jump to a specific window.ctrl+b, "Split pane horizontally.ctrl+b, %Split pane vertically.ctrl+b, arrowsSelect another pane.ctrl+b, ctrl+arrowsResize selected pane.ctrl+b, zMaximize/normalize selected pane.ctrl+b, xClose the current pane, or window if it's the last pane.
Pane juggling keybindings:
ctrl+b, spaceCycle through pane layouts.ctrl+b, !Move current pane to a new dedicated window.ctrl+b, :, join-pane -s 1Move window 1 to current window.- you can also move panes and windows between sessions with
join-pane -s [session_name]:[window].[pane] -tallows you to select the window to join to if not the current one
Session keybindings:
ctrl+dEnd of Transmission -signal like normal, closes active pane.ctrl+b, dDetach from the session, leaving it running in the background.tmux attachAttach back to detachedtmuxsession
Browsing keybindings:
ctrl+b, [Enter copy mode for copy-paste, browsing and such.ctrl+b, [, ?Search up in terminal,nfor next,Nfor previous.ctrl+b, [, /Search down in terminal,nfor next,Nfor previous.ctrl+b, [, navigate-with-vim, space, navigate-with-vim, enterCopy totmuxclipboard.ctrl+b, ]Paste fromtmuxclipboard.ctrl+b, :, capture-paneCopy the visible pane text totmuxclipboard.- you can also save it to a file with
capture-pane; save-buffer buffer.txt - you can explore clipboard with
list-buffersandchoose-buffer
You can use tmux for automation, but I use rusmux for that. Many people use tmuxinator but I don't like the idea of installing Ruby just for that.
# sends the command to specific session:window.pane
tmux send-keys -t default:1.2 'cd ~/Projects/my-thing' C-m
Sources
- tmux - Productive Mouse-Free Development, Brian P. Hogan