How can I clear scrollback buffer in Tmux?

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

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

相关标签:
13条回答
  • 2020-12-12 09:48

    why not? bind -n C-l send-keys C-l

    0 讨论(0)
  • 2020-12-12 09:51

    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.

    Without Vim

    # 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
    

    With Vim

    # 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
    
    0 讨论(0)
  • 2020-12-12 09:56

    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
    
    0 讨论(0)
  • 2020-12-12 09:59

    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.

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

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

    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!


    Caveats:

    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 SIGWINCHes; 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.

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