Can I configure bash to execute "clear" before every command typed in the console?
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.
Related videos on Youtube
Julian
Updated on September 18, 2022Comments
-
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 over 13 yearstitle in CAPS -> gimmeh-teh-code question
-
jschmier over 13 yearssimilar to previously asked question - stackoverflow.com/questions/4471896/…
-
Thor almost 12 yearsYou find this thread useful.
-
gokhan acar over 11 yearsJust 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 over 13 yearsIn
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 over 13 yearswhat about the second program with buffer overflow, do i use fgets instead of gets(str2) ?
-
unquiet mind over 13 years@Julian Yes - you should never, ever use gets().
-
jw013 almost 12 yearsA 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 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 almost 12 yearsI 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 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 over 11 yearsGilles: 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' 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 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 whylocal this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`;
is here? -
gokhan acar almost 11 yearsJust for completeness, trap '' debug turns this thing off again (two single quotes).
-
TRB over 9 yearsnote: if using over ssh, you must execute it in that environment or weirdness happens.
-
mikeserv over 9 yearsisn't $PS4 a fairly effective preexec hook in any shell?
-
Aquarius Power over 8 yearsthis 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, typels;#
commenting what was there, and run thels
command properly! thx!