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 MacVim.app to your Applications folder.
- Open the Terminal (commonly located in /Applications/Utilities/Terminal.app, 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:
- Copy the
mvimbinary 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:
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
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 else au InsertEnter * hi StatusLine ctermfg=235 ctermbg=2 au InsertLeave * hi StatusLine ctermbg=240 ctermfg=12 endif endif " 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/.ycm_extra_conf.py" 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 https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim > ~/.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 https://github.com/scrooloose/syntastic.git
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 https://github.com/fholgado/minibufexpl.vim.git
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 https://github.com/Valloric/YouCompleteMe.git
git submodule update --init --recursive
Note: You might need to install Homebrew to install YouCompleteMe. To install it, type:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
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/.ycm_extra_conf.py. You can simply copy my .ymc_extra_conf.py in this folder. To do so, simply type:
curl http://urinieto.com/drop/.ycm_extra_conf.py > ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py
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: http://www.vim.org/scripts/script.php?script_id=213 and download the latest
cvim.zip. 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:
My User Macros look like this:
§ User Macros
SetMacro( 'AUTHOR', 'Oriol Nieto' )
SetMacro( 'AUTHORREF', '' )
SetMacro( 'COMPANY', 'New York University' )
SetMacro( 'COPYRIGHT', '' )
SetMacro( 'EMAIL', 'firstname.lastname@example.org' )
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:
Please, let me know if you have any questions. I hope you enjoy your new IDE!