Vim: Annotate strings with gettext (the macro way)

If your application does not use gettext strings then you must do a lot of rewrites of following form:
  • "My String" needs to be rewritten to _("My String")
  • And in templates you need to rewrite My String to ${ _("My String") }

Doing this over huge codebase is a pain in the ass and automating it can be error prone. Luckily we can write a couple of Vim macros that can help with this job!

The basic ideas is to select text and press si to annotate it with gettext function.

To implement this simply pass this to your vimrc:

vmap si s(i_<esc>f)
au FileType mako vmap si s"i${ _(<esc>2f"a) }<esc>

You use it in a following manner:

  • You select some text (in visual mode)
  • You press si
  • Your text gets transformed from "... Text selection..." to _( "... Text selection..." )
  • There's extra support for annotation inside mako templates

The above snippet requires surround.vim plugin!

The above reason is why I love to use Vim. I can spend 10 minutes on creating a macro that saves me hours of manual labor!

8. Feb 2012 Tips · VIM Editor
© Amir Salihefendic