Can't Map <S-CR> in Vim

 ̄綄美尐妖づ 提交于 2019-12-05 11:30:28

The problem here is with the terminal emulation. Most terminals cannot distinguish between non-printing keys [1] and those keys combined with modifier keys.

However, you can still make the desired combination work if your terminal application has the ability to remap key combinations (as iTerm2, for example, does). Map the terminal application's combination to some Unicode character you'll never use, then map that key in Vim to the desired effect, and you can get around this limitation.

For this example, in iTerm2, open the Keys Preferences pane, add a Global Shortcut key, input shift and return, give it an action of Set Text, and then put ✠ (a Maltese Cross, but you could use any random unlikely-to-be-used Unicode character) as its value. In your .vimrc, add these lines:

" Map ✠ (U+2720) to <Esc> as <S-CR> is mapped to ✠ in iTerm2.
inoremap ✠ <Esc>

Or:

inoremap <S-CR> <Esc>
" Map ✠ (U+2720) to <S-CR>, so we have <S-CR> mapped to ✠ in iTerm2 and
" ✠ mapped back to <S-CR> in Vim. 
imap ✠ <S-CR>

Entering <S-CR> in Vim in iTerm2 will now ultimately result in <Esc> in Vim as desired.

[1]: E.g. space, tab, enter, delete, control, alt, escape.

That's because for iTerm <S-CR> is the same as <CR>, type Ctrl+V Return then Ctrl+V Shift+Return and you'll see that the same character is inserted in both cases.

So, when you type <S-CR> Vim gets <CR> and your mapping is not triggered.

MacVim is the equivalent of GVim: a GUI for Vim. You don't run MacVim through iTerm. You either run the GUI version (MacVim.app) OR the CLI version ($ vim).

You can launch the GUI from the CLI but iTerm's settings won't interfere in any way with MacVim's settings.

In MacVim your mapping works perfectly.

As far as I know all or most "terminals" treat the same as . Maybe you should try another sequence like jj?

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