11

I'd like to make commands I've typed (input) into terminal stand out from all the output.

For example:

imac:~ buster$ chmod -R g-w myfolder
imac:~ buster$ cd myfolder
imac:myfolder buster$ ls -l
total 0
drwxr-xr-x 9 root admin 306 Apr 20 2010 bin
drwxr-xr-x 7 root admin 238 Apr 20 2010 include
drwxr-xr-x 73 root admin 2482 May 18 17:16 lib
drwxr-xr-x 6 root admin 204 Apr 20 2010 man
imac:myfolder buster$ echo Go Giants!
Go Giants!

bold jumps to mind but I'd accept a color or even highlighting the whole line...

I'm sure there's a way to do this but it's not obvious to me...

thanks!

9

Edit your ~/.bash_profile or ~/.bashrc (see Gilles' comment below) and add the following lines:

BOLD="\[\033[1m\]"
OFF="\[\033[m\]"
PS1="${OFF}\u@\h:\w \$${BOLD}"
PS2="> ${BOLD}"
trap 'echo -ne "${OFF}" > $(tty)' DEBUG

Move the ${BOLD} around to make part of the prompt also bold. If the prompt itself should not be colored, you need the ${OFF} prefix in PS1, otherwise empty lines (pressing enter without having something written) will make the following prompt bold (credits to @Jay, thanks again!)

This adds a debug trap to turn bold format off, so it's quite a hack. Credits (works without group tty on OS X though).

This is a bit of a hack, so use it at your own risk.

Only setting your PS1/PS2 prompts to bold would be easier and just as visible:

BOLD="\[\033[1m\]"
OFF="\[\033[m\]"
PS1="${BOLD}\u@\h:\w \$${OFF}"
PS2="${BOLD}>${OFF} "
  • 1
    Not ~/.bash_profile, but ~/.bashrc (or both, if you don't source .bashrc from .bash_profile). These settings need to be set for each interactive instance of bash, and .bash_profile is read only by login shells. – Gilles 'SO- stop being evil' Oct 27 '10 at 18:16
  • Thanks, will edit my answer. I have no bashrc, so I used what was there. – Daniel Beck Oct 27 '10 at 18:20
  • 2
    I figured it out, put an ${OFF} at the front: PS1="${OFF}\u@\h:\w \$${BOLD}" – Jay Oct 27 '10 at 18:41
  • 1
    @Daniel: There shouldn't be an underscore in ~/.bashrc. – Paused until further notice. Oct 27 '10 at 19:27
  • 1
    If I use the up arrow four times in a row, to go through the command history, then the first 3 characters of the old command becomes part of the prompt, and there is no way to remove them. I can't delete them with the backspace key. – Jay Oct 27 '10 at 20:40
2

I had a little trouble with the solutions here while using El Capitan (esp. in iTerm2 but in plain old Terminal as well). I got two sorts of errors:

  1. The ability to delete my bash prompt entirely by just pressing space then backspace
  2. Unwanted characters finding their way into my bash output, e.g. the input pwd would result in

    \[\]/Users/home/Directory
    

    or in

    \e[0m/Users/home/Directory
    

I propose the following solution, which is really just more of the same.

    BOLD="\033[1m"
    OFF="\033[m"
    PS1="${OFF}\u@\h :${BOLD}"
    PS2="> ${BOLD}"
    trap 'echo -ne "${OFF}" > $(tty)' DEBUG
  • Nice job! Just a few minor differences, but I'm glad you discovered a way for this to work with El Captain. – DrZoo Apr 12 '16 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.