Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move entire line up and down in Vim

Tags:

vim

vi

In Notepad++, I can use Ctrl + Shift + Up / Down to move the current line up and down. Is there a similar command to this in Vim? I have looked through endless guides, but have found nothing.

If there isn't, how could I bind the action to that key combination?

Edit: Mykola's answer works for all lines, apart from those at the beginning and end of the buffer. Moving the first line up or the bottom line down deletes the line, and when moving the bottom line up it jumps two spaces initially, like a pawn! Can anyone offer any refinements?

like image 949
user4812 Avatar asked Apr 12 '09 14:04

user4812


People also ask

How do I change a whole line in Vim?

Use c$ or just C to quickly change from the cursor to the end of a line, cc to change an entire line, or cis for a sentence. The standard change word command requires you to type cw , then a new word, then press Escape.

How do I move multiple lines in vi?

Steps. Press “<SHIFT> + v” to enter VISUAL LINE mode. Select the text you wish to indent but using either the cursor keys or the “j” and “k” keys. To indent press “<SHIFT> + dot” (> character).


2 Answers

If I want to swap one line with the line above I usually do the following

ddkP 

Explanation

  • dd will delete the line and add it to the default register.
  • k will move up a line (j would move down a line)
  • P will paste above the current line
like image 197
JaredPar Avatar answered Oct 05 '22 11:10

JaredPar


Assuming the cursor is on the line you like to move.

Moving up and down: :m for move

:m +1 - moves down 1 line

:m -2 - move up 1 lines

(Note you can replace +1 with any numbers depending on how many lines you want to move it up or down, ie +2 would move it down 2 lines, -3 would move it up 2 lines)

To move to specific line

:set number - display number lines (easier to see where you are moving it to)

:m 3 - move the line after 3rd line (replace 3 to any line you'd like)

Moving multiple lines:

V (i.e. Shift-V) and move courser up and down to select multiple lines in VIM

once selected hit : and run the commands above, m +1 etc

like image 30
Serg Avatar answered Oct 05 '22 12:10

Serg