Write HTML Faster with Sparkup (Vim and Textmate)

words by Brian Racer

I recently came across a really great Vim(and Textmate) plug-in called sparkup.vim that “lets you write HTML code faster”. It’s actually a small python script, but has editor plug-ins to work with Vim and Textmate. It allows us to write HTML faster by leveraging the terse css selector syntax and converting it the much more verbose HTML.

Selector Expansion

Selector expansion is the plug-in’s primary purpose. It lets you write in a CSS selector syntax that get expanded to full HTML:

#album.photo

If you type that into vim and then press Ctrl-e on that line it will be expanded to:

<div id="album" class="photo">|</div>

This next one is a bit more complicated, I’ll explain what’s going on:

#container > #nav > ul > li.first {Home} + li*2 + li.last {About Us} < <  #content > p*2 < #footer > span.copyright {(c) 2010 Jetpack LLC}
  1. Creating a div with the id of “container”
  2. Creating a div with an id of “nav” that is a child of the “container” div. The > specifies we are creating a child element in the DOM tree.
  3. Creating a ul tag that is a child of “nav
  4. Creating an li tag with the class name “first“. Text in brackets will be plain text placed in-between the tag we are creating.
  5. Creating two sibling li elements. The + denotes what comes next will be a sibling. The * is a multiplier to create any number of similar elements.
  6. Creating an li tag with the class name “last” as a sibling to the previous li elements we have created. It will contain the text ‘About Us‘.
  7. Next now use the < symbol to go up two levels of the DOM tree.
  8. Next we create a div with the id of “content” that will contain two paragraph tags.
  9. We go back up a level and add a footer div that will have a span with the class of “copyright” that contains some boilerplate copyright text.

If we press Ctrl-e on that line, it will be expanded to the following:

  <div id="container">
    <div id="nav">
      <ul>
        <li class="first">Home</li>
        <li>|</li>
        <li></li>
        <li class="last">About Us</li>
      </ul>
    </div>
    <div id="content">
      <p></p>
      <p></p>
    </div>
    <div id="footer">
      <span class="copyright">(c) 2010 Jetpack LLC</span>
    </div>
  </div>

It will also place your cursor in the first empty tag denoted by the |. You can jump around to other empty tags with Ctrl-n (I change this mapping, more on that in a second).

If you are a fan of HAML but forced to use standard HTML in your projects this plug-in might make you a bit happier.

Shortcuts

The other piece of functionality this plug-in provides is a snipMate like shortcuts feature. These act much like snipMate snippets except they are hard-coded into the python script. However most are still quite useful and you can view the python script to review them.

Issues

After installing this plugin I was having tabbing/tab-expansion issues. I believe it’s Ctrl-n mapping which allows you to jump around to empty tags was conflicting with some other plug-in I had installed, possibly SuperTab. I remapped it by putting the following in my .vimrc:

let g:sparkupNextMapping = '<c-x>'

That doesn’t seem to conflict with anything for me, and since it only gets used in normal mode it doesn’t conflict with Tim Pope’s excellent ragtag plug-in (read more on that here).

Video Demonstration

Some of this might make more sense when you see it in action, so watch the following YouTube video(make sure to switch it to 720p for crisper text):

Conclusion

Have fun writing faster HTML! You can see more examples on sparkup’s Github page.


How to keep your Vim Plugins up to date

words by Brian Racer

It’s not too hard to learn something new everyday about vim, but did you know there is an easy way to keep your plugins up-to-date? GetLatestVimScripts is a plugin that can do just that. You can grab the latest version from the webpage, but it’s most likely your distributions vim package already has it (Debian, Redhat, OSX, and MacPorts do anyway – check /usr/share/vim/vim72/plugin).

How does it know where to find updates? Either we manually add information about each of our plugins to a special config file, or plugin authors can embed a special comment in their scripts to be update friendly:

$ head ~/.vim/plugin/rails.vim
" rails.vim - Detect a rails application
" Author:       Tim Pope [email protected]
" GetLatestVimScripts: 1567 1 :AutoInstall: rails.vim
" URL:          http://rails.vim.tpope.net/

" Install this file as plugin/rails.vim.  See doc/rails.txt for details. (Grab
...

The bold-ed line will tell the plugin where to find updates. More on that in a minute. First lets create the directory where updates will be downloaded, and a configuration file will be placed:

mkdir ~/.vim/GetLatest
touch ~/.vim/GetLatest/GetLatestVimScripts.dat

By default the plugin will only download the updates and not install them. To enable auto-install put the following in your .vimrc:

let g:GetLatestVimScripts_allowautoinstall=1

Now run :GetLatestVimScripts or :GLVS. Vim will analyze your plugins, see if they contain information about their download location, and add it to the .dat file. Then it will wget the plugins and install them. If you had any plugins that were update friendly, they are now updated to the latest version! Since not all plugins are update friendly, you may have to manually add lines to the GetLatest/GetLatestVimScripts.dat. The format looks like this:

ScriptID SourceID Filename
--------------------------
294 10110 :AutoInstall: Align.vim
1896 7356 :AutoInstall: allml.vim
1066 7618 :AutoInstall: cecutil.vim
1984 11852 :AutoInstall: FuzzyFinder
1984 11852 :AutoInstall: fuzzyfinder.vim
1567 11920 :AutoInstall: rails.vim
1697 8283 :AutoInstall: surround.vim

The first two lines act as comments but are required, so don’t remove them! Next is the ScriptID which is the script_id url parameter on the plugin’s vim.org webpage (ex: http://www.vim.org/scripts/script.php?script_id=642). Then there is the SourceID which is a url parameter which identifies the version of the plugin (ex: http://www.vim.org/scripts/download_script.php?src_id=8283). The ScriptID is what is compared for newer versions. If you are adding a new plugin, you can just set it to 1, run an update, and the number will automatically be set to the latest version. :AutoInstall: is a flag that signifies the update should be installed after download, and lastly Filename is just the filename of the plugin.

GetLatestVimScripts.dat is an example of my update configuration. Also checkout my vimfiles and dotfiles git repos for more vim and shell scripts you might find useful!