31

When I use the terminal in OSX, I frequently use +K to clear the screen and scrollback buffer. It's deep in muscle memory.

I've started tinkering with tmux, and one gripe is that +K wipes the whole screen, panes and all. What I'd like is for it to affect only the focused pane.

Can I somehow configure this behavior?

4 Answers 4

37

Configure iTerm2 to send clear command

Preferences -> Keys -> + (add new global shortcut)

  • Keyboard shortcut: ⌘+k
  • Action: Send text
  • value:

clear\n

Alternatively configure iTerm2 to effectively map ⌘+k to ctrl+l

  • Keyboard shortcut: ⌘+k
  • Action: Send Hex Code
  • value:

0x0c

This way you won't see the text flash and the clear command won't pollute your history.

5
  • 2
    Simple and elegant solution. Thank you, my friend.
    – Rafael
    Commented Mar 18, 2016 at 3:37
  • Beautiful solution! You've made my tmux experience far more enjoyable. Thank you :)
    – Matt Darby
    Commented Jun 2, 2016 at 2:54
  • Just a warning, this will flash the text to the terminal
    – Jay
    Commented Jun 21, 2016 at 20:13
  • 2
    This is great, but it relies on the clearcommand and therefore won't work while inside irb or rails console.
    – Robert
    Commented Jan 20, 2017 at 21:22
  • Question was about terminal not iterm. Commented Apr 29, 2022 at 14:52
20

You can use prefix+r, after the +K, to redraw the tmux window.

If you can break the habit, a normal ctrl+l works normally per pane.

5
  • 4
    ctrl+l is not the same as cmd+k because you can still scroll up and see old history with ctrl+l. If you're tailing a log for example that quickly fills your scroll back cmd+k is invaluable because you know exactly when you last left off. ctrl+l will not do this for you.
    – Bjorn
    Commented Jun 3, 2014 at 15:44
  • @BjornTipling ⌘+K is a feature of the terminal emulator, and will not work inside tmux... this binding is designed to wipe the static stdout, which is not how tmux ,and other programs like vim/emacs/weechat/mutt, are being displayed.
    – demure
    Commented Jul 4, 2014 at 23:18
  • 3
    +1 for breaking the habit, I can't stop doing it until today. The best thing is to type clear instead I guess
    – zanona
    Commented Nov 21, 2014 at 16:11
  • how did you find out to do ctrl+l
    – Honey
    Commented Feb 2, 2021 at 13:04
  • 1
    @Honey ctrl+l has been the traditional clear binding in *nix for decades. If you want to be aware of things like this, you can read the documentation for the tools you use: ie man bash
    – demure
    Commented Feb 3, 2021 at 14:35
9

bind -n C-k send-keys -R \; send-keys C-l \; clear-history

Got this from here

Props to him. This works for me just like Cmd+k works on the terminal emulator, and works for me on macOS 10.12.5 with iTerm2.

2
  • Works like a charm, even when a command like tail -f ... or a server is running!
    – Derek
    Commented Nov 14, 2018 at 9:00
  • Unfortunately, it's in conflict with nano cut text command ^K. Commented Jul 31, 2020 at 13:58
3

You can add the following lines in your .tmux.conf:

# clear the current pane
bind -n C-k send-keys C-l \; clear-history

Then reload your tmux config. Now you can use your bind key +k (e.g.: Ctrl+b,k) to clear the current pane.

I don't know the syntax for OSX but I guess you can easily adapt the line.

2
  • I see that C-l clears the screen and clear-history clears the scrollback buffer. What does the \; part do? Commented Feb 18, 2016 at 11:58
  • it runs both @NathanLong
    – Jay
    Commented Jun 21, 2016 at 20:08

You must log in to answer this question.

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