250

I want to clear all scrollback history in a particular tmux pane.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Daya Sharma
  • 2,855
  • 2
  • 15
  • 12

16 Answers16

260

This same question has been plaguing me for quite some time. Here's the best I've come up with. Put this into your .tmux.conf file:

bind -n C-k clear-history

This binds ctrl-k to the tmux clear-history command. The -n after bind makes it so you don't have to issue the tmux command prefix (ctrl-b by default). I use bash, so ctrl-l already does the equivalent of typing "clear" at the command line. With these two keys I get a nice ctrl-l, ctrl-k combo, which moves all the scroll buffer off the screen (the "clear") and then deletes all that history (the tmux "clear-history" command).

It's not quite as nice as Terminal's, iTerm's, or Konsole's 1-key combos for clearing it out, but it's a world better than typing in clear-history all the time.

juanpaco
  • 6,303
  • 2
  • 29
  • 22
  • 18
    Nice. I've been doing `bind k send-keys "clear"\; send-keys "Enter"` for the clearing part, but using the built-in `ctrl+l` is simpler, and `clear-history` to get rid of the scrollback history is a good addition. – Henrik N Jul 15 '12 at 10:33
  • 1
    this binding screws my panes up, removing all text from all panes and removes the borders around them! Any ideas on whats up with that? – Ian Vaughan May 13 '13 at 12:15
  • Yikes. What shell program do you use? Does it by chance already have some behavior defined for ctrl+k? – juanpaco May 13 '13 at 12:17
  • iTerm2. Just seen that ctrl+k already does this without the tmux binding set, so it must be an iTerm2 mapping. – Ian Vaughan May 13 '13 at 12:21
  • Weird. I use iTerm2 as well. I haven't updated it for some time, so it might be a version difference. I also don't use C-k anymore, because that caused problems with my vim bindings. I use C-n now. – juanpaco May 13 '13 at 12:49
  • I like the answer of 'bind-key b send-keys -R \; clear-history' a bit more, as no reason not clear current screen. – justingordon Dec 11 '13 at 23:20
  • 1
    Of course you don't need to set up a key binding for this. You can just do `C-b :clear-history` – Michael Mior Jun 09 '16 at 13:25
  • To get the "clear_screen + clear_buffer" functionality of iTerm's C-k, you can put the following two lines (and a comment) in your tmux.conf: `# Easily clear history and simulate iTerm ctrl-k function` `bind -n C-k send-keys -R C-l \; clear-history` `bind C-k send-keys C-k # pass through for C-k` ``` – Tyler Abair Oct 17 '17 at 19:58
  • This binding doesn't seem to have any effect on my iTerm tmux session. – p0lAris Aug 10 '18 at 04:57
  • `send-keys "clear"` was just writing "clear" for me when the pane was running something that is outputting to stdout. I found that `send-keys -R` works better. – kilgoretrout Oct 25 '18 at 17:52
106

Every answer on here talks about adding new key bindings.

If you just want to do it occasionally you don't need a mapping at all...

Prefix defaults to <Ctrl-b>

Just type <prefix> + : in the relevant pane and then type clear-history and press enter.

If you are on the command line, you can run tmux clear-history in the pane in question for the same effect.

One of the cool things about tmux is that you can just run any of the commands like that... or run them in a shell/script them like tmux command ... or make a keyboard shortcut for them.

user295691
  • 7,108
  • 1
  • 26
  • 35
