I have this mapping in my .vimrc:

inoremap <C-f> <Esc> : exec '.!printf "world"'<CR>

I expected it to replace the current line with "world" after pressing C-f in insert mode, but it replaces the line below the current line. Why is that?

When I do :exec '.!printf "world"' manually (in normal mode because it's not possible in insert mode) it correctly replaces the current line.

EDIT. This problem only appears when I'm at the very end of the line in insert mode. Otherwise it works.

  • Works as expected, here. Try again in $ vim -Nu NONE.
    – romainl
    Mar 17 at 10:23
  • @romainl I put inoremap <C-f> <Esc> : exec '.!printf "world"'<CR> in myrc file and did $ vim -Nu myrc. The problem is still there. It's only when I'm at the very end of the line in insert mode. Otherwise it works. I added this in my question.
    – k-dx
    Mar 17 at 10:44
  • Not the problem, but: execute is unnecessary in this short example
    – D. Ben Knoble
    Mar 17 at 19:09

1 Answer 1


The problem is caused by the extraneous space between <Esc> and ::

inoremap <C-f> <Esc> : exec '.!printf "world"'<CR>

The right-hand side of a mapping is a macro. It means that everything in there is supposed to be a Vim command. From insert mode, <Esc> puts you in normal mode, where :help <space> is a legitimate command. Therefore, your right-hand side doesn't tell Vim to:

leave insert mode and press : to enter command-line mode […]

which is what you intend it to do. Instead, it tells Vim to:

leave insert mode, move the cursor one character to the right, and press : to enter command-line mode […]

with an extraneous instruction in the middle.

Because s is included by default in :help whichwrap, you are effectively telling Vim to move the cursor to the line below if the cursor is on the last character of the line before executing the rest of the mapping.

The fix is easy, remove extraneous spaces from your mapping:

inoremap <C-f> <Esc>:execute '.!printf "world"'<CR>

FWIW, you can now use :help <Cmd> to implement mode-switching mappings more cleanly. In this case:

inoremap <C-f> <Cmd>execute '.!printf "world"'<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.