在 Vim 中退出插入模式时,如何防止光标向后移动一个字符? [英] How to prevent the cursor from moving back one character on leaving Insert mode in Vim?

查看:33
本文介绍了在 Vim 中退出插入模式时,如何防止光标向后移动一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以取消上述行为?

Is it possible to cancel the said behavior?

额外任务:找出一种方法,强制 Vim 在退出插入模式后立即刷新光标位置.

A task for extra credit: Figure out a way to force Vim to refresh the cursor position immediately after exiting Insert mode.

推荐答案

虽然我不建议更改默认的游标机制,实现有问题的行为的一种方法是使用以下插入模式映射.

Although I would not recommend changing the default cursor mechanics, one way of achieving the behavior in question is to use the following Insert-mode mapping.

:inoremap <silent> <Esc> <Esc>`^

这里 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 had been the last time Insert mode was left. 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.

与其他一些变通方法不同,这个变通方法不需要 Vim使用 +ex_extra 功能编译.

Unlike some other workarounds, this one does not require Vim to be compiled with the +ex_extra feature.

这篇关于在 Vim 中退出插入模式时,如何防止光标向后移动一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