Vim : swap 2 distant lines in one move
(NB : my first post)
In a Vim file, how may I swap - in one move - line 15 and line 33 (e.开发者_运维知识库g.) ?
Fastest way is to move lines:
:33m 15|15m 33
Move line 33 below line 15, then moves line 15 below line 33.
It's best to do it 'move high-number below low-number' first, otherwise you have to adjust offsets:
:15m 33|32m 14
Two ways I can think of. With Vim there are probably more!
:33 | delete | 15 | put | 15 | delete | 32 | put
...or...
13ggdd15ggPjdd33ggP
...which is fewer keystrokes but a little less comprehensible when written down!
I've found this command to be the fastest way if I'm in the file:
:15mo33
I'm frequently using 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 select characters elsewhere, and hit g"
to swap the delete characters with the newly selected ones.
精彩评论