How can I clear scrollback buffer in Tmux?

前端 未结 13 2525
悲&欢浪女
悲&欢浪女 2020-12-12 09:10

FYI, I am using Tmux through the Mac OS X Terminal app.

相关标签:
13条回答
  • 2020-12-12 10:05

    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.

    0 讨论(0)
  • 2020-12-12 10:06

    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!

    0 讨论(0)
  • 2020-12-12 10:10

    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'
    
    0 讨论(0)
  • 2020-12-12 10:11

    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.

    0 讨论(0)
  • 2020-12-12 10:11

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

    0 讨论(0)
  • 2020-12-12 10:12

    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.

    0 讨论(0)
提交回复
热议问题