I have the following mapping set up to quickly disable a block of C++ code using #if 0...#endif

:vmap 0 V'<O#if 0<Esc>'>o#endif<Esc>

What this does is set up a mapping that only works while Visual mode is enabled (vmap), which disables visual mode (V), goes to the beginning of the last visual selection ('<), enters a new line above it (O), enters the "#if 0" text then quits edit mode (<Esc>), then goes to the end of the last visual selection ('>), adds a line below (o) and enters the "#endif" text.

You could do something similar by replacing the start and end text, e.g.

:vmap 0 V'<O{% comment %}<Esc>'>o{% endcomment }<Esc>

Note that this requires the use of line-by-line visual mode, entered with a capital V, rather than character-by-character visual mode that uses a lowercase v.

I've tried to use your solution for character visual mode and failed. If I use v and select some chars and press EscEsc and press '< or '> (in both cases) I get on first non-space character of line where I've selected some characters.
–
Gregory PetukhovAug 4 '11 at 7:42

Yeah, I guess this does not work for character visual mode, it is after all a line-based operation.
–
TooManyKooksAug 4 '11 at 8:31