I’m writing a function that automatically inserts some text under certain conditions. I stuck at a part when I try to emulate Enter press. I come up with this (obvious) code:
execute "normal! a\<cr>"
The problem is that it breaks the autoindentation. For example, I have a text file like this (| is a cursor):
if (condition) {
action();|
So I run execute "normal! a\<cr>". When i need this:
if (condition) {
action();
|
it acts like this:
if (condition) {
action();
|
Of course, autoindent is on. What am I doing wrong?
It is actually working as expected. If you execute
:execute "normal! a\<cr>helloit will put ‘hello’ in the right place. If you press AEnterEsc it will drop to the start of the line. The command drops out of insert mode and since it’s a blank line, unindents.