First preexec_invoke_exec
has to be modified to prevent multiple executions of preexec
. Also, modify preexec
to take in account the actual number of lines in $PS1
:
preexec () {
# delete old prompt; one "\e[1A\e[K" per line of $PS1
for (( i=0, l=$(echo -e $PS1 | wc -l) ; i < l ; i++ ))
do
echo -ne "\e[1A\e[K"
done
# replacement for prompt
echo -ne "\e[31;1m$ \e[0m"
echo -n "$1"
echo -ne " \e[37;2m["
echo -n "$2"
echo -e "]\e[0m"
}
preexec_invoke_exec () {
[ -n "$DONTCLEANPROMPT" ] && return
DONTCLEANPROMPT=x
[ -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"`;
local this_pwd=`pwd`
preexec "$this_command" "$this_pwd"
}
trap 'preexec_invoke_exec' DEBUG
PROMPT_COMMAND='unset DONTCLEANPROMPT'
In order for preexec
to be run again, DONTCLEANPROMPT
has to be either unset or set to ''
. This is done with PROMPT_COMMAND
, which is run just before the primary prompt is issued. Therefore preexec
will be run once and only once for every command line.
These strange escape sequences are color-changing commands.
The completion code runs the following command to list available commands:
git help -a|egrep '^ [a-zA-Z0-9]'
The output of git help -a
looks like this:
add grep remote
add--interactive hash-object remote-ext
am help remote-fd
…
fsck-objects receive-pack write-tree
gc reflog
get-tar-commit-id relink
If grep is configured to print the matching part of the line in color, then command names that are in the first column will have their first letter highlighted:
$ git help -a|egrep --color=always '^ [a-zA-Z0-9]' | cat -v | head -n 1
^[[01;31m^[[K a^[[m^[[Kdd grep remote
When bash sees this output, it thinks that ^[[01;31m^[[K
, a^[[m^[[Kdd
, grep
and remote
are possible commands. The first one won't turn up, the last two are correct, the second one is mangled.
You need to configure grep not to use colors when its output is not on a terminal. If you've aliased egrep
to egrep --color=always
(and ditto for grep
and fgrep
), change that to --color=auto
. If you've set the GREP_OPTIONS
variable somewhere, change --color=always
to --color=auto
there.
Best Answer
Here's a way. It assumes you have the default key bindings.
The
\C-m
intercepts the 'Enter' key, the\C-l
(Ctrl+L
) executes the Bashclear-screen
function, and the\C-j
executes the Bashnewline-and-indent
function; so the command is binding Enter key to Ctrl+L & Ctrl+J