Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
You find this thread useful. –  Thor Aug 3 '12 at 21:48
2  
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

2 Answers 2

up vote 14 down vote accepted

Bash has a precommand hook. Sort of.

preexec () {
  clear
}
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
share|improve this answer
    
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 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? –  James Andino 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 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.

share|improve this answer
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
1  
@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

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.