I am trying to make a new custom command in my .vimrc, so that when I type


the command will convert the text of the current line into an HTML comment.

As an example:

<!-- abcdef -->

I do not want to use a macro nor a mapping. The equivalent macro would be

let @h = '0i<\!-- ^[A -->^['

I cannot find out how to do this. Is it at all possible without a script? I tried the following, with and without escapes, without success:

:command Hcom put='0i<\!-- ^[A -->^['

1 Answer 1


The :put command inserts register contents (or, as the special case you're using here, the result of evaluating an expression, here: a static string) literally into the buffer. It cannot be used to play back commands.

As your commands are normal and insert mode mappings, you can use :normal! to execute them. A key mapping would directly take them on the right-hand side; as custom :commands work in Ex mode, you need :normal as a translator from Ex to normal mode.

:command! Hcom normal! 0i<!-- ^[A -->^[

The embedded ^[ must be literal Escape characters; I would recommend to write them in the :help key-notation; you then need double quotes and :execute to interpret the string:

:command! Hcom execute "normal! 0i<!-- \<Esc>A -->\<Esc>"

Before you invest much more energy into tweaking this command, please note that there are several commenter plugins that do this very well, and those are generic (and often extensible) so that they work for any filetype:

  • Thank you, and thank you for the advice as to the plugins as well.
    – Akitirija
    Jan 13, 2018 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .