Skip to content

Instantly share code, notes, and snippets.

@trungnt13
Last active January 15, 2024 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trungnt13/bec3a7869f72deb8bfa25c318cf3a75f to your computer and use it in GitHub Desktop.
Save trungnt13/bec3a7869f72deb8bfa25c318cf3a75f to your computer and use it in GitHub Desktop.
[Note] Tmux, Byobu, Screen

Tmux Byobu Screen


Compare Tmux, Screen and Byobu

Tmux:

  • Pros:

    • Highly customizable and extensible through its configuration file and scripting.
    • Supports vertical and horizontal pane splitting, as well as pane resizing.
    • Offers a status bar that can display various information.
    • Actively maintained and widely used in the community.
    • Supports copy-paste functionality and scrollback buffer.
  • Cons:

    • Steeper learning curve compared to Screen and Byobu, especially for beginners.
    • Requires manual configuration for some features that are available out-of-the-box in Byobu.

Screen:

  • Pros:

    • A well-established and widely used terminal multiplexer.
    • Relatively easy to learn and use, with a simpler set of commands and hotkeys.
    • Supports basic terminal multiplexing features, such as session management, window splitting, and detaching/reattaching sessions.
  • Cons:

    • Lacks some of the advanced features and customizability offered by Tmux and Byobu.
    • The development of Screen has slowed down in recent years, with fewer updates and improvements.

Byobu:

  • Pros:

    • Byobu is built on top of Tmux or Screen, providing a more user-friendly interface and additional features.
    • Offers a highly informative and customizable status bar out-of-the-box.
    • Provides a set of convenient hotkeys and commands for managing sessions, windows, and panes.
    • Easier to learn and use for beginners compared to Tmux, while still offering advanced features.
  • Cons:

    • As Byobu is an enhancement of Tmux or Screen, it may have a slightly higher resource usage.
    • Some users may prefer the simplicity and direct control offered by using Tmux or Screen directly.

In summary:

  • if you're looking for a highly customizable and powerful terminal multiplexer, Tmux is a great choice.
  • If you prefer a simpler and more straightforward solution, Screen might be more suitable.
  • Byobu is an excellent option if you want a user-friendly interface with additional features built on top of Tmux or Screen

Tmux

Hello world, tmux:

  1. Start the tmux session: tmux new-session -s my_session_name
  2. List all sessions: tmux list-sessions
  3. Attach a session: tmux attach-session -t my_session_name
  4. Detach from a session: Prefix + d or tmux detach-client
  5. Detach a specific client from a session: tmux detach-client -t my_session_name -c my_client_name

Here is a list of important hotkeys for tmux:

  1. Prefix key: The default prefix key is Ctrl-b. You need to press this key before using any other tmux hotkeys.
  2. Sessions:
    • Create a new session: Prefix + :new-session
    • Switch to the previous session: Prefix + (
    • Switch to the next session: Prefix + )
    • List all sessions: Prefix + s
  3. Windows:
    • Create a new window: Prefix + c
    • Close the current window: Prefix + &
    • Switch to the previous window: Prefix + p
    • Switch to the next window: Prefix + n
    • List all windows: Prefix + l
    • Rename the current window: Prefix + ,
  4. Panes:
    • Split the current pane vertically: Prefix + %
    • Split the current pane horizontally: Prefix + "
    • Switch to the next pane: Prefix + o
    • Switch to the previous pane: Prefix + ;
    • Navigate between panes: Prefix + Up/Down/Left/Right
    • Close the current pane: Prefix + x
    • Toggle pane zoom: Prefix + z
  5. Copy mode:
    • Enter copy mode: Prefix + [
    • Exit copy mode: q
    • Start selection: Ctrl+Space
    • Copy selection: Enter
    • Paste copied text: Prefix + ]
  6. Mouse mode:
  7. Miscellaneous:
    • Detach from the current session: Prefix + d
    • Show help: Prefix + ?
    • Reload tmux configuration: Prefix + :source-file ~/.tmux.conf

These are some of the most commonly used hotkeys in tmux. You can customize these hotkeys and add more by editing your ~/.tmux.conf file.

Tmux structure

tmux
├── Session 1
│   ├── Window 1
│   │   ├── Pane 1
│   │   └── Pane 2
│   └── Window 2
│       ├── Pane 1
│       └── Pane 2
└── Session 2
    ├── Window 1
    │   ├── Pane 1
    │   └── Pane 2
    └── Window 2
        ├── Pane 1
        └── Pane 2

In this representation:

  • The top level represents the tmux environment.
  • Each session is a child of the tmux environment.
  • Each window is a child of its respective session.
  • Each pane is a child of its respective window.

