Enable Syntax Highlight and other options in vim on Mac OS

Vim is a popular command line text editor with an interface that is based not on menus or icons but on commands given in a text user interface.

Unfortunately the vim that ships with Mac OS X 10.5.* (Leopard) and above doesn’t have an optimized configuration file to allow for syntax highlighting, auto indentation, etc.

To fix this, open a new Terminal window and enter these commands to open vim’s configuration file:

cd /usr/share/vim
sudo vim vimrc

Press the “i” key to switch vim to Insertion Mode, then enter these lines below the “set backspace=2” line:

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

Press the Escape key to take vim out of Insertion Mode, then press “:” (colon) followed by “x” to save the file and exit. Enter this command again:

sudo vim vimrc

… and you’ll see that vim now automatically indents code, displays the cursor position, has syntax highlighting, etc.


Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s