teddymills
teddymills

Reputation: 11

I would like to MOVE just one line in Vim

yy and p should copy and paste 1 line of text. But I have to go back and delete the original line.

:2,5m10 should move lines from 2 to 5 to line 10. however I need to enable :set number to see what lines I am moving

I would like to move just 1 line of text, sort of like yy+p and not use :2,3m10 to move just one line.

Is there something like mm+p ? so it copies the current line into buffer and deletes the line and you p paste it where you want ?

:3m . moves line 3 to your current line.

Above line does the function I want. Can I set a key mapping so that "mm" replaces ":3m." ? I find it easier to type. TIA

Upvotes: 1

Views: 93

Answers (1)

mattb
mattb

Reputation: 3053

What you're describing is the default behaviour when using dd -it deletes a line into the buffer and p will paste it.

So dd and p works.

If you're new to vim, then it might seem a little strange that 'yanking' (with y) and 'deleting' (with d) both copy to the buffer, given the 'cut', 'copy' and 'paste' behaviours of most other editors.

You can read more about it with :help change.txt and in particular :help registers.

Also, since you say you need to enable :set number, I wonder if you've come across :set relativenumber? This is very useful - in the example below, the numbers would look this way if the your cursor was on the line with 'demonstrate':

3 This is just
2 a small
1 example to 
0 demonstrate 
1 how relative 
2 numbers can 
3 be useful

Thus if you wanted to move the line 'a small' below the line with 'numbers can', you could use the relative line numbers to know that 2k would put the cursor on the line you want, where you'd hit dd, then you'd have this situation (the deleted line is now in the buffer:

1 This is just
0 example to 
1 demonstrate 
2 how relative 
3 numbers can 
4 be useful

Then you can do 3j to move to the 'numbers can' line, and hit p. So relative numbers are a nice way to move quickly to lines you can see. Also, just for completeness, you can use relative numbers in a similar way on the command line::-2m+3 (although I know this isn't what you're after). You can even set both relative number and set number at the same time, in which case it's like in the example above, only you have the absolute line number displayed on the current line instead of a zero.

Upvotes: 1

Related Questions