Zsh clear scrollback buffer

5,219
function clear-scrollback-buffer {
  # Behavior of clear: 
  # 1. clear scrollback if E3 cap is supported (terminal, platform specific)
  # 2. then clear visible screen
  # For some terminal 'e[3J' need to be sent explicitly to clear scrollback
  clear && printf '\e[3J'
  # .reset-prompt: bypass the zsh-syntax-highlighting wrapper
  # https://github.com/sorin-ionescu/prezto/issues/1026
  # https://github.com/zsh-users/zsh-autosuggestions/issues/107#issuecomment-183824034
  # -R: redisplay the prompt to avoid old prompts being eaten up
  # https://github.com/Powerlevel9k/powerlevel9k/pull/1176#discussion_r299303453
  zle && zle .reset-prompt && zle -R
}
zle -N clear-scrollback-buffer
bindkey '^L' clear-scrollback-buffer

clear and zle .reset-prompt && zle -R are added to make sure it works for multiline prompts, which is important.

References

Share:
5,219

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin 8 months

    With Bash CtrlL will clear the screen but not scrollback buffer. In the past I have worked around this by using:

    tput reset
    

    However I have noticed that this command will not clear the scrollback buffer with Zsh. So, how is it done?

  • Marius
    Marius almost 4 years
    That's for iterm2, while OP is asking about VTE (the two do not behave the same).
  • Simba
    Simba almost 4 years
    @ThomasDickey Yes, you're right. The sequence code is for xterm, not VTE. I missed it.
  • Kamil.S
    Kamil.S over 3 years
    You can achieve the same in one step printf "\ec\e[3J"