JonnyRaa
  • 7,559
  • 6
  • 45
  • 49
  • 9
    It is possible to type the first three characters `cle`, and then autocomplete with `` at the first time, then at the second time use `` to access the previous entry. This way it is quite fast. – bct Aug 27 '18 at 02:42
  • 1
    This does not work for me because when I start typing after hitting ` the `c` key is set to create a new window. – fraxture Apr 01 '20 at 17:44
  • 1
    @fraxture I think you misunderstand - after you press then press `:` it gives you an interactive prompt - that's where you'd type 'c' – JonnyRaa May 04 '20 at 17:00
  • 2
    I think it's worth adding to this answer (explicitly) that you can run `tmux clear-history` for the same effect. The key binding answers really make this a lot more complicated (and error-prone for a rare operation) than it has to be. – user295691 Aug 25 '20 at 15:43
  • You can also use it like `clear && tmux clear-history` – Qin Heyang Oct 31 '21 at 11:55
66

As @juanpaco correctly stated, clear-history is the command to clear the scrollback buffer.
I'll add that I like to also clear what's on the screen in the same command. Issuing a send-keys -R resets (clears) the screen, so I use the following in my .tmux.conf

bind-key b send-keys -R \; clear-history

This clears the screen AND the scrollback buffer.

Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129
  • this doesn't quite work for me: my tmux doesn't know about any such "-R" option for send-keys... – gatoatigrado Oct 13 '12 at 00:18
  • 1
    Directly from tmux documentation on `send-keys`: "The -R flag causes the terminal state to be reset." You probably just need to update. – Mark Bolusmjak Oct 13 '12 at 15:59
  • 27
    The drawback of using -R is that your current input-line becames hidden, differently from when you clear your terminal with Ctrl+l. What I did is `bind -n C-l send-keys C-l \; clear-history` – volpato Mar 26 '14 at 16:44
  • 8
    For those of you who don't know (like I didn't), to arrive at the state to enter the `clear-history` command, press `Ctrl+B`, and then start the command with a colon (`:`). – palswim May 19 '14 at 23:11
  • 3
    @volpato I like your approach,but I find it still leaves one 'screen' of the buffer in the scroll back. No idea why. -R blanks out the prompt, but I 'resolved' it by adding an extra C-m at the end (but then that puts it on the second line). – Gdogg Feb 05 '15 at 20:02
  • I can fix the one screen issue by adding a sleep between clear screen and clear-history, and I can clear the bash-scrollback using `reset` instead of `clear`.: `bind-key C-k send-keys "reset" C-m\; run-shell "sleep 0.1"\; clear-history` – Gdogg Feb 05 '15 at 20:11
  • 4
    old thread, but I fixed the prompt-issue by adding a `send-keys C-l` after `clear-history`. I.e. `bind -n C-l send-keys -R \; clear-history \; send-keys C-l`. This worked out to be the fastest for me. – fnurl Jun 30 '17 at 09:31
  • 6
    You can condense this to `send-keys -R C-l \; clear-history`. – ivan Dec 10 '17 at 14:24
24

If you want to combine CTRL-L plus clear-history, add this to your ~/.tmux.conf:

bind u send-keys C-l \; run-shell "sleep .3s" \; clear-history

This even works if you're in a MySQL shell for instance.

Hotschke
  • 9,402
  • 6
  • 46
  • 53
plu
  • 1,321
  • 10
  • 14
  • 2
    This doesn't seem to work during, say, `tail -f some.log`, right? Is there a way to combine `ctrl-Z` with this? Can seem to get it right. – Aaron Gibralter Jun 22 '15 at 21:37
21

I found using send-keys -R to be a little slow - here is another way to clear screen and history with a single command

bind-key C send-keys "clear && tmux clear-history" \; send-keys "Enter"  

A nested tmux call is used as the more obvious

bind-key C send-keys "clear" \; send-keys "Enter" \; clear-history

fails to clear the screen's current text from the history - the clear-history command appears to run in a separate thread to send-keys.

user456584
  • 86,427
  • 15
  • 75
  • 107
cage433
  • 459
  • 3
  • 5
  • 6
    there's one caveat with this approach and it's that it only works on local shells, because it sends the tmux clear-history as string to be written to the active pane. I've adapted it to: ```bind-key E send-keys "C-k" \; send-keys "C-u" \; send-keys "clear" \; send-keys "Enter" \; run-shell "sleep .3s; tmux clear-history"``` – Santi P. Oct 23 '13 at 19:09
20

Found this works best in TMUX 2.6, clearing the screen, scrollback, but keeping the prompt displayed after.

Uses Ctrl-L

bind-key -n C-l send-keys C-l \; send-keys -R \; clear-history
ideasman42
  • 42,413
  • 44
  • 197
  • 320
19

Way simpler than most, I just created a shell script called cls, and run it whenever I want to clear my screen and scrollback buffer.

All it is is this:

cls

clear;
tmux clear-history;
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
  • 3
    But what if you don't have access to the command line. Such as when a server is running and you're watching the logs. – zudduz Oct 27 '16 at 18:23
  • where would you be watching the logs from? Over a webserver? If so, then I don't get why tmux is involved? – Brad Parks Oct 27 '16 at 18:32
  • 1
    It's nice to be able to watch server logs and your vim editor simultaneously. – zudduz Oct 28 '16 at 15:11
  • Do you use tmux splits? If not, just split your window into 2, and view your logs in one, and use vim in the other by just running `tmux split" inside your tmux session... But maybe I'm not getting what you're doing? – Brad Parks Oct 28 '16 at 16:05
7

Ctrl-L is used in console applications to redraw the screen. I found that when bound to send-keys -R it would cause the arrow keys to no longer function correctly in some applications (eg vim, mc).

To retain the redraw functionality in console applications, I used:

bind-key -n C-l if-shell -F '#{alternate_on}' 'send-keys C-l' 'send-keys -R C-l; clear-history'

This requires the tmux option alternate-screen to be on (which it is by default).

Phil
  • 353
  • 4
  • 7
7

if you clear your screen with clear, you can check whether TMUX is set, and create an alias accordingly.

put this in your .bashrc:

if [[ $TMUX ]]; then
  alias clear='clear && tmux clear-history'
fi
Galarmo
  • 303
  • 3
  • 7
4

For a quick fix to clear individual tmux panes you could set up an alias...

# Tmux Clear pane
alias tc='clear; tmux clear-history; clear'
Paul Hale
  • 307
  • 4
  • 14
3

I've used some of the above plus other sources to come up with:

bind k send-keys C-u \; send-keys C-k \; send-keys " clear && tmux clear-history" \; send-keys "Enter" \; run-shell "sleep .3s" \; send-keys "Up" \; send-keys C-u 

