Skip to content

Instantly share code, notes, and snippets.

@mberkowski
Last active October 1, 2024 18:16
Show Gist options
  • Save mberkowski/ec41d61cbbb9c4e69c93 to your computer and use it in GitHub Desktop.
Save mberkowski/ec41d61cbbb9c4e69c93 to your computer and use it in GitHub Desktop.
Tmux Basic Usage

Basic Tmux Usage

https://tmux.github.io/ <-- Tmux project

http://z.umn.edu/tmuxgist <-- this document!

The Prefix Keystroke Ctrl-b

  • Crtl-b c Create a new window
  • Crtl-b <N> Switch to window <N>
  • Crtl-b ? List available keystrokes
  • Crtl-b , Rename current window
  • Crtl-b " Split window horizontally
  • Crtl-b % Split window vertically
  • Crtl-b <arrow> Move around panes in the current window
  • Crtl-b w List windows

(GNU screen uses Ctrl-a as its default prefix. Check Ctrl-a ? for all bindings, many similar)

Session Management

  • $ tmux new-session -s <name> Create a new session named <name>
  • $ tmux list-sessions List currently active sessions
  • $ tmux attach -t <name> Reattach to session named <name>
  • Crtl-b d Detach session (leaving it running!)

Configuration

There are LOTS AND LOTS of options configurable in $HOME/.tmux.conf. Some useful things are:

  • set default-terminal "screen-256color" for a sane & sensible $TERM
  • set -g mouse-select-pane on allows you to click to select panes
  • set -g mode-mouse on Enables mouse/trackpad scrolling within the window (rather than scrolling the outer terminal)

Session Sharing

A session can be shared with another user who has read/write access to the its socket.

# Start the Tmux session and give it a socket path
# The session's name is "shared"
$ tmux -S /tmp/shared-session.sock new-session -s shared

# Provide read/write access by changing its group
# This makes it accessible to ANYONE IN THE GROUP
$ chgrp sharedgrp /tmp/shared-session.sock
$ chmod g+rwx /tmp/shared-session.sock

Now another user may join your session.

# The other user's shell on the same server...
# Attach to the session named "shared" while passing the socket path
$ tmux -S /tmp/shared-session.sock attach -t shared

Security Notes on Shared Sessions

  • When the second user attaches, that user is acting under the original user's account. The system sees only one user.
  • You probably shouldn't create a shared session intending to persist it after everyone disconnects. (see previous)
  • The Socket may not be removed when the session is terminated. Remove it manually rm /tmp/shared-session.sock
  • Don't chmod 777 the socket! Create it in as limited way as possible to accomplish what you need. Ideally this would be by giving it group ownership to the smallest group to which you both belong.

Easier sharing with tmate

tmate (http://tmate.io) supposedly makes sharing a little easier and is a fork of Tmux. I have not used it.

Other Tricks

Bash function to SSH and initialize or reattach to a session

sshtmux()
{
    # A name for the session
    local session_name="$(whoami)_sess"

    if [ ! -z $1 ]; then
        ssh -t "$1" "tmux attach -t $session_name || tmux new -s $session_name"
    else
        echo "Usage: sshtmux HOSTNAME"
        echo "You must specify a hostname"
    fi
}

Comparison with GNU Screen

Tmux makes GNU Screen obsolete, being much easier to work with. Things Tmux can do which Screen can't include:

  • Retaining split panes when detaching (this is UNBELIEVABLY IMPORTANT)
  • Not requiring nested sessions (see previous)
  • Vertical pane splitting without special patches

Screen allows for a password-protected session lock, Tmux does not have this feature (afaik).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment