Archive for the ‘programming’ Category

Vim (or MacVim) as a C/C++ IDE

September 1st, 2014 2 comments

This post is meant to be a little guide to setting up Vim (or MacVim) as your next favorite C/C++ IDE. Vim is probably the most powerful text editor that humans have ever created, so you should consider using it even if its learning curve is a bit steep.


First, let’s install MacVim. MacVim adds a graphical interface to Vim that can help newcomers (only for OSX users, others should consider GVim). Skip the next steps if you want to use the regular Vim instead.

  • Get the latest snapshot of MacVim from here (make sure you have the correct OSX version).
  • Decompress it (double click) and drag to your Applications folder.
  • Open the Terminal (commonly located in /Applications/Utilities/, but I suggest you put it in your Dock) and go to the MacVim Snapshot folder you just downloaded. In my case, I had to type the following:
    cd ~/Downloads/MacVim-snapshot-73/
  • Copy the mvim binary to your PATH so that you can open MacVim from any directory in your computer. Like this (you will have to enter your machine password):
    sudo cp mvim /usr/bin
  • Now you should be able to start a new document with MacVim by typing:
    mvim hello.c
    (Type :w to quit).


Now that you have Vim (and/or MacVim) installed in your system, the next thing you should do is to set it up to adapt it to your needs. The rest of the instructions work for Vim and MacVim, so I will use Vim to refer to both (simply replace vim for mvim if you want to use MacVim). Vim is highly configurable, but here I will describe what I believe is the most useful setup for new users and C/C++ developers.

The Vim (or MacVim) configuration file is located in ~/.vimrc. Therefore, type vim ~/.vimrc to start editing your configuration. You might see a blank file if you have never edited Vim’s configuration before. You can copy and paste the following. There’s a comment for the most important commands, so that you can understand what these lines do if you are interested.

" Enables highlight syntax
syntax enable
set nofoldenable

" Sweet colorscheme
" colorscheme codeschool
set background=dark

" Set utf8 as standard encoding and en_US as the standard language
set encoding=utf-8

"" Display line numbers on the left
set number

"" Use mouse (only for resizing!)
set mouse=a

" Set the focus to the correct screen (ok, no more mouse thingies)
set mousefocus

" No more annoying sounds
set visualbell

" Do not scroll sideways unless we reach the end of the screen
set sidescrolloff=0

" highlight the status bar when in insert mode
if version >= 700
    if has("gui_running")
        au InsertEnter * hi StatusLine guifg=black guibg=green
        au InsertLeave * hi StatusLine guibg=black guifg=grey
        au InsertEnter * hi StatusLine ctermfg=235 ctermbg=2
        au InsertLeave * hi StatusLine ctermbg=240 ctermfg=12

" Infere the case-sensitivity
set infercase

" Need to set this flag on in order to have many cool features on
set nocompatible

" Indent properly based on the current file
filetype indent plugin on
filetype plugin on

" Pathogen load
"filetype off " Makes syntax non-working on office box
call pathogen#infect()
call pathogen#helptags()

" Switch between files in buffer
nnoremap <C-Tab> :bn<CR>
nnoremap <C-S-Tab> :bp<CR>

" Change default fontsize to fit MacBook Pro 13'
set guifont=Monaco:h11

" Don't select first Omni-completion option
set completeopt=longest,menuone
"set completeopt=menuone,longest,preview

set tabstop=4     " a tab is four spaces
set backspace=indent,eol,start
                  " allow backspacing over everything in insert mode
set autoindent    " always set autoindenting on
set copyindent    " copy the previous indentation on autoindenting
set shiftwidth=4  " number of spaces to use for autoindenting
set shiftround    " use multiple of shiftwidth when indenting with '<' and '>'
set incsearch     " show search matches as you type
set expandtab
set shiftwidth=4
set softtabstop=4

" Always set the current file directory as the local current directory
autocmd BufEnter * silent! lcd %:p:h

" Enable folding
nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR>
vnoremap <Space> zf

set history=1000         " remember more commands and search history
set undolevels=1000      " use many levels of undo

" Tabs in command line mode behave like bash
set wildmode=longest,list,full
set wildmenu

" Highlight the entire word when searching for it
set hlsearch

"====[ Make tabs, trailing whitespace, and non-breaking spaces visible ]======
exec "set listchars=tab:\uBB\uBB,trail:\uB7,nbsp:~"
set list

" Move line by line even when the line is wrapped
map j gj
map k gk

