In Vim, why: does moving randomly corrupt my creases?

I have a small display for Alt Up and Alt Down that moves text up and down one line. I decided that this juxtaposition should be done "lose consciousness", so moving the text along one line will move it beyond the closed fold. But when I play with these comparisons, vim will ruin my creases.

Mapping:
Mappings work by calling the function :move . The argument :move is the absolute line number below which the text should be moved.

 nnoremap <A-Down> :call MoveDown_n()<CR> nnoremap <A-Up> :call MoveUp_n()<CR> function! MoveUp_n() range let pos = getpos(".") normal k let line_pos = line(".") - 1 call setpos(".", pos) execute "m".line_pos endfunction function! MoveDown_n() range let pos = getpos(".") normal jj let line_pos = line(".") - 1 call setpos(".", pos) execute "m".line_pos endfunction 

Test file:

  vim: foldmethod=marker fold A {{{ some text here }}} fold B {{{ some text here }}} fold C {{{ some text here }}} fold D {{{ some text here }}} 

Problem:
When I load a test file (with all the folds closed) and place the cursor in one of the folds, first the Alt Up and Alt Down keys seem to move the fold, as expected. But if you continue to move the folds past each other, then:

  • Folds begin to become unexpanded (“damaged”) for no apparent reason.
  • You cannot close these folds because vim cannot detect that there is a fold.
  • Saving and re-editing the file ( :e ) seems to fix the wrinkles.

Sometimes this happens with the first movement, and sometimes not. I even had test cases where the borders of the folds would be detected incorrectly, and not detected at all. For example, the fold that Vim thinks begins with the intermediate line text .

--- Edit ---
Additional tests: Sliding crease A seems to cause most problems. If you don't touch fold A, I think you can move folds B, C, and D around without encountering a problem.

If you move fold A twice (see note-1), it will damage bends B, C, and D. If you move fold A down once (see note-2), it will ruin fold B.

(note-1) place the cursor on fold A (line 5), either do :move3 :move2 , or do :move-2 , then kj , then :move-2 .
(note-2) hover over fold A (line 5), then do :move14

I noticed that after any operation :move cursor is placed in the first column of the last row of the fold. But executing the command :move-2 twice on layer A will damage the bends B, C, and D, even if you reset the cursor to the first line of addition A before executing the second command :move-2 .

If it matters: using gvim 7.3 on windows

+4
source share
1 answer

You can simply delete the whole line and insert it, and this will work out of the box (and this will be known)

 nnoremap <a-down> ddp nnoremap <a-up> ddkP 

Also at the top, the version does not work at the bottom of the file.

+1
source

Source: https://habr.com/ru/post/1481219/


All Articles