Can I configure bash to execute "clear" before every command typed in the console?

3,172

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
3,172

Related videos on Youtube

Julian
Author by

Julian

Updated on September 18, 2022

Comments

  • Julian
    Julian over 1 year

    I need to rewrite the function so that it is no longer vulnerable to stack buffer overflow.

    void hello (char *tag)
    {
       char inp [16];
    
       printf("enter value for %s:", tag);
       gets(inp);
       printf("hello your %s is %s\n", tag, inp);
    }
    

    Also, how do I rewrite the following other code to avoid buffer overflow:

    int main (int argc, char *argv[ ]) {
        int valid=FALSE;
        char str1[8];
        char str2[8];
    
        next_tag(str1);
        gets(str2);
        if (strncmp(str1,str2,8)==0)
            valid=TRUE;
        printf("buffer: str1(%s),str2(%s),valid(%d) \n", str1, str2, valid);
    }
    
    • Luca Martini
      Luca Martini over 13 years
      title in CAPS -> gimmeh-teh-code question
    • jschmier
      jschmier over 13 years
      similar to previously asked question - stackoverflow.com/questions/4471896/…
    • Thor
      Thor almost 12 years
      You find this thread useful.
    • gokhan acar
      gokhan acar over 11 years
      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.
  • Matteo Italia
    Matteo Italia over 13 years
    In fgets it's enough just to pass the size of the buffer (without the -1), since it "reads at most one less than the number of characters specified by n".
  • Julian
    Julian over 13 years
    what about the second program with buffer overflow, do i use fgets instead of gets(str2) ?
  • unquiet mind
    unquiet mind over 13 years
    @Julian Yes - you should never, ever use gets().
  • jw013
    jw013 almost 12 years
    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.
  • rush
    rush almost 12 years
    @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.
  • jw013
    jw013 almost 12 years
    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.
  • Peter.O
    Peter.O almost 12 years
    @jw013, you are right, it does break for multi-lines.. It inserts clear; to the output for each extra \n.
  • gokhan acar
    gokhan acar over 11 years
    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?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 11 years
    @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.
  • James Andino
    James Andino almost 11 years
    @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?
  • gokhan acar
    gokhan acar almost 11 years
    Just for completeness, trap '' debug turns this thing off again (two single quotes).
  • TRB
    TRB over 9 years
    note: if using over ssh, you must execute it in that environment or weirdness happens.
  • mikeserv
    mikeserv over 9 years
    isn't $PS4 a fairly effective preexec hook in any shell?
  • Aquarius Power
    Aquarius Power over 8 years
    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!