Make Sublime Text 2 More like Vim: Wrap Code, Go To Last Edit, Jump Back, and More

I’ve been trying out Sublime Text 2 as a replacement for Vim. While I enjoy using it and I experienced the “Wow, this does 90% of what Vim does” moment, I kept a running list of all the features in the remaining 10% that I relied on every day.

These included:

  • Better code wrapping (gq)
  • Go to last edit ('.)
  • Go to file in Ack search output
  • Display full path to current file (:echo expand('%:p')<CR>)
  • Jumping back and forward through files

However, Sublime Text 2 has a great Python API and I was able to whip up plugins for these tasks that perform just as well as in Vim. Update: I also found the navigationHistory plugin, which does a pretty good job of back/forward jumps.

Note: The following plugins are not verified to work with Sublime Text 3. If you’d like to contribute to updating them for ST 3, you can do so here.

Better code wrapping

Vim’s reformat text command (gq) can take multiple paragraphs and text in comments and flow them to the current textwidth setting. Some people don’t care about this, but I prefer to keep lines of code less than 80 characters wide, so I can open multiple files side-by-side. (Also, Pep-8.)

Sublime Text 2 had a “wrap” feature, but it failed to intelligently wrap comments, it joined separate paragraphs together and it wouldn’t reflow selected text (only the paragraph around the cursor).

The plugin I wrote creates a Wrap Code command (mapped to gq in Vintage mode) that works reasonably on commented lines of code (and uncommented lines), multiple paragraphs and selected text, thanks to the codewrap.py module written by Nir Soffer.

Download the WrapCode plugin on Github.

Go to the last edit

I’m used to typing '. in a buffer in Vim to move the cursor to the last edit. Great feature. Totally underrated.

If you use Vintage mode in Sublime Text 2, you’ll quickly discover that this command does nothing — worse than nothing, in fact, as it seems to refocus your cursor somewhere other than the text of the buffer, forcing you to use your mouse to recover.

While I couldn’t bind my plugin to the '. command without forking Vintage mode, I bound it to Super+' and it works the same as Vim’s.

Download the GotoLastEdit plugin on Github.

Easily open search results

Another thing I missed about Vim was its Ack plugin. The “Find in Files” feature of Sublime Text 2 is great, but it didn’t provide an easy way to quickly open a file listed in the search results via the keyboard (you can double-click on a line to open it, though).

So I wrote a plugin that, in a Search Results window, allows you to do one of the following via the keyboard:

  • On a “matched” line in the search output, open its file at the line of the match
  • On a file path in the search output (without a line number), open the file in a new tab

Download the OpenSearchResult plugin on Github.

Display full path to file in status bar

I use Vim in OS X’s full screen mode, with no tabs or status line. Working this way, I don’t have reference to the path of the current file. Of course, I know the name of the file because I usually typed it, but sometimes the full path is important; e.g., if I have two Mercurial branches of the same code in different directories.

So, I have a command mapped to ,F that displays the full path. Then it silently goes away after a moment. I love that command.

I couldn’t find an ideal way to implement this in Sublime Text 2, other than to create a command that would toggle displaying the path to the current file in the status bar. It works well enough for me, however.

Download the FilenameStatus plugin on Github.

Jumping back and forward

One of Vim’s most awesome features is Control-O/Control-I to jump back and forward — maybe I can generalize to say that all of Vim’s jump commands are part of its “killer app” status.

Sublime Text 2 doesn’t have a feature like this, but someone on the internet has packaged a version of Martin Aspelli’s navigationHistory plugin, which comes close.

Download the navigationHistory plugin on Github.

Installing the plugins

You install these plugins the same way as other Sublime Text 2 plugins, by downloading the files and dropping them into the Packages directory.

See this documentation for more details if you need additional help installing plugins.

The default key bindings are intended for Vintage mode and are oriented for OS X.

Sharp Edges: Protecting Ourselves from Digital Publishing

Nicholas Carr wrote in a recent article that he considered the ability of publishers to change text after they had released it “insidious” and a “bane” of digital publishing — specifically, if such changes are made in response to market research.

I agree that there is a challenge inherent in the new ease with which publishers may release versions of a text, but the challenge I see is different than the one Carr suggested. Data about the chapters that readers skip and areas that cause people to abandon their reading will only help publishers create better, more relevant content, just as this data has helped web site authors do the same. It is our response, as readers, to the possibility of frequent (and silent) revisions to text that I worry about.

Movable Text

As a summary of the key difference between print and electronic publishing, Carr described electronic publishing as having replaced Gutenberg’s movable type with “movable text.” For centuries, once set and printed, the text of a book remained the same. Today, with web sites and now ebooks, publishers may change the text at any time, introducing multiple divergent copies or, if the distribution method supports it, even changing the copy you are reading as you read it.

Carr wrote about the downside of this change that,

The promise of stronger sales and profits will make it hard to resist tinkering with a book … adding a few choice words here, trimming a chapter there, maybe giving a key character a quick makeover.

Books That Are Never Done Being Written

What is wrong with this? While some readers may finish every book they start, I have dismissed dozens of books in my life, at various points in their stories, after having read one too many missteps of voice or plot, or simply because I was bored. Life is short. There are more books to read than I have time for. So why should I read a poorly written book, and what do I care for the “shape” of a book (to quote Carr quoting Updike) if I can’t connect with it?

Creators and publishers who release work on the internet can get faster and more in-depth feedback from consumers than in traditional publishing — not just through comments but through analytics about a reader’s behavior. Analytics can show what people look for in a text and different ways they respond (e.g., most people stopped reading on page two). As anyone who has written a blog or maintained a web site will know, this information is extremely valuable as way of testing what people want to read and what they don’t.

The same will now be true for books. As readers, our reactions to books, not just the fact that we purchased them but more intimate details like how long we lingered on a page and where we stopped reading (if it wasn’t the end), will place us in tighter feedback loops with authors. How is this a “bane”?

Authentic Text

What is wrong with this model is that we must change our idea of the persistence and security of human knowledge to fit it. We have to create new mechanisms to ensure the authenticity of texts. There are measures we can take to accomplish this:

  • Creators should have control over changes made to their works, to protect themselves from publishers introducing alterations based on sales data.
  • Readers should have access to all released versions of a text. Each authorized edition of a text should have its own ID that is registered with a trusted authority. And if the publisher releases a new version of a book that is already on our ebook readers, we ought to have the right to approve whether or not we update to it.
  • A trusted organization of the public good should house a copy of each version of released texts, to reduce the chance that individuals, companies and governments can alter or destroy the source files.

In the traditional publishing model, a printed edition of a work with an ISBN is a “known good” source copy. It is authorized by someone. Multiple editions of a book may exist, but as readers and historians we can examine our authorized copies of these editions. We can protect them.

As we move toward using and relying on digital text, we must develop new means of protecting the authenticity of this information. The bane — the sharp edge — of “movable text” is not the ease with which we may change our books after we publish them. It is that our mechanisms for protecting ourselves from such change are outdated.