开发者

vim moving with hjkl in long lines (screen lines)

开发者 https://www.devze.com 2023-02-09 18:33 出处:网络
The highlighted text is the array in which I开发者_如何学Go want to move. I have to press g before pressing j to move a line down. Is there some mapping for my .vimrc that I can just use hjkl to mov

vim moving with hjkl in long lines (screen lines)

The highlighted text is the array in which I开发者_如何学Go want to move. I have to press g before pressing j to move a line down. Is there some mapping for my .vimrc that I can just use hjkl to move in screen lines without pressing g every time.

Thanks for your help Matthias


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 :


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("$")


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号