Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: why doesn't ":normal! i" enter insert mode?

Tags:

vim

insert

mode

In vim, if I execute the following from the command line

:normal! i 

vim does not enter insert mode. Likewise the command

:normal! A 

will move the cursor to the end of the line, but the cursor remains in insert mode.

Any ideas why this doesn't work? Failing that I would like to know an alternative way to enter insert mode from the body of a vimscript function (and have insert mode persist after the function returns of course).

Edit: Looks like :startinsert will fullfill the second part of the question, but I'm still wondering how I can do something like :normal! A or :normal! a and why those don't simply work as expected. Simulating "append" with arrow movements is a bad solution, because of things like empty lines and such.

like image 816
Ein Avatar asked Jul 20 '12 21:07

Ein


People also ask

How can you cause vim to enter insert mode?

To go into INSERT mode from COMMAND mode, you type i . To go back to COMMAND mode, you type the esc key. vim starts out in COMMAND mode. Over time, you will likely spend more time in COMMAND mode than INSERT mode.

How do I go back to normal mode in Vim?

By default, Vim starts in “normal” mode. Normal mode can be accessed from other modes by pressing Esc or <C-[> .

How do I turn off insert mode in Vim?

Pressing ESC quits from insert mode to normal mode, where you can press : to type in a command. Press i again to back to insert mode, and you are good to go.

How do I switch between vim modes?

Changing the modes The most commonly used command to enter in to insert mode is “i”. To shift back to normal mode, press Esc. To switch to the visual mode from Normal mode, different commands are v, V, Shift + v, and Ctrl + v. The most commonly used command to enter in to insert mode is “v”.


2 Answers

The normal command considers ending in insert mode as an incomplete command and aborts. From help 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. The display isn't updated while ":normal" is busy. This implies that an insert command must be completed (to start Insert mode, see :startinsert)

:startinsert might be the command you are looking for.

:normal A can be achieved by appending a bang (!) to startinsert, as suggested by Ingo Karkat. From help startinsert:

When the ! is included it works like "A", append to the line.

like image 101
Thor Avatar answered Sep 29 '22 22:09

Thor


In addition to already mentioned startinsert you can use feedkeys():

call feedkeys('A', 'n') 

will do what you want, but the key you added this way will only be processed after execution of current script/function/mapping/etc is finished.

like image 42
ZyX Avatar answered Sep 29 '22 22:09

ZyX