__MagicPower
Do one thing and do it best!

导航

 

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:

nano .bashrc

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

nano .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.

TerminalFinally, 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.

 

 转自:http://osxdaily.com/2006/12/11/how-to-customize-your-terminal-prompt/
 
posted on 2015-12-10 14:09  __MagicPower  阅读(510)  评论(0编辑  收藏  举报