Mapping -Arrows to selecting characters/lines

前端 未结 8 1059
再見小時候
再見小時候 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:26

    I found another solution that is easier to execute. The command ':behave mswin' does all that is needed to use shift plus cursor keys to select text. Works from any mode. It also supports Cmd-c, Cmd-v and Cmd-x. It works in MacVim but I did not try other platforms.

    0 讨论(0)
  • 2020-12-29 04:28

    Slightly different from progo's answer - this gives the same feel as mac apps normally have:

    nmap <S-Up> v<Up>
    nmap <S-Down> v<Down>
    nmap <S-Left> v<Left>
    nmap <S-Right> v<Right>
    vmap <S-Up> <Up>
    vmap <S-Down> <Down>
    vmap <S-Left> <Left>
    vmap <S-Right> <Right>
    

    The differences being switch to visual mode instead of visual line mode, and not losing the initial up/down etc keystroke.

    0 讨论(0)
  • 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 "<A-" . a:key . ">"
        else
            return "<Esc>" . a:key
        endif
    endfunction
    
    execute 'noremap! ' . Meta('h') . ' <left>'
    execute 'noremap! ' . Meta('j') . ' <down>'
    execute 'noremap! ' . Meta('k') . ' <up>'
    execute 'noremap! ' . Meta('l') . ' <right>'
    execute 'noremap! ' . Meta('b') . ' <C-Left>'
    execute 'noremap! ' . Meta('w') . ' <C-Right>'
    execute 'noremap! ' . Meta('e') . ' <C-Right>'
    
    for k in hjklfriends
      execute 'imap ' . Meta(k) . ' <C-o>' . k
    
      if k =~ '[a-z]'
        execute 'imap ' . Meta(toupper(k)) . ' <C-o>v' . k
        execute 'vmap ' . Meta(toupper(k)) . ' ' . k
        execute 'nmap ' . Meta(toupper(k)) . ' v' . k
      endif
    endfor
    
    0 讨论(0)
  • 2020-12-29 04:36

    It is definitely recommended that you don't remap this feature. Simply switching to visual mode and using v and the arrow keys is a better idea. V will select the entire line, v$ will select to the end of the line and vw will select the next word. There are many more commands you can use to select different lines and words. Learning these commands will not only be useful for selecting but also useful for editing your files more efficiently.

    0 讨论(0)
  • 2020-12-29 04:38

    This mapping keeps insert mode during selection (visual mode) and it starts on the correct position. You can also select a word to the left or right using Ctrl-Shift-Left/Right (if your terminal supports it):

    " Select with shift + arrows
    inoremap    <S-Left>              <Left><C-o>v
    inoremap    <S-Right>             <C-o>v
    inoremap    <S-Up>                <Left><C-o>v<Up><Right>
    inoremap    <S-Down>              <C-o>v<Down><Left>
    imap        <C-S-Left>            <S-Left><C-Left>
    imap        <C-S-Right>           <S-Right><C-Right>
    vnoremap    <S-Left>              <Left>
    vnoremap    <S-Right>             <Right>
    vnoremap    <S-Up>                <Up>
    vnoremap    <S-Down>              <Down>
    
    " Auto unselect when not holding shift
    vmap        <Left>                <Esc>
    vmap        <Right>               <Esc><Right>
    vmap        <Up>                  <Esc><Up>
    vmap        <Down>                <Esc><Down>
    

    This may be useful for quickly selecting small parts when you're in insert mode but I recommend using the default commands for selecting larger parts.

    0 讨论(0)
  • 2020-12-29 04:40

    I completed @escrafford mapping with insert mode's ones:

    " shift+arrow selection
    nmap <S-Up> v<Up>
    nmap <S-Down> v<Down>
    nmap <S-Left> v<Left>
    nmap <S-Right> v<Right>
    vmap <S-Up> <Up>
    vmap <S-Down> <Down>
    vmap <S-Left> <Left>
    vmap <S-Right> <Right>
    imap <S-Up> <Esc>v<Up>
    imap <S-Down> <Esc>v<Down>
    imap <S-Left> <Esc>v<Left>
    imap <S-Right> <Esc>v<Right>
    

    Also mapping usual copy/cut/paste like this you can return to insert mode after select+copy, for example.

    vmap <C-c> y<Esc>i
    vmap <C-x> d<Esc>i
    map <C-v> pi
    imap <C-v> <Esc>pi
    imap <C-z> <Esc>ui
    

    Now you can start a shift+arrow selection from any mode, then C-c to copy, and then C-v to paste. You always end in insert mode, so you have also C-z to undo.

    I think this approaches more to the 'expected standard' behaviour for a text editor yu are asking for.

    0 讨论(0)
提交回复
热议问题