Skip to content

How to use Vim for Erlang Development

by on September 9, 2013

vim editor logo

This post sponsored by ErlangCamp 2013 in Nashville which was epic!

You are about to learn to use Vim as your editor for Erlang development. You will learn how to install and use a variety of really powerful Vim plugins to make Erlang dev with Vim smooth and satisfying!

I have been developing Erlang now for about 13 years, many of them full time and even wrote a book on Erlang: Erlang & OTP in Action. I have loved every minute of it but there was always one thing that made me sad, probably makes you sad too – Emacs. Emacs is the de-facto editor for Erlang. The emacs mode included with the Erlang distro is quite wonderful. The fact still remains, Emacs, we do not like it. ctrl ~, ctrl x ctrl f etc… Nope!

Setting up Vim for Erlang

Let’s get started setting up Vim for Erlang development. The first thing we need to do is setup pathogen so that installing subsequent packages is really simple. The first thing to do is create the directory $HOME/.vim/autoload. Download pathogen.vim from here and place it into this directory. Now add the following 2 commands to your $HOME/.vimrc file.


call pathogen#infect()
call pathogen#helptags()

At this point pathogen will install and generate help documentation for any plugin you place into the $HOME/.vim/bundle directory – which you should of course create.

With this created now we are ready to start installing plugins to make your life easier. Try these on for size by cloning these git repos directly into the $HOME/.vim/bundle directory. They will simply work next time you start vim.

vimerl.vim Indenting, autocomplete and more for Erlang
ctrlp.vim ctrl p and open a powerful fuzzy file finder. Makes navigating file trees a thing of the past.
NERDTree Powerful file tree navigator right in vim – don’t use it much since I installed ctrlp though.
NERDTree Tabs Add the NERDTree file finder to all tabs you have open in vim.

Before we get into basics on how to use all these plugins to create Erlang magic I want to show you two bonus tricks I really love. First, get a better color scheme. To do this create the directory $HOME/.vim/colors and find yourself a slick color scheme to drop into it. I recommend vividchalk.vim by TPope.

Pro Tip
For dropbox or other file sync users keep all your vim installs in sync easily like so; take your .vim and your .vimrc and move them into your Dropbox directory. Then run:


ln -s ~/Dropbox/.vim ~/.vim
ln -s ~/Dropbox/.vimrc ~/.vimrc

Now all your machines vim installs will run just the same. If you have compatibility problems on any one, well then just skip this for that machine.

Ok, so now on to how to use these plugins for Erlang/Vim greatness.

How to Use our Vim Plugins for Erlang Dev

I am going to use the source for Erlware Commons as an example. So I clone it first and then change into the erlware_commons directory and run vim. Now lets say I know what file I want to update, specifically the “ec_date.erl” file. The first thing I do is type p and then start typing ec_date.erl.

                                                                                                                                                          
~                                                                               
[No Name] [TYPE= unix] [0/1 (100%)]                                             
> test/ec_dictionary_proper.erl
> src/ec_dictionary.erl
> src/ec_date.erl                                                               
 prt  path  ={ files }=  >> ec_da

You can see that as I start typing and get to “ec_da” ctrlp has already displayed a narrowed down list of files in the directory tree under where I have opened vim that match. The file on the bottom ec_date.erl is the one selected and so just pressing enter here will open it. If I wanted to select “test/ec_dictionary_proper.erl” then I could simply press the up arrow and select it or keep typing until it was the only selection.

Now, what if I don’t know what file I want to select? This is where NERDTree comes into play. Run :NERDTree and you will pop open the file browser. Like this:

  Press ? for help             |
                               |~                                               
.. (up a dir)                  |~                                               
<lang-projects/erlware_commons/|~                                               
▸ doc/                         |~                                               
▸ priv/                        |~                                               
▸ src/                         |~                                               
▸ test/                        |~                                               
  CONTRIBUTING.md              |~                                               
  COPYING                      |~                                               
  Makefile                     |~                                               
  README.md                    |~                                               
  rebar.config                 |~                                               
  rebar.config.script          |~                                               
~                              |~                                               
~                              |~                                               
~                              |~                                                                                                                                       

Here we can see the directory tree for Erlware Commons. Each of the directories can be easily selected and expanded. Individual files can be selected and opened. There are a variety of ways to open a file. Below are the most common:

  • <enter> will open the file in the right pane
  • T will open in a new tab within vim and keep focus in NERDTree
  • t will open in a new tab and bring focus to the new tab

IF you want to see the NERDTree browser in all your tabs use :NERDTreeTabsToggle to toggle it on and off. It will be the exact same NERDTree in the exact same state and cursor position on all tabs – nice! Once you are focused on the code in a given tab and you want to jump back to the left and into the NERDTree pane use <ctrl> ww

Once you have a load of tabs open you need to switch between then and to do this you need only two commands:

  • gt will goto the next tab the next tab
  • gT will goto the previous tab

Pro Tip
Map the tab commands and the NERDTreeTabsToggle command by adding the following to your vimrc.