" Persistent undo
set undofile                " Save undo's after file closes
set undodir=$HOME/.vim/undo " where to save undo histories
set undolevels=1000         " How many undos
set undoreload=10000        " number of lines to save for undo

" YouCompleteMe
let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/"
nnoremap <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>


Now it’s time to install some plugins. We will start with Pathogen, a script to make the installation of other scripts extremely easy. To install Pathogen, just type:

mkdir -p ~/.vim/autoload ~/.vim/bundle
curl > ~/.vim/autoload/pathogen.vim

Using Pathogen, we will install Syntastic, a wonderful plugin to identify errors in your code (linting) in the most popular programming languages. To install it, just type:

cd ~/.vim/bundle && \
git clone

Now restart Vim and type :Helptags. Linting should be working now for C/C++.

The next plugin to install is MiniBufExplorer, which will make our life easier when dealing with multiple files in Vim. To install it, simply type:

cd ~/.vim/bundle && \
git clone

We also want to have some auto complete options when developing our C/C++ programs. The best plugin I’ve found is called YouCompleteMe, which is compatible with basically all the most popular programming languages to date. Type the following to install it:

cd ~/.vim/bundle && \
git clone
cd YouCompleteMe
git submodule update --init --recursive
./ --clang-completer

Note: You might need to install Homebrew to install YouCompleteMe. To install it, type:

ruby -e "$(curl -fsSL"

You will need to do some additional setup in order to get the plugin working. As you may see in the second to last line of the .vimrc above, we set up the default configuration file in ~/.vim/bundle/YouCompleteMe/ You can simply copy my in this folder. To do so, simply type:

curl > ~/.vim/bundle/YouCompleteMe/

Finally, the last plugin to install is the C Plugin, which will help us when navigating through C/C++ files, debugging, etc. This plugin does not support Pathogen installation, so we will have to do it manually. Go to the official plugin page: and download the latest Copy the zip file to your ~/.vim/ folder and type:


Before we are done, you should configure the template file with your name and affiliation. To do so, edit the template file and edit the user macros, like this:

vim ~/.vim/c-support/templates/Templates

My User Macros look like this:

§ ==========================================================
§ User Macros
§ ==========================================================

SetMacro( 'AUTHOR', 'Oriol Nieto' )
SetMacro( 'AUTHORREF', '' )
SetMacro( 'COMPANY', 'New York University' )
SetMacro( 'COPYRIGHT', '' )
SetMacro( 'EMAIL', '' )
SetMacro( 'LICENSE', '' )
SetMacro( 'ORGANIZATION','New York University' )

If you’ve done everything correctly, you will be able to create a new .c file (e.g. vim hello.c and a nice template will be included in the top of the file. Moreover, you should be able to open multiple files (e.g. :e otherfile.c) and they will appear in the MiniBufferExplorer. And finally, if you make some errors while coding, Syntastic will tell you. As an example, see the following screenshot:

MacVim with the following plugins: Syntastic, YouCompleteMe, C.vim, and MiniBufExplorer

MacVim with the following plugins: Syntastic, YouCompleteMe, C.vim, and MiniBufExplorer

Please, let me know if you have any questions. I hope you enjoy your new IDE!

Categories: programming Tags:

Install Python 2.7.x under Snow Leopard / Lion (64 bits)

February 13th, 2012 5 comments

OS X comes with a Python installation that I find a bit confusing and misleading. What I present in this post is a guide to remove the default Python from your 64 bits OS X (i.e. Snow Leopard or Lion), install its latest version, and set up a nice environment to work with (e.g. iPython + MacVim). The only requirement for this process is to have the latest version of XCode, that you can get from the App Store.

Uninstall Python

I couldn’t find any nice and clean uninstaller for Python in OS X. So we will have to remove all files by hand. Here is the code:

sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /Applications/Python*
sudo rm -rf /Library/Python*
cd /usr/local/bin
sudo rm -rf 2to3 idle idle2.7 pydoc pydoc2.7 python*
cd /usr/bin
sudo rm -rf 2to3 idle idle2.7 pydoc pydoc2.7 python*

(if you have another versions of Python installed (e.g. 2.6, 2.5), just change 2.7 for your version number when removing the files. However, that is not really that important, just so that you don’t have that much garbage in your computer).

Install Python 2.7.x

Now let’s install the latest release of 2.7.x for OS X. You can see the latest releases here. When writing this post, the latest version is 2.7.2.

Once you have run the installer, edit your ~/.bash_profile file. To edit your file, use your favorite text edito (vim in my case):

vim ~/.bash_profile

You should now have the following lines (add them if you don’t, but the installer is supposed to add them for you):

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
export PATH

Now let’s add the compiler flags for 64-bits and the Python Path for the modules. This is important, otherwise your easy_install will probably fail. Add the following lines to your ~/.bash_profile:

export PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
export CFLAGS="-I/Developer/SDKs/MacOSX10.7.sdk/usr/lib/"
export ARCHFLAGS="-arch x86_64"

(note: change MacOSX10.7 to MacOSX10.6 if you’re using Snow Leopard in the second line)

Notice that all your modules will be installed here:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. It’s kind of important to remember this path, so you may want to add it to your sidebar in Finder or create some soft link.

Close your file and restart your bash so that the new environment variables are available. Check that you can now run python from the new terminal.

Install most common modules

I usually use easy_install to install new modules. You can get it from here (the module is called setuptools).

Download it, decompress it, and type the following from it’s folder:

python build
python install

Now you can use easy_install to install your favorite modules. E.g.:

easy_install numpy
easy_install scipy
easy_install matplotlib
easy_install ipython

If you’re running Lion, I highly recommend the SciPy SuperPack, which will do all the work for you.

Setting up your programming environment

This last section is completely biased due to my eternal love to VIM. I know some heartless people who use Emacs, or even Eclipse (I was one of them, and it was a terrible thing!) to code in Python.

If you’re a VIM lover you should already know about MacVim, but if you don’t you should definitively get it.

In any case, you should choose your favorite text editor (in case you want to use VIM -everybody wants to use VIM, but unfortunately some people don’t know that-, here it is a nice python module), and then I highly recommend using iPython instead of the classic Python console. This is especially useful for debugging and plotting. iPython is life changing, really.

To run iPython with matplotlib already running in the background just type:

ipython --pylab

Finally, here is a nice screenshot of how my programming environment looks like (iPython + PyLab + MacVim):

Beautiful isn’t it? : )