The leading space in " clear && tmux clear-history" prevents the command from being written to the history file (providing you have your shell setup to treat leading spaces this way; google "hist ignore space" + the name of your shell for more info). I like to have this command not show up in my history since this is more inline with ctrl-k in the Terminal.

The first send-keys C-u and send-keys C-k will clear whatever is currently typed at the prompt to ensure that the " clear && tmux clear-history" is successful (e.g., if you've typed "ABCDEFG" at the prompt and have your cursor between the D and the E, this ensures that "ABCD clear && tmux clear-historyEFG" is not sent to the shell, which would fail).

The send-keys "Up" and last send-keys C-u clears out the last items from your shells internal history. Even with the trailing space mentioned above the internal history of the shell will include the " clear ..." line. Sending up and Ctrl-u gets rid of this.

Lastly, in iTerm I set ctrl-k to map to ctrl-a k (I have my tmux prefix set to ctrl-a), so I can type ctrl-k which is what my hands want to do from so many years of doing so. I do this, by going to iTerm > Preferences > Profiles > Keys and adding a shortcut to send the hex code "0x01 0x6B". There's a great article here which gives more info on using hex codes with tmux and iTerm: http://tangledhelix.com/blog/2012/04/28/iterm2-keymaps-for-tmux/

That pretty much gives me ctrl-k with tmux. The only thing that still kinda nags at me is that the real ctrl-k without tmux doesn't have problems if you currently have something typed at the prompt and will preserve what you've typed while clearing the screen. As mentioned, this approach needs to clear what's typed so the " clear ..." command doesn't fail. But it's pretty damn close!

Brett
  • 83
  • 1
  • 1
  • 7
2

So, I've been using plu's approach from above for a while, but I got fed-up with the limitations thereof (basically, the ⌃L passed through is meaningless unless piped to a program that understands it.)

So I've improved upon the various approaches in different answers to this thread; although complex, this approach works with both shells and other commands:

# ⌃K: Clears the current pane (from <https://stackoverflow.com/a/34162098>)
bind-key -n C-k \
   if-shell "test \"$(printf '#{pane_current_command}' | tail -c 2)\" = sh" \
      "send-keys C-l ; run-shell 'sleep .3s' ; clear-history" \
      "split-window -vp 100 ; clear-history -t ! ; kill-pane"

Try it with tail -f /private/var/log/system.log or something!


Caveats:

There's one important note here: this is invisibly resizing the pane being cleared, if it's not a shell. This can trigger resizing behaviour in some command-line applications listening for SIGWINCHes; but my reasoning is that this isn't a big problem, because those are programs you're very likely not going to be trying to ‘clear’ anyway.

In addition, the shell-quoting situation is already a mess, and can easily become more of one when embedding #{pane_current_command}, so be careful, you may have to modify this based on your default-command setting.

The same applies to my testing of the end of that command matching "sh"; if you have a default-command of something like /bin/bash --login or something complicated involving exec, the actual command may not end with "sh"; use ⌃B : to execute display-message '#{pane_current_command}' if you want to see what is being tested against.

Community
  • 1
  • 1
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
2

After a lot of research and spending time. I have found the best way for me on zsh and terminal.app

I am using prefix-c to clear the screen and prefix-C to clear the history and the scroll buffer and leaving no lines above because I find it annoying.

Without Vim

# clear screen
bind c send-keys 'C-l'
# clear screen and history
bind C send-keys -R \; send-keys C-l \; clear-history \; send-keys

With Vim

# check if the pane is running vim
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \ | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

# clear screen
bind c if-shell "$is_vim" "send-keys c" "send-keys 'C-l'"
# clear screen and history
bind C send-keys -R \; send-keys C-l \; clear-history \; send-keys
Rahul Katariya
  • 3,528
  • 3
  • 19
  • 24
1

why not? bind -n C-l send-keys C-l

  • I would like to know to me too if there's a reason to no recommend this. – Gaston Sanchez Aug 31 '15 at 22:10
  • 4
    First of all, that's not necessary: `-n C-l` basically says ‘catch this’, and then you're immediately passing that same thing through. (i.e. this is a no-op.) … Second, your *intent* is off, because `⌃L` clears *the screen*, not the scroll back: if you hit `⌃B [` and then scroll up, you'll see that all of the scrollback is still recorded; the goal of this question is to clear **that** (tmux's) scrollback, not the visible terminal. – ELLIOTTCABLE Dec 08 '15 at 16:13
1

If you want a complete pane clearing, another option is to temporarily resize the pane to one line, clear the history, then resize it back. This should work possibly flicker-free with one tmux chain of commands. The advantage is that it works regardless of what program is running in the pane. Of course this makes the shell script logic a bit more complicated.

bind-key -n F11 run "
    if [[ #{window_panes} == "1" ]] ; then
        tmux send-keys C-l; sleep 0.1 ; tmux clear-history ;
    else
        restored_height=$((#{pane_height} + #{pane_at_bottom}));
        tmux resize-pane -y 1 \\; clear-history \\; resize-pane -y \${restored_height} ;
    fi
"
Dan Aloni
  • 3,968
  • 22
  • 30
-1

If you struggling on OSX with Iterm:

enter image description here