4

How to clear both tmux history (tmux clear-history) and zsh (zle clear-screen) with one key combination?

A common way of clearing screen is ^L, and I tried adding the following to .tmux.conf:

bind -Troot C-l send-keys C-l\; clear-history

So ^L clears the screen, and clears almost all the history, except that last one screen.

A subsequent ^L clears it all.

Can the same be achieved with one key combination?

bind -Troot C-l send-keys C-l\; clear-history\; send-keys C-l\; clear-history

doesn't work. Neither the following does:

bind -Troot C-l send-keys C-l C-l\; clear-history
0

3 Answers 3

8
+50

Try:

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).

Also, issuing a send-keys -R resets (clears) the screen:

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

This clears the screen AND the scrollback buffer.

1
  • 5
    Worked like a charm, thanks a lot. I ended up with bind-key -n C-l send-keys -R ^M \; clear-history. Added ^M to send a carriage return so that the prompt appears. From the docs: bind-key: -n is an alias for -T root, send-keys: The -R flag causes the terminal state to be reset. Commented Jul 1, 2019 at 20:27
5

I ended up doing this differently, from zsh itself:

# ~/.zshrc
clear-scrollback-and-screen () {
  zle clear-screen
  tmux clear-history
}
zle -N clear-scrollback-and-screen
bindkey -v '^L' clear-scrollback-and-screen

For one single reason - I had a C-l mapping in Vim, and the occasional press was clearing Vim screen.

There's a shortcoming to that solution though, it only works in the shell.

2

The gui solution:

  • On gnome you can setup custom keyboard shortcut to run a custom commands/script this article explain how to do it

  • On KDE you can as well setup custom keyboard shortcut to run a custom command/script this article explain how to do it

The shell solution:

You can use shell alias and function to achieve this to a command... under the file ~/.zshrc

For instance add this to ~/.zshrc and restart the shell:

c() {
   tmux clear-history
   clear-history
   clear
   reset
}

alias cc='clear'

You can then use c or cc (as a command) to execute what you want.

1
  • did you intend to put tmux clear-history inside the function?
    – iconoclast
    Commented Aug 14, 2020 at 0:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .