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.
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
startinsert
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
startinsert
execute
is no more needed, making the code quite clear.
execute "..."
if you're using any control sequence, such as in your first code line?
: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
<c-o>
withexecute "normal! o\<C-o>S"
. See:help expr-quote
which explains how double quoted strings work...