How to manage command history in tmux?

Quick Answer

To view tmux command history, press Ctrl+b : and use the up/down arrows to cycle through previously used tmux commands. For shell command history within a tmux pane, use your shell's history feature (typically Ctrl+r or up arrow). To save tmux command history, add set -g history-file ~/.tmux_history to your ~/.tmux.conf file.

# View command history
Ctrl+b :(then use up/down arrows)

# Save command history to file
set -g history-file ~/.tmux_history

Detailed Explanation

tmux provides several types of history functionality that are useful for different purposes. Understanding these different history mechanisms can greatly improve your productivity.

Types of History in tmux

There are three main types of history in tmux:

  • Command history - Previous tmux commands entered in the command prompt
  • Scrollback history - Terminal output saved in each pane's buffer
  • Shell history - Command history of the shell running in each pane (bash, zsh, etc.)

tmux Command History

tmux maintains a history of commands entered in the command prompt (accessed with Ctrl+b :):

  1. Press Ctrl+b : to access the command prompt
  2. Use the up/down arrow keys to cycle through previously used commands
  3. Press Enter to execute a command
  4. Press Escape to cancel

Saving tmux Command History

By default, tmux's command history is not saved between sessions. To enable persistent command history:

# Add to ~/.tmux.conf
set -g history-file ~/.tmux_history

This configuration saves the command history to the specified file, making it available across tmux sessions and server restarts.

Accessing Scrollback History

tmux keeps a record of terminal output for each pane, which you can access in copy mode:

  1. Press Ctrl+b [ to enter copy mode
  2. Use the arrow keys or Vim/Emacs navigation keys to scroll through the history
  3. Press q to exit copy mode

The default scrollback size is 2000 lines. To increase it:

# Add to ~/.tmux.conf
set -g history-limit 10000  # Increase to 10,000 lines

Searching tmux History

You can search through the scrollback history in copy mode:

  • In Emacs mode (default): Ctrl+s for forward search, Ctrl+r for reverse search
  • In Vim mode: / for forward search, ? for reverse search

To check your current mode or change it:

# Check current mode
Ctrl+b :show-options -g mode-keys

# Set to vi mode
Ctrl+b :set-option -g mode-keys vi

# Set to emacs mode
Ctrl+b :set-option -g mode-keys emacs

Shell Command History

Shell history within tmux panes is managed by your shell, not tmux:

  • Press Up/Down arrow keys to cycle through recent commands
  • Press Ctrl+r for reverse search in bash/zsh
  • Use the history command to view all history

To enhance shell history in tmux, consider these shell configurations:

# For bash (add to ~/.bashrc)
export HISTSIZE=10000
export HISTFILESIZE=10000
export HISTCONTROL=ignoredups:erasedups
shopt -s histappend

# For zsh (add to ~/.zshrc)
HISTSIZE=10000
SAVEHIST=10000
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_SAVE_NO_DUPS
setopt SHARE_HISTORY

Capturing History to a File

There are several ways to save the scrollback history to a file:

# Capture the entire scrollback history of the current pane
Ctrl+b :capture-pane -S - -E -
Ctrl+b :save-buffer ~/tmux-history.txt

# From the shell
tmux capture-pane -pS - > ~/tmux-history.txt

# Capture with formatting preserved
tmux capture-pane -eS - | sed 's/\r$//' > ~/tmux-history-formatted.txt

The -S - option captures from the start of history, while -E - captures to the end.