8
14/5/2014 Prompt Color | Taylor McGann's Blog http://blog.taylormcgann.com/tag/prompt-color/ 1/8 TAG ARCHIVES: PROMPT COLOR Customize Your Shell & Command Prompt Posted on 2012 June 13 As mentioned in a previous post, we received some new MacBooks and a Mac Mini at work. Since most of my team prefers using PCs, I was able to get my hands on one. I immediately noticed how different it was from the one I use at home, so I started customizing it right away. I found I had forgotten how to do a couple things and it took me longer than I would have liked to search the web, so I’ve decided to dedicate a short post on how to customize your shell and command prompt in Mac OS X. If you use Linux or Windows (think cygwin or git bash) this may apply to you too. If you don’t use any sort of shell, well, then you might just want this for future reference. Apps Terminal is the default app that comes with Mac OS X. Another great app is iTerm 2 (Free). It adds a lot of functionality that some users find lacking in Terminal. General Preferences Some programs that run the shell allow you to set the window size and buffer (essentially scrolling inside the limitations of the window). This is really helpful to setup before hand since lines that are too long will word wrap if you don’t have a large window buffer. This will inevitably happen at some point and it’s really annoying when it does, so take steps to prevent it now. If you find you navigate to a specific directory every time you open the shell, it may be a good idea to tell the app to navigate to that directory when you open the shell. In Terminal, this can be found at Preferences >> Settings >> Shell ; in iTerm 2 this can be found at Preferences >> Profiles >> General . There are a lot of other cool features (like window groupings) that you Taylor McGann's Blog A record of tech tips & tricks I stumble upon daily…and then some.

Prompt Color _ Taylor McGann's Blog

Embed Size (px)

Citation preview

Page 1: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 1/8

TAG ARCHIVES: PRO MPT CO LO R

Customize Your Shell &Command PromptPosted on 2012 June 13

As mentioned in a previous post, we received some new MacBooks and a Mac Mini at work.Since most of my team prefers using PCs, I was able to get my hands on one. I immediatelynoticed how different it was from the one I use at home, so I started customizing it rightaway. I found I had forgotten how to do a couple things and it took me longer than I wouldhave liked to search the web, so I’ve decided to dedicate a short post on how to customizeyour shell and command prompt in Mac OS X.

If you use Linux or Windows (think cygwin or git bash) this may apply to you too. If you don’tuse any sort of shell, well, then you might just want this for future reference.

Apps

Terminal is the default app that comes with Mac OS X. Another great app is iTerm 2 (Free). Itadds a lot of functionality that some users find lacking in Terminal.

General Preferences

Some programs that run the shell allow you to set the window size and buffer (essentiallyscrolling inside the limitations of the window). This is really helpful to setup before hand sincelines that are too long will word wrap if you don’t have a large window buffer. This willinevitably happen at some point and it’s really annoying when it does, so take steps toprevent it now.

If you find you navigate to a specific directory every time you open the shell, it may be a goodidea to tell the app to navigate to that directory when you open the shell. In Terminal, this canbe found at Preferences >> Settings >> Shell; in iTerm 2 this can be found at Preferences >>Profiles >> General. There are a lot of other cool features (like window groupings) that you

Taylor McGann's BlogA record of tech tips & tricks I stumble upon daily…and then some.

Page 2: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 2/8

should checkout.

Appearance

The next thing you’re going to want to do is customize your shell’s color. I like the traditionalblack background with white or light gray text and some colorful highlighting like green oreven just a plain grey.

For my shell’s font, I like to use Monaco 10pt. Smaller text let’s me see more on the screensince I usually only let my shell take up one half of the screen. I enable bold fonts and brightcolors for bold fonts, but I disable anti-aliasing (smooth edges) because I like that raw hackerfeel ;).

.profile, .bash_profile or .bashrc

Some of the most important customization takes place in the .profile file. Every time your shellloads, it will run the commands found in the “profiles.” There are a number profiles somesystem-wide (e.g. /etc/profile), others personal (e.g. .profile). The bash man page providesuseful information about the differences under the “INVOCATION” section:

Then at the bottom of the man page:

When bash is invoked as an interactive login shell, or as a non‐interactive shell with the ‐‐login option, it first reads and executescommands from the file /etc/profile, if that file exists. Afterreading that file, it looks for ~/.bash_profile, ~/.bash_login, and~/.profile, in that order, and reads and executes commands from thefirst one that exists and is readable. The ‐‐noprofile option may beused when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from thefile ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bashreads and executes commands from /etc/bash.bashrc and ~/.bashrc, ifthese files exist. This may be inhibited by using the ‐‐norc option.The ‐‐rcfile file option will force bash to read and execute commandsfrom file instead of /etc/bash.bashrc and ~/.bashrc.

FILES       /bin/bash              The bash executable       /etc/profile              The systemwide initialization file, executed for login shells       /etc/bash.bashrc              The systemwide per‐interactive‐shell startup file       /etc/bash.bash.logout              The systemwide login shell cleanup file, executed when a login shell exits

