8

Is it possible for me to say the copy the contents at a specific line (x) and paste in the current line without actually needing to go to x.

Right now, I need to type :x<Enter>yy<C-o>p. I just wanted to see if this can be done more efficiently.

1
  • The :t answer is the best, but if you wanted to stay out of command mode altogether you can also use xG to jump to line x (e.g. 7G). Commented Jan 29, 2014 at 17:27

4 Answers 4

12

there is :t you can use.

for example if you are on line #77, and you want to copy line#7 below your current line, that is, #78, you just:

:7t. 

after executing this, your cursor will be on line#78, the newly "pasted" line.

read :h :t for details, you may want to know the powerful :h range as well.

another advantage of :t instead of y/Y is, you keep " register untouched.

Sign up to request clarification or add additional context in comments.

1 Comment

Fun fact: :7t. actually moves you to line 7 then moves back to the curent line.
5

The many ways to yank text in Vim

  • :copy or :t command can copy a line. e.g. :42t .
  • :t can take ranges w/ a search pattern. e.g. :?foo?t.
  • Use :yank to yank a line. e.g. :43y
  • Yank/copy multiple lines with :global command. e.g. :g/foo/y A
  • Drop a mark and then search or go to link you want to yank then jump back to the mark. e.g. ma?foo<cr>yy`ap
  • Use <c-o> similar to using marks to jump back.
  • Use g; to move to older positions in the change list. (Similar to marks)

For more help see:

:h :t :h range :h :y :h m :h g; :h ctrl-o 

Comments

4

You can use this command

:x,xy 

That will yank line 'x' without moving your cursor and then you can just paste with p

EDIT

You can shorten the command to

:xy 

That will grab line 'x'. If you want a range of lines you could do this:

:x,x+10y 

That will yank lines x - (x+10)

Comments

0

You can use the getline() function to read the line and the append() function to write it:

call append('.',getline('42')) 

You can bundle it to a command if you want:

command! -nargs=1 FetchLine call append('.',getline(<q-args>)) FetchLine 42 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.