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?

L3viathan
  • 26,748
  • 2
  • 58
  • 81
4ae1e1
  • 7,228
  • 8
  • 44
  • 77

4 Answers4

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.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • 1
    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? – 4ae1e1 Jan 19 '13 at 18:37
  • 1
    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`? – 4ae1e1 Jan 19 '13 at 18:46
  • If `.bashrc` doesn't exist, you can create it. I will, however, edit my answer as suggested. – L3viathan Jan 20 '13 at 00:49
  • 2
    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. – 4ae1e1 Jan 20 '13 at 01:43
  • `.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`. – L3viathan Jan 20 '13 at 14:11
  • 3
    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. – 4ae1e1 Jan 20 '13 at 18:40
  • 1
    I put mine in ~/.profile on OS X 10.8 and it works fine. – mrKelley Apr 20 '14 at 17:36
  • 1
    I'm using a Macbook Pro, and for me `Control` worked and `Command` didn't when saving the file. – Adam Zerner Jul 02 '14 at 21:16
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

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • 1
    Thanks. The question was from more than one year ago. Now I use [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh) for themes (prompt and more) — personally, I use the `gallois` theme. (Check out [my dotfiles](https://github.com/ZhimingWang/dotfiles) for more information.) – 4ae1e1 Apr 18 '14 at 03:52
  • @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. – Sheharyar Apr 18 '14 at 04:01
  • 2
    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)! – 4ae1e1 Apr 18 '14 at 04:17
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.

Parag Kutarekar
  • 975
  • 11
  • 10
1

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

  • 9
    Your answer would be more helpful if you described the solution here. – kukido Jan 08 '14 at 23:08
  • The link given is misleading. The appropriate link would be http://www.hypexr.org/bash_tutorial.php#cmd_prompt. Don't create link-only answers, instead summarize the content in case the link rots and breaks. – the Tin Man Mar 21 '16 at 17:55