Vim: remove display lines instead of physical lines

When vim completes long lines between words, regular movements such as j and k will move from one physical line to another. Mappings such as "nnoremap j gj", as suggested here , will do the trick of moving the cursor along display lines instead of physical lines.

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

Is there any way to fix this?

+4
source share
3 answers

dd and yy:

:nnoremap dd g0dg$ :nnoremap yy g0yg$ 
+6
source

Yes. Just use

 :noremap j gj 

instead of its version with two "n" -s. If you do not want the rendering work to also work in visual mode, you can achieve the desired behavior with two mappings:

 :nnoremap j gj :onoremap j gj 

Modeling the behavior of d d is quite complicated, and I could not do it. This command means "delete the current line of the line and put it in the register of the line." The following was my closest attempt, but this requires much more complex text processing:

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

(again, this does not work , but may indicate a useful direction).

You may also be interested in the relevant help section:

 :h map-modes 
+5
source

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

 :nnoremap dd dg$ :nnoremap yy yg$ :nnoremap D dg$ :nnoremap Y 0yg$ 
+2
source

All Articles