Versão em português
Rodrigo-header Rodrigo Rosenfeld Rosas

Achieving Productivity with Vim as IDE

Fri, 10 Sep 2010 20:00:00 +0000 (Updated at Tue, 20 Mar 2012 21:30:00 +0000)

Finally I've got some time to finish translating my Vim original article in Portuguese (written in September/2010):

I've long insisted on trying to use Java written IDEs like Netbeans, RubyMine, Aptana/Eclipse or IntelliJ IDEA for software developing. They are fine except that they use too much system resources and you never know when the next garbage collection will happen (usually in the greatest inspiration moment).

I was so upset with memory usage (my 4GB RAM computer was swapping very often) and garbage collection that I decided to take 3 full days of my last holiday to learn how to get productivity with Vim. The result was good enough and here is the summary of what I could get from Vim and what I could not.

Note 1: if you are already a Vim user, backup your configuration files before trying this setup. Note 2: I would like to thank Michael Durrant and Vim spelling support for helping with translation.

What to expect?

In addition to these features, I've got much more ones that I've never used on my prior IDEs experience, as shown in this article.

Installation

Here are the install procedures tested on a Debian Unstable Linux distribution that should work almost seamless with Ubuntu too. In Windows, apparently the change is that the configuration directory of Vim is called "vimfiles" instead of ".vim". If you have any questions about the installation process, just post a comment.

You need to be root (or use sudo in Ubuntu) for installing the required packages.

1
2
3
4
apt-get install exuberant-ctags vim-gtk git
cd
git clone --recursive git://github.com/rosenfeld/vimfiles.git .vim
ln -s .vim/vimrc .vimrc

Some additional notes in case you have any issues with the above steps or are just curious:

Features

Vim has much more features than what I'll introduce on this article. I would suggest reading other resources on the subject if you have some free time.

Basics

Editing, saving, navigation and quiting

Unlike other editors, Vim has different modes. It starts in Normal mode, in which typed chars are interpreted as commands. Pressing 'i' or 'Insert', enter Vim in insert mode, from which you can type anything. To exit insert mode, just press 'Escape'.

Most commands are available through a command line that shows up when a colon (':') is pressed. Some of them are:

On insert/editing mode, you can call normal mode commands by pressing Ctrl+O before the command. While on normal mode, it is possible to change to insert mode using some commands:

Commands for deleting lines, words, blocks, managing surrounds and toggling comments:

Copy and paste

