Why does this Vim mapping behave differently depending on how Visual mode was entered?

喜夏-厌秋 提交于 2019-12-10 18:30:05

问题


This question is what prompted a previous question of mine here: Vim's '(insert) VISUAL' mode?

In that question I asked about "VISUAL" mode vs "(insert) VISUAL" mode, which is where you enter visual mode directly from insert mode. I have found a mapping that behaves differently depending on the "subtype" of visual mode, and I don't understand why:

vnoremap x <Esc>jjj
vnoremap <A-[> <C-G>ugv

The first mapping illustrates how Vim goes out of its way to make mappings to behave consistently: pressing x from "VISUAL" mode and pressing x from "(insert) VISUAL" mode will both move the cursor down 3 lines. The latter does not exit to insert mode and enter three j's. So <Esc> is not behaving differently depending on the mode, even though outside of a mapping it would behave differently from these two modes.

The second mapping illustrates how some maps behave inconsistently. If you highlight a word and press ALT+[ then the word is replaced with ugv if done from "VISUAL" mode but nothing happens if done from "(insert) VISUAL" mode. Presumably this is because Vim is executing the normal mode commands ugv for 'undo' and 'reselect-visual'.

This is a strange inconsistency. It is a minimal example I ran into when debugging my mappings, and would like to know why it works this way.


回答1:


I can reproduce your examples and I think your analysis is right.

When I type <C-G>ugv from (insert) visual mode, I get the expected results: <C-G> switches to (insert) select mode, ugv replaces the selection with those characters. A mapping should behave in the same way.

I guess this is a bug; the entire (insert) submode is quite special, not frequently used, and therefore prone to bugs. You could delve into the source code yourself, or raise this issue on the vim_dev mailing list. Due to the obscurity of the issue, it may be hard to garner much interest from the devs, though.



来源:https://stackoverflow.com/questions/16221213/why-does-this-vim-mapping-behave-differently-depending-on-how-visual-mode-was-en

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!