Clear scrollback in tmux?

Hi there,

I’ve recently begun using Vim/tmux. I would like to be able to clear the scrollback history in various panes when they get full, and have done a fair amount of searching online about this. It seems that the command :clear-history is supposed to do the trick, something equivalent to Cmd-K in a normal terminal. (For example, see articles here and here.) Unfortunately, when I enter this command from within tmux, it has no effect whatsoever.

It is commonly suggested to map the Ctrl-K key combo to the :clear-history command, but I can’t do that very well since I have done the Vim motion setup for navigating panes as explained on the Thoughbot blog. (I’ve tried alternate key bindings to no effect.) However, my understanding is that just entering the :clear-history by itself should work.

Here’s some info on my setup:

  • Mac OS X 10.9 Mavericks
  • Vim 7.4
  • Tmux 1.9a (installed via Homebrew)
  • .vimrc
  • .tmux.conf

Thank you in advance for any help!
Josh

Hi @joshukraine,

I’m not entirely sure what clear-history is supposed to do, and running it directly in tmux does nothing for me, but I ran into a similar problem with the clear functionality typically bound to Ctrl-l as I am also using the tmux navigation. I was able to work around it by using Prefixl with the following snippet:

bind C-l send-keys 'C-l'

You can see this in context in my .tmux.conf file here. Perhaps a similar approach could work for you, ie:

bind C-k send-keys 'C-k'

4 Likes

Thanks @christoomey for the response. I tried the key binding you suggested, but unfortunately nothing happens in tmux. As a troubleshooting step, I also tried commenting out the vim pane-switching bindings (in case they were causing a conflict), but this didn’t seem to make any difference either.

Am I correct in understanding that the clear command is supposed to clear the screen like Cmd-k does in a normal Mac terminal session? I tried entering :clear directly in the tmux command line, but nothing happened.

Any other ideas? Am I correct that Prefix l works for you to clear the scrollback on your screen from within a tmux session? Thanks again for your help.

Hi @joshukraine. Just did some tinkering and I think I understand this more completely now.

clear-history - This is a tmux command that will clear the buffer contents,
aka the lines of scrollback if you enter copy mode
(Prefix[).

clear - is a shell command to clear the visible scrollback lines.

Running tmux’s clear-history will only delete buffer contents that are not
currently visible. This means that there will be no visible sign that the
command has run. If you attempt to enter tmux’s scrollback / ‘copy-mode’ with
Prefix[, you will see that they are gone.

All this said, if you are using the Tmux navigator mappings, I would suggest
the following for clear and clear-history respectively:

# Use prefix C-l to clear the visible scrollback lines
bind C-l send-keys 'C-l'

# Use prefix C-k to clear the scrolled off (hidden) buffer lines
bind C-k clear-history

With these in place, you can use PrefixC-l to clear the
visible lines, then PrefixC-k to clear the scrolled off
lines. You can check that they are gone with Prefix[.

Hopefully this gets you what you are looking for.

2 Likes

Ah yes, you are awesome. That works! :smiley:

One last question: here on the forum, how do you format key names like you’ve been doing in your posts? (like [Prefix] [C-k] ) When I first joined the forum there was a pop-up window with formatting tips, but I can’t find where to access that now. Thanks again for the help!

Hey @joshukraine, glad that helped.

The formatting is actually using the <kbd> HTML tag. The markdown engine just passes the html through and the forum CSS has styling to make it look a bit more like keys. The same sort of thing happens on github in rendered wikis, on stack overflow, etc.

This was a super helpful discussion! I thought I would try to be slick and combine clearing visible scrollback lines with clearing the buffer history:

bind-key C-l send-keys 'C-l' \; clear-history

However, clear-history just doesn’t seem to work when chained this way. If I use a different command, like display-message 'foo' it works fine after the visible scrollback lines are cleared. Is there something I’m missing??

Turns out hitting Ctrl+l (lowercase L) twice will clear the visible lines and then the buffer with default tmux bindings