Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I swap two lines in vim?

Tags:

vim

I have this:

pick 887b66f add 222 Dziewiecsil to flowers new title
pick dc331cb new name of beginning commit

And I want to get this:

pick dc331cb new name of beginning commit
pick 887b66f add 222 Dziewiecsil to flowers new title

Is there a way to do this in a quick way using keyboard shortcuts?

like image 552
Bartłomiej Semańczyk Avatar asked Sep 26 '22 14:09

Bartłomiej Semańczyk


People also ask

How to edit a file using line numbers in Vim?

Method 1 – Using Line Numbers 1 Start by opening the file you wish to edit in Vim. 2 Next, press the ESC key to launch Vim Command Mode. 3 Enter the command: More ...

How to comment on multiple lines in Vim?

First, before you can comment on multiple lines in Vim, you need to know how to comment a single line, so let’s focus on that as our starting point. To comment out a single line in Vim, enter Visual Mode by pressing Ctrl + V. Next, navigate to the line you wish to comment out and press the C key.

How to swap the current line with the next one?

To swap the current line with the next one, type ddp while in command mode. dd - delete line (actually called cut in other editors) and save it in register Move the cursor on the first line, then type ddp. If you are not in command mode yet, hit Esc to get into command mode.

Is it possible to Swipe multiple lines at the same time?

This can become even handier when swiping multiple lines at the same time using the visual mode. This is great! much more versatile than the chosen answer! :) You could map this if you want.


2 Answers

To swap the current line with the next one, type ddp while in command mode.

  • dd - delete line (actually called cut in other editors) and save it in register
  • p - paste line from register
like image 217
Sven Marnach Avatar answered Oct 23 '22 10:10

Sven Marnach


dd deletes the current line, then you can paste the removed line using p. There's another way though using m. With m you can move lines around i.e.

:m 1 will move the current line after line 1

:m 0 will move the current line to top

:m $ will move the current line to bottom

In your example, place the cursor in the first line and type :m $

More info: http://vim.wikia.com/wiki/Moving_lines_up_or_down

like image 67
Mr. Eigenbart Avatar answered Oct 23 '22 08:10

Mr. Eigenbart