Customizing Terminal in Mac OS X 10.5
July 2nd, 2008 at 21:23If you do use Terminal.app more then once a week it makes sense to customize your Terminal to your needs.
Personaly i prefer bash so the next steps are focused on the bash shell.
1. Creating a .bashrc:
- Open Terminal.app
- Open Terminal Preference Dialog
- Navigate to the Startup section
- Make sure you have selected “command (complete path)” under “Shell open with” section. The Full path should be “/bin/bash” in our example
- Close the Preference dialog
- Navigate to your users home folder if you aren’t there already. This is usualy done with: “cd ~”
- Enter: “touch .bashrc” to create a new empty file called .bashrc
Now we have done the first steps. We are sure we do use bash and we have an empty .bashrc ….so no function till now
2. Creating a .bash_profile:
Now we have to create a .bash_profile in our Users home directory too.
- Open Terminal.app (be sure that you are in your users home directory)
- Enter the following to create a bash profile: touch .bash_profile
- Open the new bash profile. Enter: vi .bash_profile
- Fill the new bash profile with the following:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
3. Forcing Terminal.app to load our bashrc at start:
Now we have to make sure Terminal.app uses our new .bashrc while starting.
- Open Terminal.app
- Open Terminal Preference Dialog
- Navigate to the Startup section
- Change “/bin/bash” to “/bin/bash –rcfile /Users/yourusername/.bashrc“
On the next start of Terminal.app it should use our empty .bashrc file. So now lets fill it a bit
4. Define an example alias:
- Open Terminal.app
- Open your .bashrc with a texteditor (ie: vi .bashrc)
- Insert the following example alias to check if our .bashrc works: alias ls=”ls -la”
- Save and close the file
- Restart Terminal.app
After relauching Terminal.app the “ls” command should output the same in terminal then “ls -la” does
5. Tweak the history of your shell:
I love my bash history, but why not tweaking its default behaviour ?
- Open Terminal.app
- Open your .bashrc with a texteditor (ie: vi .bashrc)
- Enter the following lines to your .bashrc:
# history handling
#
# Erase duplicates
export HISTCONTROL=erasedups
# resize history size
export HISTSIZE=5000
# append to bash_history if Terminal.app quits
shopt -s histappend
After restarting your Terminal your history should we teaked as follows.
- The first command deletes duplicate-commands out of your .bash_history file which contains your bash history.
- The second command defined the size of your history
- The third line appends the history to your .bash_history to make sure you have all sessions saved.
Well that was the basic first step. You could now continue adding colors to your .bashrc, configuring how the prompt will look like and many other nice things.
Here my final example .bashrc
# Define how Bash prompt looks like:
#
# User @ Host – working dir
#export PS1=”\u@\h\w$ ”
export PS1=”\u@\h\w: “# Cli Colors
export CLICOLOR=1
# use yellow for dir’s
export LSCOLORS=dxfxcxdxbxegedabagacad# Alias
#
#
alias ls=’ls -la’# history handling
#
# Erase duplicates
export HISTCONTROL=erasedups
# resize history size
export HISTSIZE=10000
# append to bash_history if Terminal.app quits
shopt -s histappend
Tags: .bashrc, .bash_history, /bin/bash, 10.5, bash, cli, color, colors, terminal, terminal alias, terminal color




September 4th, 2008 at 05:41
I followed all steps to create a .bashrc but it does not appear to be executing it. I even put in an “echo .bashrc Script ran” command in it, but that doesn’t print out to the terminal.
I changed the startup in preferences for Terminal to no avail.
This is 10.5 – if that matters.
I’ve programmed years in csh, but not bash. How different can this be? Oh yeah, it’s on a Mac!
Do you see anything I might be doing wrong?
P.S. I ran “source /Users/username/.bashrc” and that worked. Why isn’t it executing when I start a Terminal?
September 4th, 2008 at 08:23
Hi P Reinie,
strange, as it worked for me.
if you are following this article let me know ….. I’m gonna recheck that once again on a fresh & new account and add a new comment todays evening.
best regards
fidel
September 6th, 2008 at 14:43
Hi Reinie,
yeah you are correct.
The old post didn’t worked as i missed one point in the article (creating the .bash_profile)
Sorry about that. Thats the downside if you write tutorials out of your mind and didnt double-check it.
I edited & reformated the post and if you follow it now once again it should work like a charme.
Hope it helped
Best regards
fidel
September 25th, 2008 at 15:30
Thanks for this Fidel, i have just switched to a mac, this will make it much more familiar.
Just a note to others:
Change â ‚¬Å“/bin/bashâ ‚¬ to â ‚¬Å“/bin/bash â ‚¬ €œrcfile /Users/yourusername/.bashrcâ ‚¬Å“
This is two hyphens — before rcfile
Some may need to change the quotes (delete and hit shift+”) in the colouring section of Fidel’s example rc file
Cheers Fidel
Dan
September 25th, 2008 at 16:35
Hi Dan
it seems like i do have a problem with code-quoting in this blog-software
In the source it IS a double –
I seriously need to re-check how i can solve this issue in wordpress regarding code & not interpreting it.
Great to hear it helped you.
Feel free to ask questions if you have problems with your mac / first steps in mac world.
Its always a pleasure to help others and i can even use such problems to write them down here, to help others again
Best regards
fidel
October 2nd, 2008 at 20:30
Somewhere along the line, I did get the .bashrc to read in at startup. Now I forgot what it was.
As far as the hyphens, sometimes you just have to put in a note like “(that’s hyphen hyphen)”, or “(two hyphens)”, like DanC said or “double -” like Fidel said. Else, switch to a font like courier (something non-proportional?) or html’s SAMP tag (which didn’t work for me in this text). The screen sample in section 3 clearly shows the two hyphens.
January 9th, 2009 at 08:00
[...] Post zeigt ein paar Denkanstösse die etwa Macfidel oder OS X Daily geschrieben [...]
June 11th, 2009 at 12:21
More informations (german) about history-tweaks for your Terminal can be found here
http://www.makomi.de/2009/06/10/bash-history-aufwerten/
October 9th, 2009 at 18:31
[...] ähnlichen Beitrag gab es letztes Jahr unter dem Titel Customizing-terminal-in-mac-os-x-10.5 wobei ich diesmal noch eine alte Diskussion aus dem IRC aufnehmen will nämlich den großen [...]
November 11th, 2009 at 21:12
[...] Customizing Terminal in Mac OS X 10.5 [...]
January 31st, 2010 at 05:31
[...] Customizing Terminal in Mac OS X 10.5 | macfidelity Remove history duplicates, invoke .bashrc when new terminal window is created, etc. (tags: terminal apple customize tips bash macosx) [...]
March 22nd, 2010 at 17:43
Why not just create a .profile in your homedir and paste the code from your .bashrc? It should work the same as your .bashrc without all the extra steps to get .bashrc to run.
March 22nd, 2010 at 18:02
@Nate
true – but i was willing to show ‘this’ way
To be honest … i do use a simple .bash_profile myself
Best Regards
fidel
April 20th, 2010 at 04:39
There are some differences between doing defining things in one .rc file or another… such as the order in which they are executed (read) but mostly if they are executed (read) based on if it is an interactive Terminal.app session or an executed script file or a login and I forget the other differences, and what is carried over from an interactive session to an executed shell script.
In some cases, some of the .rc files are not automatically executed so if you expected a definition it might not be there.
It’s been a while since I actively worked in a unix environment, so I just don’t recall which one is used where and why.
April 20th, 2010 at 10:31
@P Reine
thanks – always a pleasure to get feedback from your side.
Best Regards
fidel