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.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末函似,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子顿天,更是在濱河造成了極大的恐慌蔑担,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件啤握,死亡現(xiàn)場離奇詭異,居然都是意外死亡懂从,警方通過查閱死者的電腦和手機畜埋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門畴蒲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人模燥,你說我怎么就攤上這事∶春玻” “怎么了辽旋?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵檐迟,是天一觀的道長码耐。 經(jīng)常有香客問我,道長骚腥,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任廓块,我火速辦了婚禮契沫,結果婚禮上,老公的妹妹穿的比我還像新娘懈万。我一直安慰自己,他們只是感情好贷掖,可當我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布渴语。 她就那樣靜靜地躺著,像睡著了一般驾凶。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上窟哺,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天技肩,我揣著相機與錄音,去河邊找鬼虚婿。 笑死,一個胖子當著我的面吹牛至朗,可吹牛的內容都是我干的。 我是一名探鬼主播锹引,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼真仲!你這毒婦竟也來了?” 一聲冷哼從身側響起秸应,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤碑宴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后延柠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡贿条,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年整以,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片公黑。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡摄咆,死狀恐怖,靈堂內的尸體忽然破棺而出吭从,到底是詐尸還是另有隱情,我是刑警寧澤谱醇,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布鸭廷,位于F島的核電站熔吗,受9級特大地震影響辆床,放射性物質發(fā)生泄漏桅狠。R本人自食惡果不足惜轿秧,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一咨堤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧一喘,春花似錦驱还、人聲如沸凸克。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蚂维。三九已至戳粒,卻和暖如春虫啥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背涂籽。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工又活, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留苔咪,地道東北人柳骄。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像舔清,于是被迫代替她去往敵國和親曲初。 傳聞我的和親對象是個殘疾皇子体谒,可洞房花燭夜當晚...
    茶點故事閱讀 45,060評論 2 355

推薦閱讀更多精彩內容