I'm using gnome-terminal, which uses 7-bit input. This means Vim's meta/alt keys, e.g. <M-f> don't work without some extra configuration. I know how to set that up for normal keys, like F, but not for Enter.

When I press Alt+F the terminal sends ^[ F, where the ^[ is a single Escape byte (\x1b). I can make <M-f> usable by doing:

set  <F13>=^[f
map  <F13> <M-f>
map! <F13> <M-f>

How can I do this for the Enter key (<CR>)?

  • 2
    did you try set <a-cr>=^[^M ?
    – Mass
    Mar 21 '18 at 15:39
  • 1
    @Mass I did not, until you mentioned it, and it works! You should submit that as an answer and I'll mark it as such. Thanks!
    – ivan
    Mar 21 '18 at 16:04
  • @Mass The next question is how to map Alt+Shift+Enter...
    – ivan
    Mar 21 '18 at 16:10
  • It looks like gnome-terminal sends the same byte sequence for Alt+Shift+Enter as for Alt+Enter so I won't be able to use <M-S-CR>, at least not on this terminal. On a more configurable term like Alacritty I can though.
    – ivan
    Mar 21 '18 at 22:01

On many terminals (specifically the ones that send alt-modified keys as escape then key), you can map alt+enter in the same way as many other alt combinations,

set <a-cr>=^[^M

This is two characters- escape then control m. Alternatively,

execute "set <a-cr>=\<esc>\<cr>"

Then you can then use <a-cr> in maps;

nnoremap <a-cr> something

There is no standard way to map alt+shift+enter, or indeed any other alt+shift+ combination. Also, if your terminal supports it, you will have to use one of the unused F keys, e.g. <f13> through <f37> on linux (since there's no <s-c-cr> key code in vim) set to your terminal's proprietary escape sequence.

set <f25>=...

For example, my terminal sends escape, escape, then OM (four characters):

set <f25>=^[^[OM
  • 1
    There are even more unused Function keys beyond F26, though they're undocumented, They seem to go up to 37.
    – ivan
    Mar 22 '18 at 3:05
  • 1
    Good catch. Indeed, the number 37 is pretty mysterious.
    – Mass
    Mar 22 '18 at 3:18
  • For neovim the command should be map <a-cr>, the set ... semantic will output error: Unknown option: set.
    – job_start
    Apr 4 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.