I'm remapping o and O in my Git Bash vimrc to insert a new line without staying in insert mode:

nnoremap o o<Esc>
nnoremap O O<Esc>

The problem is that a new line is inserted and the literal character sequence <Esc> is added onto the new line. How can I fix this, and instead exit insert mode?

Im editing the etc/vimrc and this is Git Bash, so it is Git's own version. This path is local/programs/Git/etc/vimrc I think. :version tells me this is the system vimrc, but probably this is only for Git. Also, :scriptnames tells me both the vimrc and defaults.vim are loaded. I've looked through defaults.vim but nothing pops out.


(Doesn't look like @filbranden or @AndrewHo-Lee are planning to answer this, so adding a Community Wiki answer with the diagnosis they figured out.)

The problem turned out to be that 'compatible' was set when the mapping was defined.

With this set, the 'cpoptions' setting contains the < flag, which means that Vim will not treat the <Esc> in the mapping's rhs as a "special" key, and will instead interpret it as the five separate keystrokes: <Esc>.

'compatible' is set by default. Creating a user vimrc will cause it to be unset, but the OP did not have a user vimrc, only a system vimrc. The fix was to move their configuration from /etc/vimrc to ~/.vimrc. (Manually unsetting 'compatible' in the system vimrc before defining the mapping would also have worked, as would have using <special> when defining the mapping.)

  • Thanks @Rich for writing this up! Mar 1 at 13:44

Updated answer:

Using :substitute to replace end/beginning of line with return got the results OP wanted.

nnoremap o :s/$/\r/ <cr>
nnoremap O :s/^/\r/ <cr>

Original answer:

I can't get just o to be remapped without a recursive error. But a reasonable compromise you might consider from this answer is to use oo for new line. This leaves single o intact if you do want to go into insert mode.

nmap oo o<Esc>
nmap OO O<Esc>
  • The recursive error you're seeing is because you're using nmap instead of nnoremap (which will call the original o instead of recursively calling your mapping. I'm afraid your solution will have the same problem as the OP's original mapping.
    – Rich
    Feb 26 at 14:56
  • nnoremap o :norm o<cr> was what I was originally going for, but I get that "command too recursive" error.
    – wxz
    Feb 26 at 15:09
  • That's because :norm is then invoking your o mapping recursively. Add an exclamation point :norm! to avoid the recursion. Nonetheless, none of your solutions will solve the OP's issue, which is that they inadvertently had 'compatible' set. See the discussion in :chat where @AndrewHo-Lee and @filbranden figured this out.
    – Rich
    Feb 26 at 17:23

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.