macfidelity

|

rethinking the think-different thing

Customizing Terminal in Mac OS X 10.5

July 2nd, 2008 at 21:23

If 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: , , , , , , , , , ,

15 Responses to “Customizing Terminal in Mac OS X 10.5”

  1. P Reinie Says:

    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?

    ReplyReply
  2. fidel Says:

    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

    ReplyReply
  3. fidel Says:

    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

    ReplyReply
  4. Daniel Craig Says:

    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

    ReplyReply
  5. fidel Says:

    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 :D

    Best regards
    fidel

    ReplyReply
  6. P Reinie Says:

    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.

    ReplyReply
  7. [Terminal] Pimp your Prompt | zHome Says:

    [...] Post zeigt ein paar Denkanstösse die etwa Macfidel oder OS X Daily geschrieben [...]

  8. fidel Says:

    More informations (german) about history-tweaks for your Terminal can be found here

    http://www.makomi.de/2009/06/10/bash-history-aufwerten/

    ReplyReply
  9. mac | Snow Leopard Terminal einrichten | macfidelity Says:

    [...] ä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 [...]

  10. A Collection of 250+ Terminal Commands, Hacks, Tips and Tuts for Mac OS X Says:

    [...] Customizing Terminal in Mac OS X 10.5 [...]

  11. links for 2010-01-30 « Object neo = neo Object Says:

    [...] 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) [...]

  12. Nate Says:

    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.

    ReplyReply
  13. fidel Says:

    @Nate
    true – but i was willing to show ‘this’ way ;)

    To be honest … i do use a simple .bash_profile myself

    Best Regards
    fidel

    ReplyReply
  14. P Reine Says:

    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.

    ReplyReply
  15. fidel Says:

    @P Reine
    thanks – always a pleasure to get feedback from your side.

    Best Regards
    fidel

    ReplyReply

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>