Vim remapping weirdness
When I start with a blank .vimrc in ~, and add imap jj <esc开发者_JAVA百科>
to it, everything works fine at first (i.e. typing jj exits from insert mode -> normal mode). But as I add more to my .vimrc, eventually typing jj will skip the cursor ahead some seemingly random, short distance (a couple of words or a couple of lines) in addition to entering normal mode. I haven't pinned it down to any specific thing I've added to .vimrc -- it just at some point goes from working correctly to malfunctioning. Then, if I delete the .vimmrc file, start over again with a new .vimrc with just that mapping, the weird jumping goes away.
Any clues as to what's going on?
edit:
my complete ~/.vimrc
set nocompatible
inoremap jj <esc>
That's it. I've renamed /etc/vim/vimrc & /usr/share/vim/vimrc to vimrc.0, and similarly moved ~/plugins & ~/ftplugins.
:imap
& :inoremap
both show just i jj * <Esc>
I know this has been here for a long time, but here goes anyway.
You don't happen to have any comments after the mapping for the "jj" do you? Like:
ino jj <Esc> " map jj to escape key
Perhaps with a bunch of spaces after the closing greater than to align comments to make them pretty...
If so I would move the comment above the mapping so vim doesn't think you're typing an escape followed by a bunch of spaces or tabs (which would move your cursor ahead in seemingly random ways depending on where you were).
" map jj to escape key
ino jj <Esc>
To check and see if the problem is indeed the formatting of your vimrc file you can just type this in at the ex prompt and see if it fixes the problem for the current session.
ino jj <Esc>
If after doing that your "jj" mapping works as expected then it's probably some variation of what I'm describing. To verify the formatting of your vimrc file do a search for whitespace characters by typing the code below into the ex command prompt (set hlsearch turns on highlighting and /\s searches for whitespace. The pipe executes them in sequence.).
set hlsearch | /\s
On a side note. One nice trick that you can do is to follow the mapping with a single space and a comment. That would put you over top of the position you were editing.
ino jj <Esc> " map jj to escape key
One complaint of newbie users is that they don't like how the position moves to the character to the left of where they're editing when they hit escape. Like if you're in insert mode and your cursor is between the "h" and "r" characters like this:
one two th|ree
And you hit escape you'll be in normal mode over top of the "h". The trick with the space after the mapping will put you over top of the "r".
You may want to use inoremap
instead of imap
. This will prevent further substitution of your mapping by other mappings. From the vim wikia:
"For simplicity, we will show :imap
but careful Vimmers are in the habit of using :inoremap
which does not attempt to interpret the result of the mapping (with the :imap
command, the result is scanned to see whether it contains another mapping)."
If another mapping contains j however, this could still crop up, even when using inoremap
.
Another thing to consider in your debugging is the presence of a global vimrc
file, often in /etc/vimrc
It's possible that you have a mapping in the global file that conflicts with your user's ~/.vimrc
精彩评论