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.
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 Preferences → Profiles → Keys → [+] (Add)
For all profiles open Preferences → Keys → [+] (Add)
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, under key_bindings
, add following to your ~/.config/alacritty/alacritty.yml
:
- { key: Return, mods: Shift, chars: "\x1b[13;2u" } - { key: Return, mods: Control, chars: "\x1b[13;5u" }
Kitty, in ~/.config/kitty/kitty.conf
:
map shift+enter send_text all \x1b[13;2u map ctrl+enter send_text all \x1b[13;5u
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.
<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>
:nmap
:You should use :noremap
; it makes the mapping immune to remapping and recursion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With