Error message when starting terminal -- .zshrc linked

Viewed 27

I wanted my MacOS terminal to have a different color for outputs from the color on the command line. Like this, but not only for ls but for essentially every single output.

So I put this in my .zshrc:

preexec() { 
    tput setaf 15
    :
}
preexec_invoke_exec() {
    [ -n "$COMP_LINE" ] && return
    local this_command=`HISTTIMEFORMATE= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"`;
    preexec "$this_command"
}

trap 'preexec_invoke_exec' DEBUG

And it's worked beautifully (other than the annoying command line lag) for over a year, until just recently whenever I start Terminal I get this error:

preexec_invoke_exec:fc:3: no such event: 1
preexec_invoke_exec:fc:3: no such event: 1
preexec_invoke_exec:fc:3: no such event: 1
preexec_invoke_exec:fc:3: no such event: 1
preexec_invoke_exec:fc:3: no such event: 1
preexec_invoke_exec:fc:3: no such event: 1
(eval):1: bad pattern: ^[[97m#

Here's the actual image.

Not really sure what's wrong. According to some other posts, it may have something to do with $HISTORY not having a length, but nothing has been able to fix it so far and I there's not many similar posts. Anyone know what's going on here? Or maybe someone knows a more intuitive way to change the output color? Thanks in advance!

1 Answers

history 1 outputs the whole history, starting with event 1. In your case, you need only the last element of the history, which would be history -1.

To find the most recent command, I would do a

local h=( $(history -1) )
local this_command=$h[2,-1] # Strip the event number
[[ -n $this_command ]] && preexec $this_command

   
Related