Pages

Setting Up the Terminal in OSX

I use the Terminal in Mac OSX every day for my work and there are some small hacks that make the experience of using and coding in the Terminal much more bearable. In particular, enabling colors, some key aliases, and syntax highlighting are essentials for me on every Unix/Linux machine I use.

Colors and Aliases

Colors and aliases require a simple addition to the .bash_profile in your home directory. First, you need to make sure that you have the GNU CoreUtils installed. If you are not yet using Homebrew for Mac OSX, you should be. In the Terminal, run "brew install coreutils" to install GNU CoreUtils. (I may do a post in the future about Homebrew and the Unix/Linux Terminal in the future, but the basics are ubiquitous on the web).

You can copy-paste the code below. (For those who are new to the Terminal, use "ls -a" to show hidden directories and files, i.e. those files and directories with a leading "." character in their name like .bash_profile). The first section enables colors, which should subsequently be modifiable under Terminal > Settings > Profile by messing around with the ANSI Colors section for the appropriate terminal profile. The "Show ANSI Colors" option must also be enabled on this page.

# Terminal colors (after installing GNU coreutils)
NM="\[\033[0;38m\]"
HI="\[\033[0;37m\]" 
HII="\[\033[0;31m\]" 
SI="\[\033[0;33m\]" 
IN="\[\033[0m\]"

export PS1="$NM[ $HI\u $HII\h $SI\w$NM ] $IN"
export CLICOLORk=1
export LSCOLORS=ExFxBxDxCxegedabagacad

# Useful aliases
alias ls="ls -HFG"
alias cd..="cd .."
alias c="clear"
alias e="exit"
alias ssh="ssh -XY"
alias ..="cd .."

alias ssh_command="ssh -XY user@login.example.org"

The second section enables aliases: These are shortcuts for Terminal commands (which can often be quite long) that, when typed into the Terminal, execute the more complex command. The concept and name are intuitive. For example, with the aliases above "ls" gets substituted by "ls -HFG" every time I want to use "ls". I find this particularly useful for ssh commands since I need to log into a lot of computational mainframes and remembering all their IP addresses is unreliable.

Syntax Highlighting in Nano

I like to use nano as my default text editor in the Terminal: It is simple to use and quite frankly I have not gotten around to memorizing all those emacs and vim shortcuts. One of the drawbacks to nano compared to these other editors is that it has no code syntax highlighting. As this StackExchange article shows, it is possible to add it using Homebrew.

In the Terminal, install an updated version of nano using Homebrew. This creates /usr/local/share/nano which contains a number of common syntax files. We need to create a .nanorc file in the home directory which configures nano to include all of the available syntax highlights. The StackExchange article provides the relevant command; the code summary is reproduced below.

brew install nano
/bin/ls /usr/local/share/nano/*.nanorc | xargs -I {} echo 'include "{}"' >> ~/.nanorc
source ~/.nanorc