map <C-t> :tabn<Enter>
map <C-n> :tabnew<Enter>
map nt :NERDTreeTabsToggle<Enter>

Ok, now on to editing Erlang with vimerl.

Editing with vimerl

This is not going to be an exhaustive list of vimerl editing commands but just a few of the goodies. The 20% you will use 80% of the time.

Auto-indenting

vimerl will auto-indent for you as you type. But if you come across a line that you want to indent try typing ==. Lets say you want to indent a block of code. Simple, mark the line that starts the block with ma then go to the end of the block and tell vimerl to indent to the mark as such: ='a. Now if your whole file is a mess then try gg to go to the beginning of your file and then =G to indent all the way to the end. You can do this all in one step as in gg=G.

Code Completion

ctrl-x ctrl-o after typing a module name and a : will cause vimerl to suggest function names for you. It does this by searching the .beam and .erl files in the erlang code path (code:get_path() to see what they are) as well as looking at your rebar deps_dir if you are using rebar.config as part of your project.

Skeletons

This is the feature that I loved most about the emacs mode for Erlang, well this and the auto indenting (most of the time, the fun() indenting still feels like a kick in the teeth). Here is a list of the most useful skeletons and the commands to generate them from within vimerl.

  • :ErlangApplication generate the skeleton for an OTP application behaviour.
  • :ErlangSupervisor generate the skeleton for an OTP supervisor behaviour.
  • :ErlangGen[Server|Fsm|Event] skeletons for gen server, fsm and event – yay!

Brilliant isn’t it. Before I let you go there is one more invaluable command you should know about which is :help vimerl which will give you a list of all the other useful commands you may want to use. Remember to get it working be sure to add call pathogen#helptags() to the top of your .vimrc file. Goodbye Emacs, welcome back old friend Vim.

Follow me on twitter @martinjlogan

<esc>:wq

About these ads

From → Erlang, Tutorials

9 Comments
  1. Reasonable addition is vimshell (https://github.com/Shougo/vimshell.vim) which is a full shell that allows to run Erlang VM inside vim (see also http://code.hootsuite.com/vimshell/). I also like vim-surround and vim-repeat by Tim Pope.

    • ypaq, I will take a look at vimshell thanks for the tip. I use both vim-surround and vim-repeat and agree they are quite nice. I did not feel they were essential tools for Erlang dev. Perhaps I will do a followup with some additional plugins like these you mentioned and AutoComplPop (https://github.com/vim-scripts/AutoComplPop) among others

  2. hq1 permalink

    1. Replace vimerl’s indentation with https://github.com/vim-erlang/vim-erlang-runtime
    2. ???
    3. Profit

  3. Thank you for sharing, Martin! Glad to see I’m not the only Erlang vim holdout!

    You must be the highest profile Erlang guy who does Vim instead of Emacs :)

    Nerd tree is pretty cool, and all the rest of this I already use, however I did not know about ctrl+o/ctrl+p for searching the other modules in a project, that’s fantastic, I’ll have to give that a spin today.

    That said, I’m personally not a huge fan of running tabs within Vim. I’ve tried it, but I find myself preferring screen + a vim instance per screen (Maybe I’m just not willing to unlearn screen/byobu bindings).

    To aide in that workflow, I have a tool I just released yesterday called “Fed” for “(F)inding (ED)itor”, (https://github.com/choptastic/fed), which basically does much of what NerdTree does but from the console, and it allows you to also specify which editor to open for which files, so if you work in web development a lot, and have images in your project tree, you can configure fed to run gimp to edit images instead of it opening in vim.

    Using your example above with ec_date (my personal fav!), from the command line (in any directory in your project), you could type `fed ec_da` and it’ll open ec_date.erl, or `fed ec_d` and it’ll give you a choice.

    I recognize, of course, that this is blatant self-promotion, and if that’s unwelcome, I apologize and will retract this (if possible), but I thought it was at least a semi-useful alternative to NerdTree, without stepping on NerdTree’s toes.

    • Jesse, the comment is welcome. Glad you got some use out of the article. Lemme know when you post a tutorial on fed and I will give it a shot.

      • choptastic permalink

        Hi Martin! I just put together a short video demonstrating Fed: http://youtu.be/iIsmgZlY3_M (and before doing so added fuzzy path searching as well). Otherwise, I have a blog post that demos it at: http://sigma-star.com/blog/post/introducing-fed

        I’m undecided on if fuzzy searching should be the default or not, I’ll have to use it for a while to see if it’s too forgiving. But feel free to give it a looksee :)

        Thanks!

  4. dcy permalink

    Most of he plugins you introduced i am using too. But i do not use pathogen, I use https://github.com/gmarik/vundle or https://github.com/Shougo/neobundle.vim. it’s more powerful.
    By the way, there are many useful plugins writen by Shougo(https://github.com/Shougo).
    For example: neobundle, neocomplcache and so on.

Trackbacks & Pingbacks

  1. Используем Vim в качестве IDE для Erlang | Записки программиста

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.