Create session with a name

SESSION_NAME='mysession'
tmux new -s "${SESSION_NAME}"

Create detached session with a name

This particular example will run SimpleHTTPServer in the background:

SESSION_NAME='python_sesh'
tmux new -s "${SESSION_NAME}" -d 'python3 -m http.server'

Kill tmux session programmatically

kill -9 "$(top -n 1 | pgrep tmux)"

Attach to session with name

SESSION_NAME='mysession'
tmux a -t "${SESSION_NAME}"
# Alternatively:
tmux attach -t "${SESSION_NAME}"

List sessions

# Outside of a tmux session:
tmux ls
# Within a tmux session:
Ctrl b s

Background session

Ctrl b d

Rename current session

Ctrl b $

Kill session

SESSION_NAME='ohno'
tmux kill-session -t "${SESSION_NAME}"

Background session on remote host and close ssh session

return ~ .

Resources: https://gist.github.com/henrik/1967800


Use tmuxinator for environment automation

Great tutorial here: https://collectiveidea.com/blog/archives/2017/03/27/using-tmuxinator-to-automate-your-environment


Use mouse to copy and paste in a pane

In iTerm2:

  1. Hold down the Option key and the alt key
  2. Drag your mouse with the left click to select a body of text in a particular pane

Use Ctrl c to copy and Ctrl v to paste.

If on a mac:

# Copy
⌘ c
# Paste
⌘ v

Resource: https://stackoverflow.com/questions/12287432/how-to-copy-to-system-clipboard-from-tmux-output-after-mouse-selection


Show Help Menu

This particular example will show the help menu for Ctrl b t, which will display the current time:

tmux_clock

Ctrl b / t

Enable Scrolling

Type this command in:

Ctrl b [

At this point you can use your arrow keys, page up/page down, etc.

Resource: https://superuser.com/questions/209437/how-do-i-scroll-in-tmux


Screen splitting

Horizontal split:

Ctrl b "

Vertical split:

Ctrl b %

Change direction of split:

Ctrl b SPACE-BAR

Resources: https://gist.github.com/MohamedAlaa/2961058

Change starting dir of new tmux session

  1. Detach from the tmux session
  2. Run this command:
SESSION_NAME='mysession'
NEW_START_DIR='/home/ubuntu/somewhere'
tmux attach-session -t "${SESSION_NAME}" -c "${NEW_START_DIR}"

Resource: https://stackoverflow.com/questions/27307815/how-to-change-the-starting-directory-of-a-tmux-session

Detach from nested tmux session

Ctrl b Ctrl b d

Resource: https://superuser.com/questions/249659/how-to-detach-a-tmux-session-that-itself-already-in-a-tmux

Paste

Ctrl b ]

Reload tmux config

tmux source-file ~/.tmux.conf

Add this to your dotfiles so you can run it for all panes:

source_tmux_conf() {
    session=`tmux display-message -p "#S"`

    for pane in $(tmux list-panes -s -F "#{pane_id}"); do
        tmux send-keys -t $session.$pane "tmux source-file ~/.tmux.conf" C-m
    done
}