How to clear a pane in tmux?

Quick Answer

To clear a tmux pane, use clear or Ctrl+l for basic clearing. For a complete reset, use clear && printf '\e[3J' or reset. You can also create a key binding like Ctrl+b C for a thorough clear command.

clear

Detailed Explanation

There are multiple ways to clear the content of a tmux pane, ranging from simple screen clearing to full terminal resets. The method you choose depends on how thoroughly you want to clear the display.

Method 1: Basic clear command

# Standard shell clear command
clear

# Or use keyboard shortcut
Ctrl+l

This moves the current line to the top of the screen and clears everything below it. However, you can still scroll back to see previous content. This is equivalent to the clear command in most shells.

Method 2: Clear with scrollback reset

# Clear and reset scrollback buffer
clear && printf '\e[3J'

This clears both the visible content and the scrollback buffer, giving you a truly clean slate. The \e[3J ANSI escape sequence specifically clears the scrollback buffer in most terminal emulators.

Method 3: Terminal reset

# Reset the terminal
reset

# Or use tput
tput reset

This completely resets the terminal state, clearing the screen and fixing any display issues. It's the equivalent of turning your terminal off and on again.

Method 4: Using tmux's send-keys command

You can create a key binding in your ~/.tmux.conf to clear the current pane:

# Add binding to clear screen and scrollback with prefix+C
bind C send-keys "clear && printf '\e[3J'" \; send-keys "Enter"

With this binding, pressing Ctrl+b C will send the clear command followed by Enter to the current pane.

Method 5: Clear all panes at once

To clear all panes in a window simultaneously, you can use synchronized panes:

# Enable synchronized panes
Ctrl+b :setw synchronize-panes on

# Clear all panes
clear

# Disable synchronized panes
Ctrl+b :setw synchronize-panes off

Method 6: Using a custom function

Create a function in your shell's configuration (like ~/.bashrc or ~/.zshrc):

# Add to your shell configuration
function clear_pane() {
  clear && printf '\e[3J' && tmux clear-history
}
alias cls="clear_pane"

Now you can simply type cls to thoroughly clear the pane and its history.

Pro Tip

If you frequently need to clear panes, consider these key binding alternatives for your ~/.tmux.conf:

# Clear with Ctrl+l (without prefix)
bind -n C-l send-keys C-l \; run 'sleep 0.1' \; clear-history

# Clear history only, without clearing the screen
bind -n C-k clear-history

Vim Users Note

If you use Vim inside tmux, the Ctrl+l binding might conflict with Vim's redraw screen command. In that case, use a different key binding or prefix-based clear command.