Wednesday, 14 January 2015

How to change Mac Terminal shell prompt


A quick fix is run this in your mac terminal:

PS1="->"

Your prompt will change to "->"

It will back to normal if you open a new terminal.

The permanent solution is put this change into your profile.

To do this you can run below commands: (in any directory)


vi ~/.bash_profile 

This command will open your profile by vi, and you use down arrow of your keyboard to move to the end of this file and then click "o" key on your keyboard, that will allow you to install and edit in a new line.

Then paste or type this:

export PS1="->"

Then click "esc" key on your keyboard.

Then type "w" and then type "q" on your keyboard.

After that click "enter" key. That will bring you back to the shell.

Later terminals will have your new prompt. (I mean if you open a new terminal)

However if you want to apply your changes to current terminal, you should run:

. ~/.bash_profile 

Note : above command has a "." at the beginning,  and a space after it. They are important don't forget them.

There are so many parameters you can play with:

The \u tells bash to insert the name of the current user into the prompt string. If your user name is alice, your prompt string will be "alice—>". If you are a C shell user and, like many such people, are used to having a history number in your prompt string, bash can do this similarly to the C shell: if the sequence \! is used in the prompt string, it will substitute the history number. Thus, if you define your prompt string to be:
PS1="\u \!--> "
then your prompts will be like alice 1—>, alice 2—>, and so on.

But perhaps the most useful way to set up your prompt string is so that it always contains your current directory. This way, you needn't type pwd to remember where you are. Here's how:
PS1="\w--> "


Table of prompt customizations that are available :
\a  The ASCII bell character (007)
\A  The current time in 24-hour HH:MM format
\d  The date in "Weekday Month Day" format
\D {format} The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation; the braces are required
\e  The ASCII escape character (033)
\H  The hostname
\h  The hostname up to the first "."
\j  The number of jobs currently managed by the shell
\l  The basename of the shell's terminal device name
\n  A carriage return and line feed 
\r  A carriage return
\s  The name of the shell
\T  The current time in 12-hour HH:MM:SS format
\t  The current time in HH:MM:SS format
\@  The current time in 12-hour a.m./p.m. format
\u  The username of the current user
\v  The version of bash (e.g., 2.00)
\V  The release of bash; the version and patchlevel (e.g., 2.00.0)
\w  The current working directory
\W  The basename of the current working directory
\#  The command number of the current command
\!  The history number of the current command
\$  If the effective UID is 0, print a #, otherwise print a $
\nnn    Character code in octal
\\  Print a backslash
\[  Begin a sequence of non-printing characters, such as terminal control sequences





What I recommend to use is: PS1="\W:"


http://stackoverflow.com/questions/14416274/how-to-suppress-or-customize-mac-terminal-shell-prompt







zsh:


https://scriptingosx.com/2019/07/moving-to-zsh-06-customizing-the-zsh-prompt/
http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

I am doing:
PROMPT='%*-%B%F{240}%1~%f%b %# '


No comments:

Post a comment