How to Customize your Terminal Prompt

Whether you use the Terminal occasionally or regularly, you might find it appropriate to change the way it looks beyond the color scheme and transparency settings. How about changing the actual command line prompt? It’s quite easy, and can be a fun way to further customize your Macs appearance.
Changing the way the terminal prompt looks is not too complicated, but it involves some fairly minor usage of the command line, which, given that you are wanting to customize how the bash prompt looks, we assume you’re at least somewhat familiar with the Terminal. And yes, this applies to all versions of OS X, as every version on every Mac defaults to using bash as the command prompt. OK let’s get started.

The default command line prompt in Mac OS X is something like:

ComputerName:CurrentDirectory User$

That would look something like this when the Terminal has been launched:

MacBook:~/Desktop Admin$

Not too bad, but kind of boring, and not the best, right? It’s quite easy to change though, and you can really customize your bash Terminal prompt to look like almost anything you want.

We’re going to assume you use the default bash shell, so you’ll be editing either the file .bashrc, .bash_profile, or .profile if you installed fink.

Thus, to start customizing the bash prompt, at your current Terminal prompt, type the following to load the appropriate profile into nano text editor:

vi ~/.bashrc

Yes, you can change that to .bash_profile or .profile depending on your specifics:

vi .bash_profile

You’ll probably be presented with a plain file, so type the following onto a line in the terminal to get started:

export PS1=" "

It’s between those quotation marks that your bash prompt customization occurs.
Between the quotation marks of export PS1=” “, 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/)

(Note if you just want to use a custom bash prompt one-off or to test out the appearance of changes before setting them in the bash profile, you can simply use the export commands, the change will take effect immediately with the export command but will be abandoned when that Terminal session is ended.)
So, let’s take a few examples. Perhaps you want your Terminal prompt to display the User, followed by the hostname, followed by the directory, then the appropriate .bashrc entry would be:

export PS1="\u@\h\w $ "

which will look like the following when rendered at the actual bash prompt:

Admin@MacBook~Desktop/ $

Cool huh? You can also change the prompt itself to anything, it doesn’t have to be the $ sign, simply replace it with whatever else you’d like to use, : for example would be:

export PS1="\u@\h\w: "

which is the same as above, but : instead of $

Admin@MacBook~Desktop/: "

So, play around and see what you prefer. My personal favorite is the following:

export PS1="\W @ \h [\u] $ "

This renders looking the currently active directory (PWD), the hostname of the computer, and the username of the current user, looking like the following:

/System @ MacBookPro [Paul] $

With modern versions of OS X, you can even include an Emoji in the prompt by dragging it into the place you’d like it to display, for example:

export PS1="\h:\W (DRAG EMOJI HERE) $ "

That would display as:

Hostname:Desktop (Emoji) $

Seen in this picture below:

A customized terminal prompt in Mac OS X

When you’re satisfied with the appearance of your prompt, save the .bash_profile file edits in nano by hitting Control+o and you can then exit out of the nano program by hitting Control+x
If you really want to, you can also just use a standard text editor like like TextWrangler or TextEdit to edit your .profile, but if you’re changing your terminal prompt you should likely learn how to modify files from the command line too.
If you’d rather change things to look more graphical, there’s also a simple how-to designed to make your Terminal prompt include an emoji character (yes, the same emoji icons that people use for text messaging), you can read that here if you are interested.
Terminal
Finally, keep in mind this is changing the command prompt, not the appearance of the Terminal app windows. If you like customizing the look of things, changing the appearance of Terminal is well worth doing, as you can add a huge number of changes and customizations to the way a standard Terminal window renders. Added together with a custom command prompt and the days of having a boring looking terminal will be long gone, on your Mac anyway. It’s probably obvious, but yes these prompt customizations work beyond OS X and in Unix and Linux as well.
Do you have a cool prompt you want to share? Post yours in the comments, try to include both the export command as well as what the prompt will render as, to make it easier for others to determine if they want to try it or not.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容