Page 3: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 3/8

In essence, .bash_profile is read upon login and .bashrc is read for each new shell openedsince you can have multiple shell sessions running at once without logging in again. In MacOS X, the .bash_profile overrides the .profile. I’ve run into some problems with .profile in thepast, so I’ve actually switched everything to .bash_profile.

The next four sections will discuss:

How to change prompt escapes (bash)How to change prompt color (bash)How to create your personal “bin”How to create aliases

Change Prompt Escapes

First, I like to customize the prompte e. I can’t stand it when the prompt is white and blendsin with the rest of the text in the shell. The appearance of the prompt is stored in theenvironment variable $PS1. Try typing echo $PS1 in your shell. The text you see is a stringcoded with the display setting for your shell’s prompt. It might look something like this:

\h:\W \u$

In this example, the \h represents the host computer, \W the working directory and \u thecurrent user. All this information makes sense if you were to use the CLI a lot. “Back in theold days,” people would interface between various servers or computers over a network (esp.in business scenarios). When you’d change to a different server, you’d want to know thehost computer you were accessing. Not all the computers had GUIs. Thus \h would let youknow which computer you were on; whether you were on yours or another.

The \u is common for similar reasons. Sometimes you use the su command to substituteuser and you’ll want to know which user you are acting as.

The \W should be self explanatory. You don’t want to have to type pwd or ls all the time toknow where you are at in the file hierarchy.

In my prompt, I’ve gotten rid of the host symbol (I don’t switch hosts often and when I do,the other prompt is usually different enough that I can tell I’m on a different machine) and

       ~/.bash_profile              The personal initialization file, executed for login shells       ~/.bashrc              The individual per‐interactive‐shell startup file       ~/.bash_logout              The individual login shell cleanup file, executed when a login shell exits       ~/.inputrc              Individual readline initialization file

Page 4: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 4/8

replaced it with the history number prompt escape (\!). This escape let’s you know whichnumber in the command history you have just typed. That way if you see a previouscommand that you’d like to repeat a couple lines up you just type !<number>. To view yourcomplete command history, type the command history. A simplified version of my promptlooks like this:

\! \u:\W$

Here’s a comprehensive list of prompt escapes to add to your prompt:

Change Prompt Color

To color code your prompt on a Mac, use the following template:

\a         # an ASCII bell character (07)\d         # the date in "Weekday Month Date" format (e.g., "Tue May 26")\D{format} # the format is passed to strftime(3) and the result           # is inserted into the prompt string an empty format           # results in a locale‐specific time representation.           # The braces are required\e         # an ASCII escape character (033)\h         # the hostname up to the first '.'\H         # the hostname\j         # the number of jobs currently managed by the shell\l         # the basename of the shell's terminal device name\n         # newline\r         # carriage return\s         # the name of the shell, the basename of $0 (the portion following           #   the final slash)\t         # the current time in 24‐hour HH:MM:SS format\T         # the current time in 12‐hour HH:MM:SS format\@         # the current time in 12‐hour am/pm format\A         # the current time in 24‐hour HH:MM format\u         # the username of the current user\v         # the version of bash (e.g., 2.00)\V         # the release of bash, version + patch level (e.g., 2.00.0)\w         # the current working directory, with $HOME abbreviated with a tilde\W         # the basename of the current working directory, with $HOME           # abbreviated with a tilde\!         # the history number of this command\#         # the command number of this command\$         # if the effective UID is 0, a #, otherwise a $\nnn       # the character corresponding to the octal number nnn\\         # a backslash\[         # begin a sequence of non‐printing characters, which could be used           #   to embed a terminal control sequence into the prompt\]         # end a sequence of non‐printing characters

Page 5: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 5/8

