I would like to be able to select several lines using vim when editing an HTML file, then enter some hotkey and get these lines wrapped in a django comment block.

For example, I have:

one line
another line

I want to get:

{% comment %}
one line
another line
{% endcomment %}

Maybe worth to take a look to this vim plugin http://www.vim.org/scripts/script.php?script_id=1697


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.

  • It works! Thank you for simple clean solution. – Gregory Petukhov Aug 4 '11 at 6:19
  • 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 Petukhov Aug 4 '11 at 7:42
  • Yeah, I guess this does not work for character visual mode, it is after all a line-based operation. – user8909 Aug 4 '11 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.