Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Map <S-CR> in Vim

Tags:

vim

macos

macvim

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!

like image 663
JoeQuery Avatar asked Mar 22 '11 08:03

JoeQuery


People also ask

What is CR in vim mapping?

The <CR> in vim mappings is the carriage return usually the Enter on your keyboard.

What does CR stand for on a map?

A county highway (also county road or county route; usually abbreviated CH or CR) is a road in the United States and in the Canadian province of Ontario that is designated and/or maintained by the county highway department.

What does CR mean terminal?

CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.

What does C mean vim?

In Vim (and Emacs) documentation, C- and M- stand for Ctrl and Meta (i.e. Alt ) respectively. So C-O is Ctrl O . Follow this answer to receive notifications.


2 Answers

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.

like image 59
Tadhg Avatar answered Sep 27 '22 18:09

Tadhg


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.

like image 40
Raimondi Avatar answered Sep 27 '22 18:09

Raimondi