Can I configure bash to execute "clear" before every command typed in the console?
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
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.
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.