How to suppress (or customize) Mac Terminal shell prompt - macos

Currently in my Terminal, every shell prompt looks like ComputerName: FooDir UserName$. The UserName part simply wastes too much space out of my precious 80 columns. Is there a way to suppress it?

The prompt is defined by the environment variable PS1 which you can define in .bash_profile.
To edit it, open or create the (hidden) file .bash_profile:
nano .bash_profile
and add a line that says
export PS1=""
Between the quotation marks, you can insert what you would like as your terminal prompt. You can also use variables there:
\d – date
\t – time
\h – hostname
\# – command number
\u – username
\W – current directory (e.g.: Desktop)
\w – current directory path (e.g.: /Users/Admin/Desktop)
The default prompt for common Linux distributions would be \w $, which evaluates to ~ $ in your home directory or e.g. /Users $ somewhere else. There are also website (like this one) that can help you with building your prompt.
If you want to remove the UserName part, your choice would be \h: \w$.
Once you made your changes, save the file with Control+o, Return, Control+x.

Here's an excellent article with a full list of Variables and Colors:
Customize your Shell Command Prompt
For a simple, minimalistic prompt, you can try this. Add the following line to your .bash_profile or simply test it first by running it in your terminal:
export PS1="\[\033[0m\]\w\$ "
It'll look something like this:
Here's my Prompt (source), also very simple:
export PS1="\[\033[1;97m\]\u: \[\033[1;94m\]\w \[\033[1;97m\]\$\[\033[0m\] "

2019 onwards, MacOS default shell is Z Shell. To customize command prompt, add a file named .zshrc in user home and put following line that sets a PS1 environment variable with desired prompt format:
export PS1="[%n]%~> "
Open new terminal
This is result of following format expansion:
%n User name
%~ Current directory
See full list of available expansions here.

Your answer can be found right here:http://www.hypexr.org/bash_tutorial.php#vi at about the middle of the page. :)

Related

How to customize the shell prompt in the VS Code terminal on macOS

I'm trying to customize my integrated terminal shell prompt in vscode, and was successfully able to change the theme (so that I can see my current working directory and branch I'm on), however now I want to remove the first portion 'anhlucci#Anhs-MacBook-Pro'. How do I do that?
I use Ubuntu with bash, and I only add the following lines to the end of ~/.bashrc:
if [ "$TERM_PROGRAM" = "vscode" ]; then
PS1='\[\033[01;34m\]\w\[\033[00m\]\$ '
fi
I found that vscode sets TERM_PROGRAM environment variable, and then use it to modify PS1 only to vscode.
The command line prompt is not dictated by Visual Studio Code, but by bash. The prompt is dictated by the PS1 variable in bash. You can view it as follows:
echo "$PS1"
To give you an idea of how that works, this is how my prompt looks like:
[hongli#Leticia Projects]$
My $PS1 looks like this:
[\u#\h \W]\$
Things like \u and \h are formatters that are substituted with a specific value. \u is for the current username, \h is for the hostname.
I'm guessing your $PS1 contains something like \u#\h in the beginning. Remove that and reset the PS1 variable, for example like this:
PS1='[\W]\$ '
Finally, you need to persist this in your bash configuration file so that the next time you start your shell it will show that same prompt. The bash config file is typically ~/.bashrc or ~/.profile depending on the exact Linux distribution you use. Make sure you set $PS1 in there.

Editing ~/.bash_profile doesn't customize command-line prompt properly

Related Issue: How to change the terminal prompt to just current directory?
I have added export PS1="\w\$ " to my ~/.bash_profile, but the prompt for the command line just displays this:
\w$
It recognizes the backslash escape for the $ character, but not for the filepath. It does the same thing when I use a capital 'W'.
The problem was that my terminal is running zsh. I created ~/.zshrc and wrote the following code:
PROMPT="%/\$ "
I referred to this resource to find the appropriate characters for displaying the current file path: ZSH Prompt Expansion.

How to add time at which commands were executed to terminal on Mac? (change format)

The default format for my terminal line looks like this:
David-Mac-mini:~ david$
I'd love if there was a way for it to show by default as:
David-Mac-mini:~ david$ [Hh:Mm:Ss]
You have edit or create your .bashrc or .bash_profile profile file.
Add the command export PS1="\t"
You can add the following lines to customize your Terminal prompt:
\d – Current date
\t – Current time
\h – Host name
\# – Command number
\u – User name
\W – Current working directory (ie: Desktop/)
\w – Current working directory with full path (ie: /Users/Admin/Desktop/)

How to change shell prompt in Unix?

I want to disable the Unix shell prompt character ($, #, %) which usually we see in terminal. Is there any command or setting which can do this? I am using Solaris OS.
By shell prompt character I mean:
>$
>#
You need to adjust your PS1 environment variable in your .profile file.
I guess you could set it to "" to have it empty.
ex:
export PS1=""
EDIT: it can also be in your .bashrc file, or any other shell you are using.
You can get fancy and put the host name in there. But basically you change the PS1 environment variable:
export PS1=hello
You can add this command in your ~/.bashrc file. Or other startup file, if you use another shell.
I suggest first check the man pages for the shell (whatever is yours? echo $SHELL) under shell variables.
There are four types of prompt strings(PS) PS1, PS2, PS3, PS4, for your problem PS1 adjustment is sufficient.
To check the current settings: echo $PS1
To change: PS1="" for the current session, to make it permanent export it in your ~/.bashrc or ~/.profile.
To make it permanent for the user: export PS1="whatever special characters you want"
for more special characters and examples you can visit here "http://linuxconfig.org/bash-prompt-basics"

Modifying bash prompt

When I open my terminal in Mac OS X, the command prompt current reads:
James-MacBook:project1 sam$
project1 is the name of the current directory.
What I want is to display the full path instead of James-MacBook.
How do I achieve this?
Your current prompt appears to show the hostname and the basename of your current directory. That means that the bash prompt, PS1, is likely set to:
PS1='\h:\W\$ '
To get the full directory name, use \w in place of \W:
PS1='\h:\w\$ '
You can set this at the command prompt. To make it permanent, this command can go into ~/.bashrc or ~/.bash_profile or other depending on how your system is configured.
You can read more about the options for command prompts, for which there are many options, in the PROMPTING section of man bash. Regarding the \w and \W options mentioned above, man bash explains how they are used:
\wthe current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM
variable)
\W the basename of the current working directory, with $HOME abbreviated with a tilde
Easy. This is not an OS X thing, but a bash thing. Try this:
export PS1='$(pwd): '
Then if you want to make it permanent, just edit your .bash_profile:
nano ~/.bash_profile
And place that first command in there.

Resources