\[\033[COLOR_CODE_HERE\]PROMPT_ESCAPE_OR_TEXT_HERE\[\033[0m\]

Most Linux distributions use a little different format:

\e[COLOR_CODE PROMPT_ESCAPE\e[0m

The first portion before the desired prompt escape or text only begins painting the chosencolor (e.g., \[\033[1;34m\]). To stop painting a color, you have to reset to another coloror turn color off (e.g., \[\033[0m\]).

Here's a comprehensive list of color encoding:

# Regular Colors\[\033[0;30m\] # Black\[\033[0;31m\] # Red\[\033[0;32m\] # Green\[\033[0;33m\] # Yellow\[\033[0;34m\] # Blue\[\033[0;35m\] # Purple\[\033[0;36m\] # Cyan\[\033[0;37m\] # White

# High Intensty\[\033[0;90m\] # Black\[\033[0;91m\] # Red\[\033[0;92m\] # Green\[\033[0;93m\] # Yellow\[\033[0;94m\] # Blue\[\033[0;95m\] # Purple\[\033[0;96m\] # Cyan\[\033[0;97m\] # White

# Background\[\033[40m\] # Black\[\033[41m\] # Red\[\033[42m\] # Green\[\033[43m\] # Yellow\[\033[44m\] # Blue\[\033[45m\] # Purple\[\033[46m\] # Cyan\[\033[47m\] # White

# High Intensty backgrounds\[\033[0;100m\] # Black\[\033[0;101m\] # Red\[\033[0;102m\] # Green

Page 6: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 6/8

\[\033[0;103m\] # Yellow\[\033[0;104m\] # Blue\[\033[10;95m\] # Purple\[\033[0;106m\] # Cyan\[\033[0;107m\] # White

#Replace any leading leading 0; with 1; for bold colors#Replace any leading 0; with 4; to underline

Once you've decided on the appropriate prompt add export PS1="<custom prompt>"to your .profile. For example, this is what the line in my .profile looks like:

Add Personal "bin" to the PATH Variable

Every now and again you may want to create your own custom commands, scripts orprograms for the CLI. Instead of mixing these in with the rest of the OS's, just create yourown personal "bin" folder and add it to your PATH variable so that you can run thosecommands from any folder in the shell.

export PATH=$PATH:/Users/Taylor/bin

Create & Use Aliases

Aliases are really nifty. They can save you a lot of extra effort for frequently used and/orlengthy commands. For example, I found that I liked to use ls ‐lhaG a lot more than justls as follows:

alias ls='ls ‐lhaG'

Alias long commands that you'd forget or never want to type. I use Git to version my code.The git log command is very powerful and can include a lot of options. Instead of typingthe various options every time, I use an alias called glg:

Conclusion

At the end of the day, this is what my .bash_profile looks like:

UPDATED 2014-04-02

export PS1="\[\033[1;34m\]\!\[\033[0m\] \[\033[1;35m\]\u\[\033[0m\]:\[\033[1;35m\]\W\[\033[0m\]$ "

alias glg='git log ‐‐date‐order ‐‐all ‐‐graph ‐‐format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'

Page 7: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 7/8

##################### MY ALIASES #####################

# git command autocompletion scriptsource ~/bin/git‐completion.bash

# git commamands simplifiedalias gst='git status'alias gco='git checkout'alias gci='git commit'alias grb='git rebase'alias gbr='git branch'alias gad='git add ‐A'alias gpl='git pull'alias gpu='git push'alias glg='git log ‐‐date‐order ‐‐all ‐‐graph ‐‐format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'alias glg2='git log ‐‐date‐order ‐‐all ‐‐graph ‐‐name‐status ‐‐format="%C(green)%H%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"'

# ls alias for color‐modealias lh='ls ‐lhaG'

# lock computeralias lock='/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession ‐suspend'

# hibernation and sleep settingsalias hibernate='sudo pmset ‐a hibernatemode 25'alias sleep='sudo pmset ‐a hibernatemode 0'alias safesleep='sudo pmset ‐a hibernatemode 3'alias smartsleep='sudo pmset ‐a hibernatemode 2'

# up 'n' foldersalias ..='cd ..'alias ...='cd ../..'alias ....='cd ../../..'alias .....='cd ../../../..'

# simple ipalias ip='ifconfig | grep "inet " | grep ‐v 127.0.0.1 | cut ‐d\ ‐f2'# more detailsalias ip1="ifconfig ‐a | perl ‐nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'"# external ipalias ip2="curl ‐s http://www.showmyip.com/simple/ | awk '{print $1}'"

# grep with coloralias grep='grep ‐‐color=auto'

# proxy tunnel#alias proxy='ssh ‐D XXXX ‐p XXXX USER@DOMAIN'# ssh home

Page 8: Prompt Color _ Taylor McGann's Blog

14/5/2014 Prompt Color | Taylor McGann's Blog

http://blog.taylormcgann.com/tag/prompt-color/ 8/8

Proudly powered by WordPress

Share:

Like this: Like

Be the first to like this.

What have you done to customize your shell or change your command prompt?

Posted in Technology | Tagged Bash, Bash Profile, Bashrc, CLI, Command LineInterface, Command Prompt, Cygwin, iTerm, Linux, Mac OS X, Prompt Color,Prompt Escape, Shell, Terminal | 12 Replies

#alias sshome='ssh ‐p XXXX USER@DOMAIN'

# processes#alias ps='ps ‐ax'

# refresh shellalias reload='source ~/.bash_profile'

################################## ENVIRONMENTAL VARIABLES ##################################

# Add homebrew sbin to PATH variableexport PATH=$PATH:/usr/local/sbin

# Add personal bin to PATH variableexport PATH=$PATH:/Users/Taylor/bin    # May be redundant; check ~/.bash_profile, /etc/profile, /etc/paths, /etc/bashrc

# Show dirty state in prompt when in Git reposexport GIT_PS1_SHOWDIRTYSTATE=1

# Change promptPS1_OLD=${PS1}export PS1='\[\033[1;34m\]\!\[\033[0m\] \[\033[1;35m\]\u\[\033[0m\]:\[\033[1;35m\]\W\[\033[0m\] \[\033[1;92m\]$(__git_ps1 "(%s)")\[\033[0m\]$ '

Email Pocket Twitter 5 Facebook 1 Google LinkedIn