Fresh Start with MacVim

jjjjjjjjjjjjjjjjjkk

Posted by Grego on January 15, 2015

After talking with a fellow vim enthusiast today I rekindled my desire to be in a real vim environment again. After all, the only reason I lost interest was because of same nasty issues I had with PHP syntax highlighting, which I haven’t been using a lot lately anyway. I still appreciate SublimeText, however, which will probably take over as my PHP/web solution until I work things out with PHP+vim.

my vim set up

I decided to wipe my polluted vim config and start fresh.

Go download MacVim if you want the GUI version. In case you haven’t already, I recommend creating a git repository for your config. It’s great for all of your command line stuff:

mkdir -p ~/dotfiles/vim
git init
git add .
git commit -m "initial commit"
ln -s ~/dotfiles/vim ~/.vim

Now that we got that out of the way, time to copy over our vimrc example. I’m on OS X so mine is in /usr/share/vim/vim73/vimrc_example.vim.

cp /usr/share/vim/vim73/vimrc_example.vim  ~/dotfiles/vimrc
ln -s ~/dotfiles/vimrc ~/.vimrc

Installing VIM Plugins

To install plugins for vim, I was previously using Vundle. I’ll use it again this time, unless I find something better along the way.

Install Vundle

Following the vundle setup guide:

 git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Then add to your vimrc:

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

If you’re already in vim you’ll have to refresh the vimrc file, you can do this by using:

:so %

If you’re not in vim, open it! Then type :PluginInstall to install.

Next thing we do is grab a colorscheme, cause this MacVim default black on white junk is killing my eyes. I know I like the famous obsidian color scheme, so I’m gonna search for it using vundle. From vim:

:PluginSearch obsidian

A split window appears magically displaying my themes, and a keymap to install is detailed across the top of the screen. Press i to install. Then yy to yank the line, and switch back to your vimrc buffer, which is probably on the right side C-w l and add the line under the other Plugin lines, to make sure that it gets loaded every time you restart vim.

installing a colorscheme on macvim

And I chose obsidian2.vim cause we all know 2 is better than 1. (Actually I have no idea what I’m talking about). I tried it out by typing :colorscheme obsidian2 and it just worked. So I’ll add that to my vimrc as well.

Installing toggle_comment to toggle comments

I know there are a few vim plugins that toggle comments, I’m not sure which one is the best. I know in the past I’ve used NERDCommenter, so this time I’m trying something new: toggle_comment. I hope it’s good.

You can just add:

Plugin 'toggle_comment'

To your plugins section of .vimrc

Adding swift support

Not sure how good this is going to be but I’m going to add swift syntax support to vim using a plugin I found on GitHub:

Plugin 'Keithbsmiley/swift.vim'

Changing the font

Next thing I’m gonna do is change this hideous font. Add to vimrc:

set gfn=monaco:h15

Then

:so %

It might look a little like comic sans, but we’ll get to that at a later date.

Refreshing vimrc when saving the file

We have only added a couple of things so far, but you can already see how annoying this will get tiring to reload vimrc every time we make a change. Go ahead and add this, too:

au! BufWritePost .vimrc so %
au! BufWritePost .gvimrc so %

This automatically executes the source (so) command on % (the currently loaded file, i.e., your vimrc) every time you save the vimrc files.

Finish Installing Command-T (Compile it!)

When we added some of the default config from the Vundle installation instructions, we added a line that installs the Command-T vim plugin. However, this plugin still needs its supporting files to be compiled. From the command line:

  cd ~/.vim/bundle/command-t/ruby/command-t
  ruby extconf.rb
  make

If you’re having issues with this, you might want to check the Command-T Documentation.

Next, set up a shortcut key to invoke :CommandT from vim. I added this in my .gvimrc in order to be able to use the ⌘F key combination. If you come from Sublime Text, you could also set it to ⌘P for consistency.

if has("gui_macvim")
    macmenu &Edit.Find.Find\.\.\. key=<nop>
    "use cmd-f as cmd-t
    map <D-f> :CommandT<CR>
endif

You can see here I also disable the default ⌘F (which is under the Edit > Find > Find… menu). If you wanted to use ⌘P instead, you can ignore the macmenu part.

Set up default tab/space settings

Next, I’ll set up my default tab settings. I am always getting these mixed up, but there are tons of resources on google if you search for vim tab settings, and you can check out Indenting source code for more information as well.

"default tab settings
set sw=4
set softtabstop=4
set expandtab

These are my “sensible defaults.” sw stands for shiftwidth, softtabstop is for setting how many spaces to add/remove when I hit Tab or Backspace and expandtab replaces tab keystrokes with spaces (uses softtabstop). I will set up per-filetype settings later to override this to only two spaces for certain programming languages.

And since we’re now using spaces, we want to be able to distinguish a tab character from a space in our code, so we can use listchars to achieve that:

"and invisible characters to distinguish tabs and spaces...
set list
set listchars=tab:>-,trail:·

This sets up tabs to appear as >- and trailing spaces will show as a raised dot ·.

Then I added some nice basic keymaps:

"some nice basic keymaps

"duplicate line cmd-d
map <D-d> yyp

" cmd + number for switching tabs
map <D-1> 1gt
map <D-2> 2gt
map <D-3> 3gt
map <D-4> 4gt
map <D-5> 5gt
map <D-6> 6gt
map <D-7> 7gt
map <D-8> 8gt
map <D-9> 9gt
map <D-0> 0gt

"toggle_comment to use CMD-/
map <D-/> q

These probably encompass the functionality I use the most. ⌘D duplicates the current line and using the ⌘ + [number] key combination switches to that tab number.

There were some extra tweaks I didn’t tell you about in my .gvimrc file. They look like this:

set cursorline
hi cursorline guibg=#444444
set gfn=monaco:h15

" for MacVim & Command-T
if has("gui_macvim")
    macmenu &Edit.Find.Find\.\.\. key=<nop>
    "use cmd-f as cmd-t
    map <D-f> :CommandT<CR>
endif

That’s a huge chunk for today and I’ll be back more in the future to set up some more things, and probably replace some of these solutions out with alternatives even :)