问题
...how would you do it?
task for extra credit: figure out a way to force vim to refresh the cursor position immediately after exiting insert mode
回答1:
Although I would not recommend changing default cursor mechanics, one way of achieving the behavior in question is to use the following Insert-mode mapping.
:inoremap <silent> <Esc> <Esc>`^
Here the Esc key is overloaded in Insert mode to additionally run the ``^` command which moves the cursor to the position where it was the last time Insert mode was stopped. Since in this mapping it is executed immediately after leaving Insert mode with Esc, the cursor is left one character to the right as compared to its position with default behavior.
Unlike some other workarounds, this one does not require Vim to be compiled
with the +ex_extra
feature.
回答2:
Although there are tricks to deal with this (such as the ESC mappings mentioned in the previous two posts), there's no consistent way to do this. The reason is that there is no way to determine the method that was used to enter insert mode. Specifically, given the string abcDefg
with the cursor on the D
:
If you press i, the insert mode location will be between the
c
andD
. A normal ESC will put the cursor onc
;<C-O>:stopinsert<CR>
(or the backtick method) will put the cursor onD
.If you press a, the insert mode location will be between the
D
ande
. A normal ESC will put the cursor onD
;<C-O>:stopinsert<CR>
will put the cursor one
.
If you REALLY want to do this, you could fudge it with something like this:
let insert_command = "inoremap <ESC> <C-O>:stopinsert<CR>"
let append_command = "iunmap <ESC>"
nnoremap i :exe insert_command<CR>i
nnoremap a :exe append_command<CR>a
BUT: remember that this will only deal with i
and a
as methods of entry: if you use visual block mode, I
, or A
or whatever, you'll need to come up with new commands to match (and there are a lot of them). Therefore, I'd strongly recommend you don't do this.
Personally, I'd recommend getting used to the default behaviour. You can easily make it logical for i
OR logical for a
. If you change the default to logical for i
at the expense of logical for a
, you'll just get confused when you use a standard vi/vim install.
回答3:
inoremap <silent> <Esc> <C-O>:stopinsert<CR>
in your .vimrc
回答4:
I do believe the proper way to do this is
au InsertLeave * call cursor([getpos('.')[1], getpos('.')[2]+1])
回答5:
Based on Nathan Neff's comment, the best approach I've found is
autocmd InsertLeave * :normal `^
set virtualedit=onemore
autocmd
moves the cursor back to where it was when insert mode ended (i.e. one forward compared to the default).
virtualedit
makes it act consistently at end of line (so it can be one forward of the last character on the line).
回答6:
What about:
:imap <Esc> <Esc><Right>
来源:https://stackoverflow.com/questions/2295410/prevent-cursor-from-moving-back-one-character-on-insert-mode-exit