Make OS X Terminal commands I type BOLD
Solution 1
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} "
Solution 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:
- The ability to delete my bash prompt entirely by just pressing space then backspace
-
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
Meltemi
Updated on September 17, 2022Comments
-
Meltemi over 1 year
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!
-
Gilles 'SO- stop being evil' over 13 yearsNot
~/.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. -
HikeMike over 13 yearsThanks, will edit my answer. I have no
bashrc
, so I used what was there. -
HikeMike over 13 years
-
Jay over 13 yearsWhen i press <Enter> at an empty prompt, the new prompt is bold. Is there a fix for this?
-
HikeMike over 13 years@Jay: I'd have to research it. Is there a reason you're doing this, or just because it's possible?
-
Jay over 13 yearsI figured it out, put an ${OFF} at the front: PS1="${OFF}\u@\h:\w \$${BOLD}"
-
HikeMike over 13 years@Jay Aaah, of course. I'll edit my answer. Thanks!
-
Gilles 'SO- stop being evil' over 13 years@Daniel: Regarding
.bashrc
vs.bash_profile
, the document you cite is mostly correct. (The one mistake I spotted was that it gives an example withPATH
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. -
Jay over 13 yearsTo answer your previous question, I do press <Enter> 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.
-
Dennis Williamson over 13 years@Daniel: There shouldn't be an underscore in
~/.bashrc
. -
Jay over 13 yearsThere 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 <Tab>, gets printed in bold.
-
HikeMike over 13 years@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.
-
HikeMike over 13 years@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.
-
Jay over 13 yearsIf 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.
-
HikeMike over 13 years@Jay not for me, must be something else. I even reset my
inputrc
so I get the default up-arrow behavior. Sorry. -
Meltemi over 13 yearswhat 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.
-
HikeMike over 13 years@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, just search fortrap [-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 theOFF
format string to the filename of the current TTY. -
DrZoo about 8 yearsNice job! Just a few minor differences, but I'm glad you discovered a way for this to work with El Captain.