vim moving with hjkl in long lines (screen lines)

前端 未结 4 1666
孤独总比滥情好
孤独总比滥情好 2020-12-15 06:34

\"enter

The highlighted text is the array in which I want to move. I have to press g b

相关标签:
4条回答
  • 2020-12-15 06:48

    This is a great idea for when editing long text files, like latex documents. But it's not such a good idea when editing code, as it can screw up some macros (as jelera notes on Ned's answer). My compromise is to only enable it when in insert (and replace) modes, and not for anything else (I'm not aware of any macros that I use in insert mode that use navigation keys):

    " move on soft lines in insert mode
    inoremap <Down> <C-o>g<Down>
    inoremap <Up> <C-o>g<Up>
    

    (adapted from this answer)

    0 讨论(0)
  • 2020-12-15 06:49

    You can simply remap j and k (for example) to gj and gk:

    " map j to gj and k to gk, so line navigation ignores line wrap
    nmap j gj
    nmap k gk
    
    0 讨论(0)
  • 2020-12-15 06:52

    You can remap the j and k keys (not sure if you really need h and l..)

    :map j gj
    :map k gk
    

    Once you tried and liked them, add them to your .vimrc without the leading :

    0 讨论(0)
  • 2020-12-15 07:08

    I use the following snippet that helps with all forms of navigating, including things like $ to end of line and such.

    " mapping to make movements operate on 1 screen line in wrap mode
    function! ScreenMovement(movement)
       if &wrap
          return "g" . a:movement
       else
          return a:movement
       endif
    endfunction
    onoremap <silent> <expr> j ScreenMovement("j")
    onoremap <silent> <expr> k ScreenMovement("k")
    onoremap <silent> <expr> 0 ScreenMovement("0")
    onoremap <silent> <expr> ^ ScreenMovement("^")
    onoremap <silent> <expr> $ ScreenMovement("$")
    nnoremap <silent> <expr> j ScreenMovement("j")
    nnoremap <silent> <expr> k ScreenMovement("k")
    nnoremap <silent> <expr> 0 ScreenMovement("0")
    nnoremap <silent> <expr> ^ ScreenMovement("^")
    nnoremap <silent> <expr> $ ScreenMovement("$")
    
    0 讨论(0)
提交回复
热议问题