In normal mode (don't use ':'):

In visual mode, 'y' copies the selection, while '"+y' / '"y' copy the content to registers + and .

Windows and Tabs

I've already commented about basic tabs-related commands. Further commands follow below:

Quick file open

The '<c-x><c-f>' (Ctrl+X Ctrl+F) shortcut activates the quick open file dialog.

Vim will list files in your current dir (launch ':pwd' command to see what is it and ':cd ~/new/path' to change to a new path). While you type, files are filtered considering the typed expression. For instance, 'a/c/uc' will list 'app/controllers/user_controller.rb' as an option.

Hit Enter to open the file in the current buffer. Ctrl+t will open it in a new tab. Ctrl+Enter will open in a new window.

Snippets

Snippets are expanded with the TAB key. For instance, div<TAB> will expand to <div id="?">?</div>.

The bundled snippets are located in ~/.vim/bundle/snipmate/snippets and ~/.vim/bundle/rosenfeld/snippets.

Feel free to modify them and include new ones on bundle/*/snippets and ~/.vim/snippets.

Editing HTML, XML, ERB, ASP, JSP, PHP, GSP, etc

Shortcuts for working with HTML/XML also work on PHP, ASP, ERB, JSP, etc, once the file type is properly configured like "html.erb". This can be achieved with command ":set ft=html.erb", for ERB files, for instance. You can also set these associations automatically according to file extension. See some examples in ~/.vim/filetype.vim.

Some shortcuts for working on HTML have been already discussed. Here are some more shorcuts, for being used on insert mode:

For template files, like ERB, JSP, PHP, etc:

For ERB (Ruby), I've created the following alternative snippets:

If you use KDE, it's possible to launch kcolorchooser for returning a hex color into the document (a CSS, for instance), hitting F12. Take a look at ~/.vim/initializers/kcolorchooser-mapping.vim for changing your software of choice.

Spelling check

Commands:

Tags

There are some alternatives for working with tags in Vim:

Plugin tag-list

Commands:

Native support integrated to ctags program (provided by exuberant-ctags, for instance)

For this to work, you must create a "tags" file in the current directory. Take a look at the output of "ctags --list-languages" to see the supported languages:

1
ctags -R --languages=Ruby,Javascript

Groovy is not supported by standard exuberant-ctags, but adding this content to ~/.ctags file seems to work. You can do that with this command (in Linux or Mac):

1
curl https://raw.github.com/gist/2142910/ctags >> ~/.ctags

Then, use the following commands for jumping to tag definition of the word under cursor: - Ctrl+] or Ctrl+<LeftMouse> or g<LeftMouse>: jump to definition in current window - Ctrl+T or Ctrl+<RightMouse> or g<RightMouse>: go back to position before jump - Ctrl+w, ]: split horizontally and jump to tag definition - g, Ctrl+] and Ctrl+w, g, ]: presents a list of definitions before jumping if there are multiple definitions - ':tag TagName': go to 'TagName' tag definition - ':ts TagName': open a list with found definitions to choose from - Ctrl+\: go to definition in a new tab

Indenting

Commands:

Finding and Replacing

Commands:

Markers

Commands:

Changes list

Commands: - ':changes': list all changes in the current buffer - g;: go to the last change - g,: go to next change - 4g;: go to the change #4 (numbers are displayed by the ':changes' command)

Navigation among buffers

Commands:

File tree navigation

File tree shortcuts:

External commands

For running an external command:

Git integration

See $VIMHOME/bundle/vcscommand/doc/vcscommand.txt for other commands. For instance: - \cd: show the diff for the current file in a new horizontal split - \cr: review the last committed version of the file in a new window

Suppose you want to know what are the differences from your current unsaved changes and the original file: - \cr: split the original version in a new horizontal split. If you want the split to be vertical, you can move the window to the left (Ctrl+w, H) or right (Ctrl+w, L). H and L must be capital. - run ':diffthis' in both windows: see next topic on diff.

You can also take a look at $VIMHOME/bundle/fugitive/doc/fugitive.txt for further git shortcuts, like: - ':Gstatus': show the output of 'git status' and allows you to stage or unstage files under cursor pressing '-', or viewing the diff in a vertical window (pressing 'D') or in a horizontal window (pressing 'dh'). - ':Gcommit', ':Gblame' and ':Gmove' are other self-explanatory examples. Take a look at fugitive documentation for more details.

Viewing files difference

Open at least two windows with the text you want to see the differences and type ':diffthis' on each window. For turning the diff off, type ':diffoff'. Use 'dp' in one highlighted diff for putting it in the other window or 'do' to obtain the difference content from the other window. Use '[c' and ']c' for navigating backwards and forwards to the next start of a change. See ':h diff' for more details.

Getting vim help

Ruby specifics (Rspec, RDoc, etc)

Commands (won't work in some terminals, use gVim or MacVim):

Rails commands (use tab for auto-complete most commands):

Debugging:

You need to install the 'ruby-debug-ide19' or 'ruby-debug-ide' gem for this to work:

Refactoring

Although Vim doesn't allow you to directly refactor some variable for instance (at least, I don't know how to do that in Vim), it can help you refactoring your code in many ways, from substitution commands to variable extraction like the example above:

Suppose you want to refactor the code below as follows:

1
2
3
  if User.find(params[:id]) and current_user.admin?
  # ...
  end

to:

1
2
3
  @user = User.find(params[:id])
  raise NotFoundException unless @user and current_user.admin?
  # ...

For extracting the "User.find(params[:id])" to the "@user" variable, you can position the cursor under the "U" and run the commands "c% @user" (change the content "User.find(params[:id])" with "@user"), "Ctrl+o, O" (execute the 'O' command while on insert mode [Ctrl+o] - create a new line above the current), "@user = " (just typing), 'Ctrl+R "' (paste the cut content).

With all these explanations, it may seem hard, but take a look at how we can achieve this with so few keystrokes: c% @user <Ctrl+O>O @user = <Ctrl+R>".

Learning how to use Vim in its full power will allow you to do many tasks quicker than any other editor or IDE in the overall. For instance, RubyMine will allow you to do the same with less keystrokes for this specific case, but for special cases, Vim will still be more useful and not much less productive than RubyMine for this common case. Actually, cutting "User.find(params[:id])" is much faster in Vim ("c%") than selecting the whole text in RubyMine or any other IDE. The same apply for change the content inside quotes, parenthesis, XML tags, etc among other features.

What doesn't work?

Unfortunately, I couldn't find every feature I wanted in Vim yet. Some of them present on regular IDEs include:

More to come

There are still more useful commands like folding and other interesting features that I'll comment when I have more time available.

I've already commented about several commands and I suggest you to start learning those that you use more often, like snippets, simple search and replace, quick file opening, tabs usage and buffer navigation. For those that work with HTML, I would recommend taking a look at the "surround" plug-in, that are specially useful for working with XML/HTML tags.

As a last keynote, this article was written with Vim in the Markdown format. Many of these examples include tags and for escaping them in the document, I've used the command ':%HTMLSpecialChars' from the plug-in 'htmlspecialchars'.

If you can take some time to improve your Vim skills it will save you many coding time during your coder life.

Good advantage and have fun!

comments powered byDisqus