Suppose I have the following two lines in a function:

execute "normal! o"
execute "normal! S"

Is there a way to combine these so it is simpler, for example:

execute "normal! o<c-o>S"

It seems I need to 'escape' the <c-o> somehow.

  • 1
    Yes, escape <c-o> with execute "normal! o\<C-o>S". See :help expr-quote which explains how double quoted strings work... – filbranden Jun 15 '20 at 3:53

In execute parameters, you need to escape special characters sequences with \:

execute "normal o\<c-o>S"

But this will not let you in insert mode. You could add


to force insert mode, but it will not respect autoindent. I don't know how to get around that.

You might as well go for a list of instructions:

normal! o
normal! S

execute is no more needed, making the code quite clear.

  • sure, but wouldn't you need execute "..." if you're using any control sequence, such as in your first code line? – David542 Jun 15 '20 at 5:23
  • @David542 Yes, indeed. I edited my post to make it clearer what I meant. – Biggybi Jun 15 '20 at 5:40
  • You still want a bang on normal – D. Ben Knoble Jun 15 '20 at 11:13
  • @D.BenKnoble I'm not sure I understand the doc but I believe in you. – Biggybi Jun 15 '20 at 12:32
  • Well, typically it’s unlikely you want to use someone’s mapping for S when you do :normal S in a plugin—but without the bang, if I have nnoremap S anything it will take effect! Its like noremap for normal; it’s become something of a best-practice, and it’s good defensive programming. The idea is to use it unless you know you want to invoke a mapping via normal – D. Ben Knoble Jun 15 '20 at 12:36

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.