Code Like Zell

Vim single line move when :set wrap is on

Posted 25 January 2009.

If you are editing a long paragraph with word wrap on, you may find that ‘j’ and ‘k’ jump farther than you would expect. This is because ‘j’ is moving to the next real line instead of the next wrapped line. To make vim behave more intuitively—for me, at least—put this in your vim config file (~/.vimrc in my case):

  function! ScreenMovement(movement)
     if &wrap
        return "g" . a:movement
     else
        return a:movement
     endif
  endfunction
  onoremap   j ScreenMovement("j")
  onoremap   k ScreenMovement("k")
  onoremap   0 ScreenMovement("0")
  onoremap   ^ ScreenMovement("^")
  onoremap   $ ScreenMovement("$")
  nnoremap   j ScreenMovement("j")
  nnoremap   k ScreenMovement("k")
  nnoremap   0 ScreenMovement("0")
  nnoremap   ^ ScreenMovement("^")
  nnoremap   $ ScreenMovement("$")

Thanks to rick_h in #vim

All CLZ Posts

Hide this
Joypad Banner