12

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!

Meltemi
  • 6,807
  • 11
  • 30
  • 30

2 Answers2

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} "
Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
  • 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
  • @Gilles: [An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling .bash_profile instead of .bashrc. Other GUI terminal emulators may do the same, but most tend not to.](http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html) – Daniel Beck Oct 27 '10 at 18:22
  • When i press at an empty prompt, the new prompt is bold. Is there a fix for this? – Jay Oct 27 '10 at 18:31
  • @Jay: I'd have to research it. Is there a reason you're doing this, or just because it's possible? – Daniel Beck Oct 27 '10 at 18:41
  • 2
    I figured it out, put an ${OFF} at the front: PS1="${OFF}\u@\h:\w \$${BOLD}" – Jay Oct 27 '10 at 18:41
  • @Jay Aaah, of course. I'll edit my answer. Thanks! – Daniel Beck Oct 27 '10 at 18:44
  • @Daniel: Regarding `.bashrc` vs `.bash_profile`, the document you cite is mostly correct. (The one mistake I spotted was that it gives an example with `PATH` being set in `.bashrc`, but that should be `.bash_profile`.) Basically the only sane configuration is for `.bash_profile` to source `.bashrc`, as in the example given. See also [Difference between .bashrc and .bash_profile](http://superuser.com/questions/183870). – Gilles 'SO- stop being evil' Oct 27 '10 at 19:05
  • To answer your previous question, I do press at an empty prompt ever now and then. Sometimes gunk characters end-up in there somehow. Sometimes the prompt gets displayed at the end of the previous line instead of on its own line. I press enter to fix it. – Jay Oct 27 '10 at 19:09
  • 1
    @Daniel: There shouldn't be an underscore in `~/.bashrc`. – Dennis Williamson Oct 27 '10 at 19:27
  • There are still a few problems with this. Pressing the up arrow a few times causes the terminal to think the first 3 characters of the command are part of the prompt, and it won't let you remove them. Also, the list of matches you get when typing part of a file name then , gets printed in bold. – Jay Oct 27 '10 at 20:02
  • @Jay Resizing the window also colors the prompt like the command. It's quite easy to break this, actually -- mine and your second issue are when the shell (re)writes something without hitting PS1 or Enter. This is probably why this is a hacky approach as written in the answer, with a non-hacky alternative provided. But it's the best I could come up with, and I'm not seeing that many better answers (yet). Let's hope someone smarter than me can figure this out. – Daniel Beck Oct 27 '10 at 20:08
  • @Jay Could you try to reproduce the "first three characters" issue again? I think I fixed it, but my up-arrow is a different command anyway and I can't find where I changed it right now. – Daniel Beck Oct 27 '10 at 20:23
  • 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
  • @Jay not for me, must be something else. I even reset my `inputrc` so I get the default up-arrow behavior. Sorry. – Daniel Beck Oct 27 '10 at 20:44
  • what does the trap "hack" in the first example do exactly? I'm not much of a BASH expert (obviously). I can sorta surmise the turning on and off of BOLD codes w/in the 2 main prompts...but not sure what trap/debug mean here. – Meltemi Oct 28 '10 at 15:56
  • @Meltemi: "hack" means I'm using a debugging facility in a way it probably shouldn't be used. `trap` is a bash buildin, so I'll refer you to the [bash man page](http://www.cs.sunysb.edu/documentation/bash/bash.html), just search for `trap [-lp] [arg] [sigspec ...]` in there and you see what it does. Short version is: "If a sigspec is DEBUG, the command arg is executed after every simple command (see SHELL GRAMMAR above).". Means whenever a command was entered, we send the `OFF` format string to the [filename of the current TTY](http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x721.html). – Daniel Beck Oct 28 '10 at 17:42
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
stpasta
  • 21
  • 1
  • 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