Visual indication that Prefix key is pressed

⚠️ Warning!: WIP

If you're looking for a more visual indication, you can try customizing your tmux configuration to provide some feedback when the prefix key is pressed. One way to do this is by using the setw command to change the status bar color temporarily.

  1. Create tmux_prefix_indicator.sh
  2. chmod +x tmux_prefix_indicator.sh
  3. ./tmux_prefix_indicator.sh
#!/bin/bash
# Check if the tmux configuration file exists, and create it if it doesn't
TMUX_CONF="$HOME/.tmux.conf"
if [ ! -f "$TMUX_CONF" ]; then
  touch "$TMUX_CONF"
fi

# Add the necessary lines to the configuration file
cat <<EOT >> "$TMUX_CONF"
# Change status bar color when prefix is pressed
set -g prefix2 C-b
bind C-b set status-style "bg=colour33" \; switch-client -n \; set status-style "bg=default" \; switch-client -n
EOT

# Restart tmux
tmux source-file "$TMUX_CONF"

Tmux completetion and configuration for bash

  1. Save to tmux_autocomplete_setup.sh
  2. chmod +x tmux_autocomplete_setup.sh
  3. ./tmux_autocomplete_setup.sh
#!/bin/bash

########## Check for tmux and bash-completion installed
if [[ "$(uname)" == "Darwin" ]]; then
    if ! command -v brew >/dev/null 2>&1; then
        echo "Homebrew is not installed. Please install Homebrew and try again."
        exit 1
    else
        # Install tmux if not installed
        if ! command -v tmux >/dev/null 2>&1; then
            echo "Installing tmux..."
            brew install tmux
        fi

        # Install bash-completion if not installed
        if ! brew ls --versions bash-completion >/dev/null 2>&1; then
            echo "Installing bash-completion..."
            brew install bash-completion
        fi
    fi
elif [[ "$(uname)" == "Linux" ]]; then
    # Add a check for the user's permissions and provide an error message if they do not have the necessary permissions
    if ! sudo -v >/dev/null 2>&1; then
        echo "You do not have the necessary permissions to install packages. Please run the script as a user with sudo privileges."
        exit 1
    fi

    # Install tmux if not installed (Linux)
    if ! command -v tmux >/dev/null 2>&1; then
        echo "Installing tmux..."
        sudo apt-get install -y tmux
    fi

    # Install bash-completion if not installed (Linux)
    if ! type _init_completion &>/dev/null; then
        echo "Installing bash-completion..."
        sudo apt-get install -y bash-completion
    fi
else
    echo "Unsupported operating system '$(uname)'. Please install tmux and bash-completion manually."
    exit 1
fi

########## Determine the current shell and install tmux autocomplete
current_shell="$(basename "$SHELL")"

# Bash
if [[ "$current_shell" == "bash" ]]; then
    echo "Setting up tmux autocomplete for Bash..."

    if [[ "$(uname)" == "Darwin" && ! $(grep -q "/etc/profile.d/bash_completion.sh" ~/.bash_profile) ]]; then
        echo "[MacOS] Setup tmux autocomplete in ~/.bash_profile"
        # Append code block to .bash_profile
        cat << EOF >> ~/.bash_profile
# <<< bash-completion <<<
if type brew &>/dev/null
then
  HOMEBREW_PREFIX="\$(brew --prefix)"
    if [ -f \${HOMEBREW_PREFIX}/etc/bash_completion ]; then
        . \${HOMEBREW_PREFIX}/etc/bash_completion
    fi

  if [[ -r "\${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh" ]]
  then
    source "\${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
  else
    for COMPLETION in "\${HOMEBREW_PREFIX}/etc/bash_completion.d/"*
    do
      [[ -r "\${COMPLETION}" ]] && source "\${COMPLETION}"
    done
  fi
fi
# <<< bash-completion <<<
EOF
    fi

    # Check if tmux_completion is already installed, Download the tmux completion script for Bash
    if [ ! -f ~/.tmux_completion ]; then
        curl -o ~/.tmux_completion https://raw.githubusercontent.com/imomaliev/tmux-bash-completion/master/completions/tmux
    fi

    # Get the path to the bash profile
    if [[ "$(uname)" == "Darwin" ]]; then
        BASH_PROFILE="${HOME}/.bash_profile"
    elif [[ "$(uname)" == "Linux" ]]; then
        BASH_PROFILE="${HOME}/.bashrc"
    else
        echo "Unsupported operating system '$(uname)'. Please install tmux and bash-completion manually."
        exit 1
    fi

    if [ ! -f "${BASH_PROFILE}" ]; then
        echo "${BASH_PROFILE} does not exist. Stop the installation!"
        exit 1
    fi

    # Add the source line to ~/.bashrc or  ~/.bash_profile
    if ! grep -q "source ~/.tmux_completion" "${BASH_PROFILE}"; then
        echo "Adding tmux autocomplete to ${BASH_PROFILE}"
        echo "source ~/.tmux_completion" >> "${BASH_PROFILE}"
        # Apply the changes
        source "${BASH_PROFILE}"
    else
        echo "tmux autocomplete is already enabled in ${BASH_PROFILE}"
    fi
