59

I am trying to customize the behaviour of Enter key in Normal mode in Vim. Here is my .vimrc:

nmap <CR> o<Esc>
nmap <S-CR> i<CR><Esc>

I am trying to make Enter simply append a newline after the current line when pressed. However, when the Shift-Enter combination is pressed, I want to break the current line at the cursor.

The latter does not work. Whenever I press Shift-Enter it just appends a line without breaking it at the cursor.

2
  • 3
    Alternative proposal: Map i<CR><ESC> to <C-J>. That results in a nice <S-J> and <C-J> duality for splitting and joining lines. And use unimpaired’s ]<space> for adding empty lines (as proposed by Peter Rickner).
    – Chronial
    May 3, 2013 at 13:58
  • I was attempting to do the same =) This is what I ended up doing stackoverflow.com/a/26861286/226255
    – Abdo
    Nov 11, 2014 at 9:56

6 Answers 6

79

I managed to correct my terminal key-code for Shift+Enter by sending the key-code Vim apparently expects. Depending on your terminal, (Adding Ctrl+Enter as a bonus!)

iTerm2

For a single Profile open PreferencesProfilesKeys[+] (Add)
For all profiles open PreferencesKeys[+] (Add)

  • Keyboard shortcut: (Hit Shift+Enter)

  • Action: Send Escape Sequence

  • Esc+ [13;2u

    Repeat for Ctrl+Enter, with sequence: [13;5u

urxvt, append to your .Xresources file:

URxvt.keysym.S-Return:     \033[13;2u
URxvt.keysym.C-Return:     \033[13;5u

Alacritty, add following to your ~/.config/alacritty/alacritty.toml:

[[keyboard.bindings]]
chars = "\u001B[13;2u"
key = "Return"
mods = "Shift"

[[keyboard.bindings]]
chars = "\u001B[13;5u"
key = "Return"
mods = "Control"

Kitty, in ~/.config/kitty/kitty.conf:

map shift+enter send_text all \x1b[13;2u
map ctrl+enter send_text all \x1b[13;5u

Windows Terminal

Add these to the actions array of the JSON config (via Settings):

        {
            "keys": "shift+enter",
            "command": { "action": "sendInput", "input": "\u001b[13;2u" }
        },
        {
            "keys": "ctrl+enter",
            "command": { "action": "sendInput", "input": "\u001b[13;5u" }
        }
14
  • 3
    This should be the accepted answer! (I only needed to create the iTerm mapping for Shift+Enter)
    – Sheharyar
    Mar 16, 2019 at 7:25
  • 2
    This is awesome. The mappings in Alacrrity is always so confusing. Thanks for sharing! Oct 30, 2019 at 19:53
  • 7
    On tmux 3.2a (at least in macOS and FreeBSD) you will also need to add in ~/.tmux.conf lines with bind -n S-Enter send-keys Escape "[13;2u" etc. Otherwise tmux will not pass through the codes to neovim.
    – Miguel
    Dec 28, 2021 at 11:59
  • 2
    How do you find which key code vim expects for some given keybind? @rafi
    – StaticESC
    Nov 7, 2022 at 9:15
  • 2
    These mappings correspond to the CSI u codes. You can refer to them here: leonerd.org.uk/hacks/fixterms. So <ESC>[13;2u means: - <ESC>[: CSI input - 13 represents \r according to the ASCII table: en.wikipedia.org/wiki/ASCII#Control_code_chart - ;2u represents the Shift modifier May 26, 2023 at 6:14
50

Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today in the terminal version of Vim (<S-CR> should work in GVIM on all platforms, and in the Windows console Vim). This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.


Note on mapping <CR>:

If you map <CR> in normal mode, it'll interfere with selection of history items in the command-line window and with jumping to error under cursor in quickfix/location list windows! (Unless you add the following:)

:autocmd CmdwinEnter * nnoremap <CR> <CR>
:autocmd BufReadPost quickfix nnoremap <CR> <CR>

Note on :nmap:

You should use :noremap; it makes the mapping immune to remapping and recursion.

2
  • 3
    Thanks for the :nmap vs :noremap, very useful to know. I'll probably bind it to something else. May 3, 2013 at 16:44
  • Hey, thanks a lot!. I just realized why I was getting that weird behaviour (I had set nnoremap <cr> : =P). Btw, how did you found that? Dec 5, 2014 at 1:36
15

I also wanted to map <S-CR> and found that I couldn't get it to work in CLI mode until I added a second mapping using Ctrl+V then <Shift+Enter> for the mapped keystroke. The <S-CR> mapping is still needed for GVIm to work as expected, however. This would render the your .vimrc snippet as follows:

nnoremap <CR> o<Esc>
nnoremap <S-CR> i<CR><Esc> " Needed for GVIm
nnoremap ^[0M i<CR><Esc>   " Needed for CLI VIm (Note: ^[0M was created with Ctrl+V Shift+Enter, don't type it directly)

I tested this on Ubuntu 12.04. Happy Vimming!

1
  • Didn't work on my Git Bash (Windows) nor Linux Mint's terminal Vim. Worked on both GVims.
    – awvalenti
    Jun 24, 2021 at 17:03
9

You can't map <S-CR> in CLI Vim, no matter how hard you try, because Vim can't distinguish <S-CR> from <CR>.

You must find another mapping or stick with GVim/MacVim.

edit

Some terminal emulators, like iTerm.app or Terminal.app on Mac OS X, allow you to set up shortcuts to send specific characters sequences to the shell. If you have that possibility it may be worth a try but you'll quickly get used to a platform-specific gyzmo that can't be ported so, well… I don't really recommend it.

2
  • With iTerm it's probably possible to map any crazy combination you want, and use custom escape sequences if you run out of the standard ones. And with e.g. PuTTY you can compile in your key maps, like I have done with F10 and Backspace.
    – Steven Lu
    Aug 9, 2013 at 21:49
  • This answer only provides most of the same information as the two most upvoted answers, and it is older than both. Apr 3, 2022 at 22:38
6

Ingo Karkat and romainl are 100% correct. However what you are asking is common so I want to give you some options.

I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [<space> and ]<space> which create blank lines above and below the current line respectively. Unimpaired also provides nice mappings for moving through the quickfix list, buffer list, option toggling, and many others. See :h unimpaired for more.

If you do not want to use unimpaired plugin but like the mappings below are some quick mappings to put in your ~/.vimrc file:

nnoremap <silent> [<space>  :<c-u>put!=repeat([''],v:count)<bar>']+1<cr>
nnoremap <silent> ]<space>  :<c-u>put =repeat([''],v:count)<bar>'[-1<cr>
2
  • I took the liberty to correct "black lines" :-) Oh, and +1 on the unimpaired recommendation. In fact, I have my own alternative in the LineJuggler plugin, as I don't like it how Tim put together a jumble of unrelated functionality, just grouped by common mapping prefixes. May 3, 2013 at 18:15
  • Thank you for your changes and mentioning your LineJuggler plugin. I like to have plenty of plugins to choose from. Cheers! May 3, 2013 at 18:30
0

You can add a key map in the insert mode.

:inoremap <S-CR> <ESC>o

This works for me in the mingw terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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