Home Open Source Classics Customizing Terminal

About

Open Source Classics is a directory and how-to guide for free and open source programs and resources useful to classicists, as well as a place for musings about the relationship of scholarship to free and open source principles. Continue the conversation at the Open Source Classics Forum and Technology Forum. Have a recommendation for an article? Interested in becoming a contributor? Email the editors.

Login

Customizing Terminal
Written by David Andrew Collier   
Saturday, 09 August 2008 08:02

We have already reviewed some basic terminal commands in my last post, but now I would like to talk about how to customize the terminal.  A very easy way to do this is to click on the Terminal window, and then Preferences.  Here you can do a number of things, like choose from pre-set "themes", or alter them to your own liking.


 

 

picture1

But why not use the terminal itself to set its environment to your liking? There are two important files that we can create which Bash (or our terminal shell) will run whenever it starts up:  .profile (or .bash_profile, .bash_login, whichever exists) and .bashrc. Actually, Bash will run one file or the other (either .profile or .bashrc), depending upon the situation.  But we will make this an inconsequential distinction by making one "call" the other.

Here is how my terminal screen appears at start-up:

picture4

 We see several differences from the normal terminal start-up screen.  First, there are various colors.  In addition to the 'Last Login' at the beginning, there are also 9 lines from Book 2 of the Aeneid, and a greeting.  I also have chosen to include some version information on Darwin and GNU Bash, as well as some last use and time info.  Finally, I have a custom prompt -- a green arrow symbol.  How did I accomplish all of this? Very easily, actually. Let's take a look at my .profile and .bashrc files.

The .profile reads like this:

#######Dave's Profile#######

####Call .bashrc####

source ~/.bashrc

###Welcome Screen###

echo -e "${COLOR_PURPLE}Ecce autem gemini a Tenedo tranquilla per alta
(horresco referens) immensis orbibus angues
incumbunt pelago pariterque ad litora tendunt;
pectora quorum inter fluctus arrecta iubaeque
sanguineae superant undas, pars cetera pontum
pone legit sinuatque immensa volumine terga.
fit sonitus spumante salo; iamque arva tenebant
ardentisque oculos suffecti sanguine et igni
sibila lambebant linguis vibrantibus ora.

Welcome to Darwin, Dave."
echo -e "${COLOR_GREEN}Kernel Information: "'uname -smr'
echo -e"${COLOR_RED}'bash --version'"
echo -e "${COLOR_YELLOW}Uptime: "'uptime'
echo -e"${COLOR_ORANGE}Server time is: "'date'${COLOR_NC}

(Followed by additional PATH modifications for MacPorts)

My .profile is fairly simple, and pertains almost exclusively to the welcome message.  Whenever my terminal opens a new bash shell, it processes this file according to its directions. First it calls the .bashrc file (where bulk of my custom settings will be found), and then it uses the echo command (with the -e option that handles escape characters) to produce my welcome message.  echo is a straighforward command that does just what it says: whatever <string> you enter after the command will be echoed back to the screen.  Let's have a look at my .bashrc file:

###.bashrc###

###COLOR SETTINGS###
export TERM=xterm-color
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
export CLICOLOR=1
alias ls='ls -G'
export COLOR_NC='\e[0m' # No Color
export COLOR_WHITE='\e[1;37m'
export COLOR_BLACK='\e[0;30m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_GRAY='\e[0;30m'
export COLOR_LIGHT_GRAY='\e[0;37m'
alias colorslist="set | egrep 'COLOR_\w*'"  ##command to list all the colors

###Custom Prompt###
export PS1="\[${COLOR_GREEN}\]\w -> \[${COLOR_NC}\]"

###Navigation Aliases###
alias ..='cd ..'
alias ~='cd ~'
alias portup='sudo port selfupdate'
alias la='ls -a'
alias cleanup='sudo periodic daily weekly monthly'
alias lastclean='ls -al /var/log/*.out'

###New bookmark command###
### While in directory, type save <name> to save directory as name. ###
### Then can 'cd <name> at any time to switch to that directory.    ###
### type <show> to list all the 'bookmarks'.                        ###

if [ ! -f ~/.dirs ]; then  # if doesn't exist, create it
        touch ~/.dirs
fi

alias show='cat ~/.dirs'
save (){
        command sed "/!$/d" ~/.dirs > ~/.dirs1; \mv ~/.dirs1 ~/.dirs; echo "$@"=\"`pwd`\" >> ~/.dirs; source ~/.dirs ;
}
source ~/.dirs  # Initialization for the above 'save' facility: source the .dirs file
shopt -s cdable_vars # set the bash option so that no '$' is required when using the above facility

Perhaps the easiest way to review the .bashrc is to tackle the code section by section and to explain what everything is doing.

We can start with the colors script. I borrowed this section of code (along with some other ideas for my .bashrc file) from this blog.  You'll learn early on that the terminal and other UNIX issues are treated broadly in a number of blogs and how-tos online.  If you don't have the level of fluency to write your own scripts, you can always borrow from someone else.  You'll notice two commands in the colors script -- export and alias -- and both of these commands will return frequently when customizing your terminal.

export is a command used to set environment variables, and so is one of our main commands in the .bashrc file, we are using export to define UNIX colors (the difficult to remember sequence of numbers and letters)