Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 4 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 ]]
        then
                zle clear-screen
        else
                zle accept-line
        fi
}

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

 
discard

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.