This is not intended to be a complete solution for converting haml files to erb. Indeed, there may be better ways to approach this problem (and if you know of any, I’d be glad to hear about them in the comments below). The point of this demonstration is to showcase some of the things that you can acheive using Vim’s macros. You may encounter other scenarios where these techniques come in useful.
Single line conversion
For a haml one-liner the conversion to ERB is straightforward. The following steps were taken:
qpto record keystrokes into the p register
Ito insert “<%” at the beginning of the line
Ato append “ %>” at the end of the line
qto finish recording the macro
I then searched for the pattern
^\s*= to jump to the next one-liner. The macro can be replayed by pressing
@p. Subsequent one-liners can be converted by running
n jumps to the next match, and
@@ replays the macro that was most recently played.
Whereas haml uses significant whitespace to delimit blocks (in a similar fashion to Python), erb uses the
end keyword (like tradtional Ruby). I created a macro that allows me to visually select a region that needs to be terminated with an
end keyword. Playing the macro converts the first line to erb by wrapping it in
<%- %> delimitters, then appends the
<% end %> keyword after the block.
The macro works by setting marks at the top and bottom of the visual selection. This allows me to jump between the two regions that need to be edited. You can set a mark by pressing
m followed by a letter. I chose to use the letter ’t' for the top mark, and ‘b’ for the bottom mark because I find these easy to remember. You can use whatever mnemonic suits you. Having set the marks, you can then jump to them by typing
't. That is, single-quote followed by the letter of the mark you want to jump to.
When I open an ERB file, Vim detects the filetype from the file extension and sets the auto indentation accordingly. In this case, I have a HAML file with the ERB extension. This is going to confuse Vim’s automatic indentation, so I switched it off by running: