Mapping -Arrows to selecting characters/lines

前端 未结 8 1061
再見小時候
再見小時候 2020-12-29 03:55

I started to use vim recently, but I miss the character/line selection methods from other text editors. By default vim maps ,

8条回答
  •  有刺的猬
    2020-12-29 04:34

    I've written this to be able to navigate using Alt+hjkl (and friends) and select using Alt+HJLK when both in insert, visual and normal mode.

    So the same can be applied to normal arrow keys as well

    let hjklfriends = ['h','j','k','l','w','e','b','W','E','B', 'n', 'N', 'y', 'Y', 'p', 'P']
    
    " define if using alt (it works in neovim) or Escape key.
    function! Meta(key)
      if has('nvim')
            return ""
        else
            return "" . a:key
        endif
    endfunction
    
    execute 'noremap! ' . Meta('h') . ' '
    execute 'noremap! ' . Meta('j') . ' '
    execute 'noremap! ' . Meta('k') . ' '
    execute 'noremap! ' . Meta('l') . ' '
    execute 'noremap! ' . Meta('b') . ' '
    execute 'noremap! ' . Meta('w') . ' '
    execute 'noremap! ' . Meta('e') . ' '
    
    for k in hjklfriends
      execute 'imap ' . Meta(k) . ' ' . k
    
      if k =~ '[a-z]'
        execute 'imap ' . Meta(toupper(k)) . ' v' . k
        execute 'vmap ' . Meta(toupper(k)) . ' ' . k
        execute 'nmap ' . Meta(toupper(k)) . ' v' . k
      endif
    endfor
    

提交回复
热议问题