I would like to configure bash to execute clear command every time I type some command in the terminal (before executing my command). How can I do that?

I'm using Debian Linux.

  • You find this thread useful. – Thor Aug 3 '12 at 21:48
  • 3
    Just curious: What's you application for this? Except when I'm debugging programs with a lot of output, I usually want to keep as much as possible on the screen to help me keep track of the context I'm working in. – Joe Aug 11 '12 at 0:05

Bash has a precommand hook. Sort of.

preexec () {
preexec_invoke_exec () {
    [ -n "$COMP_LINE" ] && return                     # do nothing if completing
    [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
    local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`; # obtain the command from the history, removing the history number at the beginning
    preexec "$this_command"
trap 'preexec_invoke_exec' DEBUG
  • Gilles: I pasted the above into a konsole terminal and the output from each command I subsequently entered got cleared before I could read it. Am I missing something? Also, what happens for this (and the other answer below) if I invoke a multi-line bash script where more than one line (or any line other than the last one) generates interesting output? – Joe Aug 10 '12 at 23:20
  • @Joe This should clear the screen after you press Enter, before executing the command. It works like this for me. The preexec hook is executed for each interactive command, it doesn't matter whether the command is a built-in or an external command or many commands. – Gilles 'SO- stop being evil' Aug 10 '12 at 23:27
  • 1
    @Joe you need to add this line bellow do nothing if complete [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return; the problem is that PROMPT_COMMAND is being run and also trapped after the actual command; Also Can some one tell me why local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`; is here? – Heavy Gray Jun 30 '13 at 2:25
  • @JamesAndino history -1 prints the last command line, with a history number in front. The sed filter strips off the history number. – Gilles 'SO- stop being evil' Jun 30 '13 at 9:02
  • @JamesAndino That made it work (and I see Giles added it to his answer.) – Joe Jul 1 '13 at 17:56
bind 'RETURN: "\e[1~clear; \e[4~\n"'

After that every time you press return instead of just writing \n it will move to the beginning of line, enter the text clear;, then move to the end and enter \n as it expected.

  • 2
    A newline causes the prompt to be printed anyways, so putting the clear in the prompt accomplishes the same thing without polluting the command history with clear commands. – jw013 Aug 3 '12 at 20:51
  • 2
    @jw013 The difference is that in my case clear is executed before the command and command output doesn't disappear. However in case with prompt it does. – rush Aug 3 '12 at 20:54
  • I wish there were some way to do this without having to modify the command line itself - I'm sure this would break in interesting ways on complex or multi-line commands but I can't find any better way to do this. – jw013 Aug 3 '12 at 21:22
  • @jw013, you are right, it does break for multi-lines.. It inserts clear; to the output for each extra \n. – Peter.O Aug 3 '12 at 23:39
  • this tip is very good thx!! I needed to bind some specific code to F2, but some times the line has some command I typed but not executed, so with this ex.: bind "\"\\eOQ\":\"\e[1~ls;#\\n\"" it will move to the beginning, type ls;# commenting what was there, and run the ls command properly! thx! – Aquarius Power Feb 1 '16 at 18:02

from a question I asked today (with credit to user @aecolley's answer) :

bind '"\C-m": "\C-l\C-j"'

The \C-m simulating the 'Enter' key, the \C-l simulating Ctrl+l as it's clear and the \C-j is "newline-and-indent", so the command is binding Enter key to Ctrl+l & Ctrl+j

that works on GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin14) and the other answers on this thread do not. also, this does not pollute history with 'clear' commands every other command.

  • 1
    note: if using over ssh, you must execute it in that environment or weirdness happens. – TRB Jan 24 '15 at 1:32
  • greate it works on my ubuntu 16.04 terminal, thanks but how does it work, could you provide a link or some documentation please? – Andrzej Rehmann Jul 6 '16 at 16:59

Consider only clearing when you want to

cb4() {
  preexec () {

This uses a hook called preexec, confirmed works with zsh too

Then any session you want to automatically clear before every command you run: cb4

If you are certain you always want to clear in every context

preexec () {

And a if you actually want to reset the terminal

replace the word clear with tput reset

tput is optional but speeds up the reset process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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