How to suppress (or customize) Mac Terminal shell prompt
Asked Answered
E

4

43

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?

Ezra answered 19/1, 2013 at 16:21 Comment(1)
Also see: apple.stackexchange.com/a/224151/54395Alkalinity
S
71

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.

Slaw answered 19/1, 2013 at 16:52 Comment(8)
Thanks for help. But I can't find .bashrc on my machine. I've heard a lot about it before, like changing $PATH with it, etc., but it never existed. And creating it wouldn't help—I created it, loggout out and back in, but nothing changed. Maybe there is another file in control on OS X 10.8?Ezra
I managed to succeed by creating .bash_profile in user directory. Thank you for the information on $PS1. Maybe you would like to edit your answer and include .bash_profile?Ezra
If .bashrc doesn't exist, you can create it. I will, however, edit my answer as suggested.Slaw
Actually what I said is that creating .bashsc had no effect, but when I tried to create .bash_profile with the same content, it worked as suggested.Ezra
.bashsc? I assume that was a typo? Anyways, as I don't have a Mac I can't confirm this, so I'm just going to do as you said and replace all occurences of .bashrc with .bash_profile.Slaw
Yeah, sorry that was a typo... OS X is somewhat different from Linux you know. Most annoyingly, every major release of OS X itself is somewhat different in handling these kinds of stuffs :( They are enhancing accessibility for dummies and as a result, they are hiding a lot of things to prevent dummies from playing around with.Ezra
I put mine in ~/.profile on OS X 10.8 and it works fine.Boggs
I'm using a Macbook Pro, and for me Control worked and Command didn't when saving the file.Ergotism
H
14

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:

Simple Terminal Prompt

Here's my Prompt (source), also very simple:

export PS1="\[\033[1;97m\]\u: \[\033[1;94m\]\w \[\033[1;97m\]\$\[\033[0m\] "

enter image description here

Headsman answered 18/4, 2014 at 0:0 Comment(3)
Thanks. The question was from more than one year ago. Now I use oh-my-zsh for themes (prompt and more) — personally, I use the gallois theme. (Check out my dotfiles for more information.)Ezra
@KevinSayHi Yeah, I posted it here so it could be helpful to others as well. Also, very nice - I was thinking of switching to zsh myself.Headsman
Z Shell is really nice. Definitely give it try. There's no way back once you've made the switch (just like the Windows to OS X switch)!Ezra
N
2

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 Command prompt snapshot

This is result of following format expansion:

  • %n User name
  • %~ Current directory

See full list of available expansions here.

Nica answered 22/9, 2020 at 3:42 Comment(0)
N
1

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

Navvy answered 8/1, 2014 at 22:47 Comment(2)
Your answer would be more helpful if you described the solution here.Cowbind
The link given is misleading. The appropriate link would be hypexr.org/bash_tutorial.php#cmd_prompt. Don't create link-only answers, instead summarize the content in case the link rots and breaks.Lingo

© 2022 - 2024 — McMap. All rights reserved.