FYI, I am using Tmux through the Mac OS X Terminal app.
why not? bind -n C-l send-keys C-l
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.
# 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
# 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
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
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.
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;
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!
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 SIGWINCH
es; 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.