Vim Macros for Wrapping Strings for Gettext

I’m working on a website, and we decided to localize it using GNU gettext. Soon enough, I found it tiring to wrap each string manually in _( and ), and also to do it in Smarty (using {t}string{/t}). So I decided that I needed a macro that would let me highlight the string that needs translation, and the macro would wrap it for me.

I ended up writing two macros: one for PHP files (but it’s also good for C/C++, etc.) and one for Smarty.

:vmap tg di_(<ESC>pa)<ESC>
:vmap ts di{t}<ESC>pa{/t}<ESC>

To use these macros, just highlight the string for translation in Vim’s visual mode and press tg (or ts), and your string will be wrapped for translation.

3 thoughts on “Vim Macros for Wrapping Strings for Gettext”

  1. Hi,

    thanks for the idea. I was thinking about adding a mapping myself but wanted to check what others did in this area. I’ve ended using:
    :vmap tt “tdi_(t)
    Which works better for me because it doesn’t play with the default buffer 🙂

    /Janusz

Leave a Reply

Your email address will not be published. Required fields are marked *