Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to clear my terminal upon hitting submitting an empty command. In other words, I would like to call clear when I hit Enter on empty line. Is that possible?

I understand that is not how the terminal works, but there might be a nice hack such as binding the Enter key and checking terminal or something.

share|improve this question
up vote 7 down vote accepted

This function should do what you want; all it does it to check if the current prompt input ($BUFFER) is empty (-z):

magic-enter () {
        if [[ -z $BUFFER ]]
                zle clear-screen
                zle accept-line

Define it as a widget with

zle -N magic-enter

and then bind it to [ENTER]:

bindkey "^M" magic-enter

If you use a non-standard key binding for [Enter], adapt the line with zle accept-line. You can check with bindkey | grep "\^M".

share|improve this answer
Awesome, thanks :) – Mikulas Dite Jul 29 '13 at 22:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.