Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Swapping in Vim

Tags:

vim

vi

editor

Anyone know of any tricks in vim to easily swap two lines of code? I know that swapping adjacent lines is trivial, but let's say I wanted to swap line 23 with line 52. Is that achievable?

like image 725
user1427661 Avatar asked Mar 08 '13 14:03

user1427661


4 Answers

yes!

:23m52|51m22 

then press Enter

it is also easy to write a function to do it.

like image 127
Kent Avatar answered Sep 23 '22 18:09

Kent


My LineJuggler plugin has normal and visual mode mappings ]E and [E (and others, similar to the popular unimpaired plugin) to swap the current line / selection with [count] lines below / above.

I think a :[range]Swap {range} command would also be helpful; I'm working on that.

Edit: Here it is: LineJugglerCommands plugin.

like image 35
Ingo Karkat Avatar answered Sep 21 '22 18:09

Ingo Karkat


If you have a visual selection when you paste, it will put the selection into the default buffer after pasting. So you would do 23Gdd51GVp23GP

like image 42
Kevin Avatar answered Sep 23 '22 18:09

Kevin


user move command to move lines. e.g., swap line 23 and line 52:

:23m52 | 52m23

type :help move to get help usage of move, it may move a range of lines :-)

like image 28
Peixu Zhu Avatar answered Sep 23 '22 18:09

Peixu Zhu