4

Yesterday I'm trying to make a doc command abbreviation for python.

The original command is normal 0i"""^M""", which works in command-line mode.
But failed when using alias doc defined by ca doc normal 0i"""^M""".

And later, I find I need to quit insert mode, do some movement, and then enter insert mode again.
The command normal 0i"""^[$a^M""" failed both in command-line mode and with alias.

How should I deal with ^M and ^[ in normal command alias,
and how should I manipulate insert mode quit and enter again action with normal command?

PS: The ^M and ^[ character was made by combining CTRL-V + Enter/Esc.

  • 1
    "Command mode" is a synonym for "normal mode". Did you mean "command-line mode"? – romainl Sep 8 '15 at 11:43
  • @romainl, yes, it should be command-line mode, I've edited the content. – Zen Sep 8 '15 at 12:24
2

And later, I find I need to quit insert mode, do some movement, and then ...

I'm not sure where you found that from, but it's the other way around - you should quit insert mode before ending a :normal command, otherwise :normal will cancel the last operation. From :h :normal:

{commands} should be a complete command.  If
{commands} does not finish a command, the last one
will be aborted as if <Esc> or <C-C> was typed.
This implies that an insert command must be completed
(to start Insert mode, see :startinsert).  A ":"
command must be completed as well.  And you can't use
"Q" or "gQ" to start Ex mode.

Now, your original command:

ca doc normal 0i"""^M"""

using an abbreviation instead of a proper command, ^M will be taken assumed to end the normal command (the way it would be if you'd actually typed it), and not be a part of it. Then you'd have to do:

ca doc normal 0i"""<cr>"""

Use the <cr> code available for maps and abbreviations. Though I think you'd be better off using a normal-mode map instead of using a command-mode one. Or use a proper command:

command! Doc normal 0i"""<cr>"""

Then you can use the :Doc command.

  • Excellent answer, is there such a mapping to Esc just as <cr> to Enter? Is there a vim help command that can view all such mappings? – Zen Sep 14 '15 at 9:14
  • 1
    @Zen indeed there is: <esc>. (See the first example in macro.txt.) – muru Sep 14 '15 at 9:16

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.