sakurashinken
sakurashinken

Reputation: 4078

Color output of last command in terminal

When I am reading the output of a bash command in terminal on mac, I find it hard to locate where the output of the command starts. I would like to color the output of the latest command in red and and then when I run a new command, have only the output of that command be red while the output of previous commands is black.

Edit:

As an example,

echo hi
hi <- should be red

then when I enter another command

echo hi
hi <- should be black
echo 'hi there'
hi there <- should be red

Upvotes: 1

Views: 532

Answers (2)

Zombo
Zombo

Reputation: 1

Here is a shell function to do that:

xtrace() {
  awk '
  BEGIN {
    d = "\47"; printf "\33[36m"
    while (++q < ARGC) {
      x = split(ARGV[q], y, d); y[1]
      for (z in y) {
        printf "%s%s", !x || y[z] ~ "[^[:alnum:]%+,./:=@_-]" ? d y[z] d : y[z],
        z < x ? "\\" d : ""
      }
      printf q == ARGC - 1 ? "\33[m\n" : FS
    }
  }
  ' "$@"
  "$@"
}

Put this in your ~/.profile or similar, then run like this:

$ xtrace echo alfa 'bravo charlie'
echo alfa 'bravo charlie'
alfa bravo charlie

The command will be printed in blue, followed by the output of the command printed normally.

colored output

Source

Upvotes: 1

Ken Thomases
Ken Thomases

Reputation: 90681

It's not exactly what you asked, but Terminal has a feature called "Marks". By default, it automatically marks each command line in the window. The marks appear as faint square brackets ([, ]) in the margins of the window.

You can jump between marks using the items in the Edit > Navigate menu. It's most convenient to use the keyboard shortcuts for those menu items, such as ⌘↑ for Jump to Previous Mark. If you hold down the Shift key, that changes to Select to Previous Mark. These can make it easier to find the beginning of command output.

Upvotes: 1

Related Questions