Two Wrongs

Custom Surrounding Text with surround.vim

Custom Surrounding Text with surround.vim

surround.vim is a great Vim plugin by the almighty Tim Pope. It is commonly mentioned when people ask about plugins that really should be default features in Vim. Until this point, I haven't really used it a lot. I'm aware the key combinations to invoke it are natural in a Vim workflow, but I also realise they will take some time for me to learn – time I haven't put into it yet.

However, this time I had a really good reason to use the plugin.

animation of me using the described surround.vim command

I had a bunch of markup which defined a table, in which some words were supposed to use the translation tag provided by the Django internationalisation engine. This means that a bunch of $word should be replaced with {% trans "$word" %}. Not fun to do manually. And since it's not a symmetric thing either, I thought using a surrounding plugin wouldn't work.

Fortunately, I was wrong. You can tell surround.vim to do pretty much any kind of surrounding. By defining

let g:surround_116 = "{% trans \"\r\" %}"

(where 116 is the ASCII code for the small letter "t"), I have told surround.vim that by pressing t I want to surround something with the translation tag.

This means that I can, as in the gif demonstration, select something with visual mode and then press shift+s, t and bam! Surrounded with the translation tag.