Vim single line move when :set wrap is on

Posted: 03/29/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 (at least for me, and probably you since you are here), 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

About Me

I'm a skier, web developer, entrepreneur, freelancer, and all around stand-up guy living in Manhattan. This is me, repping one of my favorite shirts...

Follow me on twitter or send me an email.

All Posts