And finally, for something -not that- completely different: Happy programming!

Categories: programming, python Tags:

Open Terminal Tab in the same Directory in OSX

November 17th, 2011 No comments

I tend to work with multiple tabs in my terminals, keep opening, closing them, and so it is very useful to me to open a new terminal in my current folder instead of going to the home.

I’ve found this nice script that will do the work for you:

osascript -e 'tell application "Terminal"' \
-e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
-e "do script with command \"cd `pwd`;clear\" in selected tab of the front window" \
-e 'end tell' &> /dev/null

If you c&p this in a new file called “nt” in your /usr/local/bin, and you change it’s permissions to be executable ( chmod +x /usr/local/bin/nt ), all you have to do to open a new tab in your current directory will be:

$> nt


Categories: osx, programming Tags:

Compile and use PortAudio on OSX 10.7 (Lion)

November 10th, 2011 No comments

UPDATE: Version v19_20111121 doesn’t compile on my Lion, but version v19_20110326 does. I’ve updated this document accordingly.

UPDATE: Apparently, this also works on Snow Leopard. Not only that, but it might be the only way to use it under Snow Leopard.

It is frustrating to download the latest stable version of PortAudio, try to compile it on the new version of Mac OSX 10.7 (Lion), and get a bunch of errors.

However, for some reason, the previous stable release does work on 10.7.

Here the instructions (nothing fancy, just regular UNIX commands to compile and install):

  • Download the source file
  • Go to your directory and untar:
    $> tar zxvf pa_stable_v19_20110326.tgz
  • Configure:
    $> cd portaudio
    $> ./configure
  • Compile:
    $> make
  • Install:
    $> sudo make install

Now you should be able to go to the examples directory and compile and run the examples.

You must change the commas in #include "portaudio.h" for brackets: #include <portaudio.h>, so that the linker will look for the portaudio library inside your library path. To compile, for example paex_sine.c, you should type (inside the exmaples directory):

$> gcc -o paex_sine paex_sine.c -lportaudio

Then, you can just type ./paex_sine to listen to a 5 seconds sinewave.

If you are one of those who don’t like to install new libraries in your system path, you can always link with your libraries locally. In this case, you can leave the include with the commas. From the examples directory, you could compile some of these examples like this:

$> gcc -o paex_sine paex_sine.c -I../include/ -L../lib/.libs/ -lportaudio

In this case you will also need your DYLD_LIBRARY_PATH variable to point to your portaudio lib directory. Like this (it’s better to put the full path, I’m just putting the local path here as an example):

$> export DYLD_LIBRARY_PATH=../libs/.libs/

Now you can run it (from the examples directory): ./paex_sine

You’re all set.
Have fun coding with PortAudio!

Categories: programming Tags: