9

Got my first Mac over the weekend, and I'm trying to get adjusted. This line in my vimrc, which worked on my windows, won't work with vim through iTerm

inoremap <S-CR> <Esc>

I'm wanting Shift-Enter to act as Escape in insert mode. I've tried using Enter and Return, but that requires me to use the Fn key on my Macbook, which is just as annoying as the escape key.

I Appreciate the help!

JoeQuery
  • 318
  • 1
  • 3
  • 14
  • 1
    Possible duplicate of [VIM - How to map Shift-Enter](http://stackoverflow.com/questions/16359878/vim-how-to-map-shift-enter) – rafi Feb 26 '17 at 14:50

3 Answers3

19

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.

Tadhg
  • 986
  • 7
  • 5
  • The accepted answer just explains the problem. This answer provides a solution. Thanks Tadhg, I learnt something new :) – justin Apr 19 '14 at 09:43
7

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.

Raimondi
  • 5,163
  • 31
  • 39
2

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?

romainl
  • 186,200
  • 21
  • 280
  • 313
  • You're correct, I meant Vim through Terminal. I edited my original post to reflect that. Thanks for the clarification! – JoeQuery Mar 22 '11 at 12:21