Thursday, January 17, 2008

Bash and Vim on OS X 1.5


2008-01-23: I have updated this post with better information. Pardon me not updating the screenshots to show the red highlighting against the directories. I recently got a MacBook Pro. Yes I like it; no I don't think it is better than a Gnu/Linux system; they are both good in different ways. One immediate problem I had was that, in bash, the ls command didn't highlight files, directories or links with different colors. The quick solution to this is to alias your ls command to be ls -G. This works unless you are like me and want a lovely dark background for the terminal. They use blue for the directory color and I can't see the blue against black. I want it to be red. Of course it is just a matter of knowing what config files to tweak... Here is a boring post about getting that done.

In order to change this behavior you need to have a local preferences file for bash. You might already have one. I didn't have a .bash_profile .profile or .bashrc so I created a file called .profile that contained the following.

export TERM=xterm-color
export CLICOLOR=true
export LSCOLORS=bxfxcxdxbxegedabagacad
#alias ls='ls -G'

To understand what that means look here for a full discussion. It is probably also documented in the man pages somewhere. To get the highlighting to take effect without restarting your terminal just type 'source .profile' without the quotes.

Now what about the prompt itself? Wouldn't it be nice if that worked like an ubuntu prompt? I like how ubuntu gives you your current position in the directory structure as part of the title of the terminal window. With a little more magic we can make this happen as well. Add this to your .profile to see what I mean:

TITLEBAR='\[\e]0;\u@\h:\w \a\]'
export PS1=${TITLEBAR}'\u@\H:\W\[\e[37;1m\]$ \[\e[0m\]'
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
#export DISPLAY=:0.0

If you want to know what all that garbage means you can read the man pages or refer to these nice tutorials here and here.

My other hangup was getting vim to work properly. I wanted syntax highlighting, line numbers, auto indentation and other bells and whistles. No problem, once again it is just a matter of knowing the config files. I created a .vimrc in my home directory and put the following into it.


set nu " Line numbers on
set nowrap " Line wrapping off
" Formatting (some of these are for coding in C and C++)
set ts=2 " Tabs are 2 spaces
set bs=2 " Backspace over everything in insert mode
set shiftwidth=2 " Tabs under smart indent
set autoindent
set smarttab

" Visual
set showmatch " Show matching brackets.
set mat=5 " Bracket blinking.
set background=dark
syntax on

Now things are much better. Be advised that all these settings are for dark terminal backgrounds. I use the "homebrew" theme for terminal. Make sure you set it as your default or subsequent tabs will open up with some other theme.

References:
  • http://www.macosxhints.com/article.php?story=20031025162727485
  • http://www.funtoo.org/en/articles/linux/tips/prompt/
  • http://www.macworld.com/article/50257/2006/04/bashprompt.html
  • http://forums.macrumors.com/showthread.php?t=171291
  • http://systhread.net/texts/200703bashish.php
  • http://biodegradablegeek.com/2007/12/13/using-vim-as-a-complete-ruby-on-rails-ide/

5 comments:

KetanPadegaonkar said...
This comment has been removed by the author.
Shlomo said...

Clarification. The .profile file I show in the post defines lots of colors in a nice, human readable way. Then down at the bottom you see the custom highlighting definition.

PS1="${BRIGHT_CYAN}[${CYAN}\u$@\h${WHITE}:\w${BRIGHT_CYAN}]${NORMAL}\$ ${RESET}"

Turns out this is completely unnecessary. I thought since I used a dark background the default color scheme wouldn't be good, but it is so you could just add the alias to your .profile and nothing else.

alias ls='ls -G'

That is what mine looks like now. :)

Shlomo said...

Another great customization is having a custom prompt. Here is a great tutorial on that http://www.funtoo.org/en/articles/linux/tips/prompt/

Shlomo said...

So if you read my comments you will notice that I didn't know what the PS1 definition was really doing. It is all about your prompt, not the highlighting of other stuff.

Shlomo said...

Ok. Given I really know what I am talking about now, I am going to ammend the original post.