Vim: exchange two remote lines in one move

(NB: my first post)

In a Vim file, how can I swap - in one move - line 15 and line 33 (for example)?

+4
source share
4 answers

Two ways I can think of. With Vim, probably more!

:33 | delete | 15 | put | 15 | delete | 32 | put 

... or...

 13ggdd15ggPjdd33ggP 

... which is less than keystrokes, but a little less clear when recording!

+4
source

The fastest way is moving the lines:

:33m 15|15m 33

Move line 33 below line 15, then move line 15 below line 33.

The best thing to do is to "move the number with a low number first", otherwise you will have to adjust the offsets:

:15m 33|32m 14

+8
source

I found this command in the fastest way if I'm in a file:

 :15mo33 
+3
source

I often use the following:

 " Tip #470 : Piet Delport & Anthony (ad_scriven) vnoremap <silent> g" <esc>:call <sid>SwapVisualWithCut()<cr> function! s:SwapVisualWithCut() normal! `.`` if line(".")==line("'.") && col(".") < col("'.") let c = col('.') normal! gvp```] let c = col('.') - c normal! `` :silent call cursor(line("."),col(".")+c) normal! P else normal! gvp``P endif endfunction 

The idea is to delete something anywhere, then go to the selection of characters elsewhere and press g" to replace the delete characters with new ones.

+1
source

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


All Articles