VIM - How to map Shift-Enter

后端 未结 5 1755
广开言路
广开言路 2020-11-27 16:30

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

nmap  o         


        
相关标签:
5条回答
  • 2020-11-27 16:53

    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, 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
    
    0 讨论(0)
  • 2020-11-27 16:55

    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!

    0 讨论(0)
  • 2020-11-27 17:03

    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.

    0 讨论(0)
  • 2020-11-27 17:05

    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.

    0 讨论(0)
  • 2020-11-27 17:10

    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>
    
    0 讨论(0)
提交回复
热议问题