Browsing Tag

mac terminal


Syntax Highlighting in the Mac OS X Terminal

January 2, 2013

In your programming career, sooner or later you will come across the command line text editor. In the beginning it will look and feel wired, but with time you will get used to it. After a while you will get used to not having the IDE and fancy auto complete. You will even get used to the fact that the syntax is not highlighted, but you do not have to. As it turns out, you can 'activate' your terminals syntax highlighting features. This will help you a lot down the road and can be applied for Mac (yes, even servers, no I do not know about Windows.

So I created a simple program in python to see what my terminal will display now if I were to open the file via vi. Here is the file content:

import time

def main():
	print 'hello'
	x = 42


If I were to run the following command (assuming the file is named


The output:

syntax highlight off

We can do better than that. First of all navigate to your home folder. We are going to add some color highlighting to the ls function. This will make it easy to recognize file types in the command line. That is the folder that opens up when you open up the terminal. Now run the following command to create (or modify if exsiting) your .profile file.

vi .profile

If you have File Vault turned on, you might have to add sudo at the beginning of the command and type your password at the prompt. Once the file is open type 'i' to switch to vi insert mode. Then type the following into the file:

alias ls='ls -G'

After you are done typing hit the 'esc' key followed by 'wq' and enter. This will switch you out of insert mode, save (write) & quit. See An Extremely Quick and Simple Introduction to the Vi Text Editor for more information about vi. This command will add some syntax highlighting to your terminal ls function. So now your directory list will be colorful. To add the syntax highlight to vi we will need to do a little more work.

First, change the directory like so:

cd /usr/share/vim

Then open a file called vimrc using sudo. Like this:

sudo vi vimrc

This will take you back into the vi enviorment. Now you are going to go into insert mode again by pressing 'i' key. Add the following code after the line:

backspace = 2

The code to add:

set ai                  " auto indenting
set history=100         " keep 100 lines of history
set ruler               " show the cursor position
syntax on               " syntax highlighting
set hlsearch            " highlight the last searched term
filetype plugin on      " use the file type plugins

" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if ! exists("g:leave_my_cursor_position_alone") |
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif |
\ endif

Save and exit again, by pressing 'esc' and 'wq'. Restart your terminal session and everything should be all colorful. If you were to go back to view your vimrc file you should see something similar to this:

vim color configure

And out python file from before should look like this when you open it:

vim python color highlight

Much better.

Note that at any point you can disable this feature by removing the added line of code. You can also disable terminal syntax highlighting by typing in the following command from within vi:

 :syntax off