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 need a macro that will let highlight the string that needs translation and the macro will wrap 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.