February #TMIL - vim

Although it's already halfway through March, I'm not quite ready to give up on my resolution to collect a few of the things I've been learning each month as an opportunity to review the material. Don't worry, it's all a healthy exercise in non-competitive professional development. I haven't gone crazy but the risks of losing balance described here are very real, especially when you enjoy what you're doing. OK, on to the good stuff.


I've been meaning to learn more than the basics of Vim for years and finally dug in last month.

Like so many things, getting your environment set up properly in the first place will make things much easier. And repeatable. I began with Vundle, a plug-in manager for Vim. Although the project's founder recently announced he'd be stepping down, it looks like a few people have stepped up to work on it, so it seems solid for now. Grab it here.

To further ease the transition, I downloaded and expanded MacVim then moved the mvim executable into /bin so I could trigger it from the command line. To get around, I knew I needed NERDTree. After adding the bundle to my ~/.vimrc file, (based on the sample one here) then running :BundleInstall in MacVim. Next up was basic navigation. The :NERDTree command opened a sidebar file explorer, and :NERDTreeToggle to show/hide it. While you can launch a project from the terminal with mvim . you can also get there from within MacVim with :NERDTree path/to/myawesomeapp. This post from Thoughbot shows the basics of the vim-rails plugin, such as commands like :Rmodel mymodel to jump right to app/models/mymodel in edit mode. The plugin also adds syntax highlighting (set `syntax on` in vimrc), which is always helpful. There are bundles for Coffeescript and Javascript, too.

To make these more easily available to me at work or wherever, I created a dotfiles repo. Now on whatever machine I'm using, I can install vundle, download my repository and create a symlink to the .vimrc from the user home path, then :BundleInstall.

Here are a couple of other essential vim shortcuts in normal mode:
  • gg=G to tidy up the indentation of a file
  • zt to put the line the cursor is on at the top of the screen
  • :%s/searchterm/replacementterm/g to search and replace 
  • $ or 0 to get to end/beg of line, hjkl for left, down, up, right
  • dd to delete a line
  • gg or 1G jumps to top of file, can use 2G to jump to start of line 2, G to jump to the end of the file
  • :set number to add line
And a few for visual block mode (control-v to get into it):
  • 5j$ will select five lines and select to end of each line
    • replace by hitting r after a selection, then the next key will replace 
    • or delete with d
  • when you have an area of text selected, the letter o jumps to the opposite corner to more quickly resize the selection
  • search and replace in a selection by pressing : to enter a command, then s/searchterm/replaceterm/g and hit enter
  • ctrl-V then G to select column down to end of the file


One feature I'd really like to master is jumping around with ctags to explore source code and documentation. Here are some common commands:
:tag [identifier]Jump to the identifier.
:tagsList the tag stack.
Control-]Jump to the tag under the cursor.
:tselectSelect which tag location to go to for the current tag.
Control-tJump back from the current tag.
:stjump [identifier]Jump to the identifier in a new split window.
(chart source and how to get setup)

This link shows how to set them up in a Rails project (ctags -R . $(bundle list --paths)), then usage is simple as, for example, :tag link_to to show show the source of the link_to helper. The commands in the chart above work as well. control-G will show current file's location and you can use use :!echo % | pbcopy to copy the path to your system clipboard. Pry has some great features for jumping around documentation as well, and I've been using Pry-rescue much more after watching this great video (slides).


-Check out Divvy, which has become an essential part of my workflow. It allows you to auto-resize and move windows around your screen using only keyboard shortcuts.
-I completed Metaprogramming in Ruby, a book I'd highly recommend. I'll look at some examples from the book in next month's post after digesting it a bit more.

Popular posts from this blog

Thinking About BIPA and Machine Learning

A Privacy Engineer's Guide to the EU AI Act

Changing PDF Metadata with Python