SilentDev
SilentDev

Reputation: 22777

How to make Mac Terminal prompt and command bold?

I want to make the prompt (and command, if possible) bold.

So if I do all these commands:

username$ ls
*lists a bunch of things*
username$ mkdir testdir
username$ ls
*lists a bunch of things*

I want the

username$ ls
username$ mkdir testdir
username$ ls

to all appear in bold, or at least the username$ to appear in bold. Simply because it is hard when I am scrolling through the terminal to know when a new command is executed and stuff.

Is this possible?

Thanks in advance.

Upvotes: 2

Views: 3170

Answers (1)

Siguza
Siguza

Reputation: 23910

The prompt part (username$) is controlled by the PS1 shell variable. You can have that variable changed by any init script like so:

export PS1="\[\e[1m\]$PS1\[\e[0m\]"

The 1 in there encodes "bold" and the 0 at the end is "reset". If you didn't put a reset in there, then the rest of the line would also be affected, but so would any text after it be, which is probably not what you're looking for.

You can add more styles and colours by separating codes with a semicolon like so:

export PS1="\[\e[1;5;31m\]$PS1\[\e[0m\]"

The basic codes can be enumerated with a simple one-liner:

for x in $(seq 0 255); do printf "\x1b[${x}m${x}\x1b[0m\n"; done

For more info, see ANSI escape codes.

The \[ and \] in there are not part of ANSI codes though, but are specific to PS1: they tell bash that the characters in between are non-printing, which it important e.g. to calculate line wrapping. See bash prompt escape sequences.

Upvotes: 6

Related Questions