else
    echo "Tmux autocomplete is not supported for $current_shell."
    exit 1
fi

########## Customize tmux configuration
# Define the settings to be added
# copy-pipe command stores selected text in tmux buffer same to copy-selection,
# plus pipes selected text to the given command pbcopy. So we get text stored in two places:
# the tmux copy buffer and the system clipboard.
settings_to_add='
set -g history-limit 50000

## Change background and border of active pane
set-option -g pane-border-style fg=default,bg=default
set-option -g pane-active-border-style fg=red,bg=default

set-option -g default-terminal "xterm-256color"
# set-option -g default-terminal "screen-256color"
set -g window-style "bg=black"
# set -g window-active-style "bg=grey20"
set -g window-active-style "bg=#000040"

### copy-mode

# Set custom key bindings for copy mode
bind-key -T copy-mode 'i' send-keys -X cursor-up
bind-key -T copy-mode 'k' send-keys -X cursor-down
bind-key -T copy-mode 'j' send-keys -X cursor-left
bind-key -T copy-mode 'l' send-keys -X cursor-right
bind-key -T copy-mode 'u' send-keys -X start-of-line
bind-key -T copy-mode 'o' send-keys -X end-of-line
bind-key -T copy-mode '.' send-keys -X page-up
bind-key -T copy-mode ',' send-keys -X page-down

# selection and clipboard
bind-key -T copy-mode '[' send-keys -X begin-selection
bind-key -T copy-mode 'Space' send-keys -X begin-selection
bind-key -T copy-mode ']' send-keys -X clear-selection

# bind-key -T copy-mode Enter send-keys -X copy-selection-and-cancel
if-shell "uname | grep -q Linux" {
    bind-key -T copy-mode Enter send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
}
# macOS configuration
if-shell "uname | grep -q Darwin" {
    bind-key -T copy-mode Enter send-keys -X copy-pipe-and-cancel "pbcopy"
}
'

# Check if the tmux.conf file exists, if not, create it
tmux_conf="${HOME}/.tmux.conf"
if [ ! -f "${tmux_conf}" ]; then
    touch "${tmux_conf}"
fi

# Check if the settings are already in the tmux.conf file
if ! grep -qF -- "${settings_to_add}" "${tmux_conf}"; then
    # If not, append the settings to the file
    echo "${settings_to_add}" >> "${tmux_conf}"
    echo "Settings added to tmux.conf"
else
    echo "Settings already exist in tmux.conf"
fi

########## reload tmux
tmux source-file "$tmux_conf"

Issue with bash-completion in MacOS Homebrew

To check if bash-completion is properly installed in MacOS:

# ~/.bash_profile
if type brew &>/dev/null
then
  HOMEBREW_PREFIX="$(brew --prefix)"
    if [ -f ${HOMEBREW_PREFIX}/etc/bash_completion ]; then
        . ${HOMEBREW_PREFIX}/etc/bash_completion
    fi

  if [[ -r "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh" ]]
  then
    source "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
  else
    for COMPLETION in "${HOMEBREW_PREFIX}/etc/bash_completion.d/"*
    do
      [[ -r "${COMPLETION}" ]] && source "${COMPLETION}"
    done
  fi
  source $HOME/.tmux_completion
fi
echo -e "\nif [ -f \$(brew --prefix)/etc/bash_completion ]; then\n    . \$(brew --prefix)/etc/bash_completion\nfi\nsource ~/.tmux_completion" >> ~/.bash_profile

Copy-mode

  • Ctrl+b + [ to enter copy mode
  • i - up, k - down, j - left, l - right
  • , - page up, . - page down
  • u - home, o - end
  • [ - start selection, Space - start selection, ] - clear selection
  • Enter - copy selection to clipboard
  • Ctrl+r - reverse search (up), Ctrl+s - forward search (down)
  • q - quit copy mode

Terminal Tips and Tricks

Killall python3 process

for pid in $(ps -ef | grep "python3" | awk '{print $2}'); do kill -9 $pid; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment