4
votes

When vim wraps long lines between words regular movements like j and k will jump from one physical line to the next. Mappings like "nnoremap j gj" as suggested here will do the trick of moving the cursor by display lines instead of physical lines.

There's at least one problem with this approach though. For example, dj will delete two physical lines instead of two display lines.

Is there a way to fix this?

3

3 Answers

6
votes

dd and yy:

:nnoremap dd g0dg$
:nnoremap yy g0yg$
5
votes

Yes. Just use

:noremap j gj

instead of its version with two "n"-s. Unless you want the mapping work in visual mode as well, you may achieve the desired behavior with two mappings:

:nnoremap j gj
:onoremap j gj

Simulation of dd behavior is quite tricky, and I couldn't do this. This command means "delete current line linewise and put it in a linewise register". The following was my closest attempt, but it requires much trickier text processing:

:nnoremap dd g^dg$:call setreg(v:register,'','al')<BR>

(again, this doesn't work, but may point you to a helpful direction).

You may also be interested in the relevant help section:

:h map-modes
2
votes

If you want dd and yy to only work on display lines, you would need to use the following mappings:

:nnoremap dd dg$
:nnoremap yy yg$
:nnoremap D dg$
:nnoremap Y 0yg$