15

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?

3
  • It's remotely possible that there's a bash alias for tput (though the bash style tends to be hard-coded escape sequences). Perhaps the TERM (and corresponding reset differ: some use the hard-reset, some don't). Commented May 3, 2019 at 23:13
  • Works for me, but this depends on the terminal. Which terminal emulator are you using? Do you have the same problem in other terminal emulators? Are you using an interface such as screen or tmux? Do you, at any point (e.g. in your zsh configuration, or in your .profile, or in your terminal configuration, or anywhere else), change the value of the TERM environment variable? Commented May 4, 2019 at 23:21
  • @Gilles I am just using stock Manjaro from here osdn.net/projects/manjaro/storage/xfce Commented May 5, 2019 at 0:28

2 Answers 2

13
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

3
  • That's for iterm2, while OP is asking about VTE (the two do not behave the same). Commented Jul 20, 2019 at 15:17
  • @ThomasDickey Yes, you're right. The sequence code is for xterm, not VTE. I missed it. Commented Jul 20, 2019 at 15:42
  • 9
    You can achieve the same in one step printf "\ec\e[3J" Commented Dec 27, 2019 at 8:23
2
  • Cmd-K
  • Menus / Edit / Clear to Start
2
  • What terminal is this for? Commented Jan 1, 2024 at 17:31
  • Works on MacOS Terminal Commented May 11 at 7:37

You must log in to answer this question.