iamnotsam
iamnotsam

Reputation: 10380

Paste to end of the file in VIM without moving cursor

In a text document, I'm [visually or otherwise] selecting several lines, cutting them with d... I'd like to paste these lines to the end of the file without moving the cursor. Is there a relatively simple way to do this?

Upvotes: 3

Views: 749

Answers (4)

Michael Berkowski
Michael Berkowski

Reputation: 270599

You can define a mapping which marks the current location, pastes at the end of the buffer using :$put then returns to the original cursor location using the mark.

This works because :put allows a line number prefix (the last line being representable as $). From :help put:

:[line]pu[t] [x]        Put the text [from register x]

This would map it to <leader>p:

:nnoremap <leader>p :mark '<cr>:$put<cr>`'

It sets the ' mark at the cursor, pastes at the end, then returns to the ' mark with `

Upvotes: 2

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

Use the Implicit Mark from Last Jump

You can use the implicit mark (e.g. ') to return your cursor to the location it occupied just before the last jump. For example:

Gp''

This will (G)o to the end of the file, (p)aste the contents after the last line, and then return to your position at the time you typed G.

Upvotes: 4

Johan
Johan

Reputation: 20763

Depends on how you mean "without moving the cursor".

This will paste at the bottom of the current file and then allow you to continue where you cut the lines from.

  1. split window with :split
  2. move to bottom with shift+g
  3. paste with p
  4. close the duplicate split view (zz or :q)

If you dont like the split view, you can use the ctrl+o to jump back after G

  1. move to bottom with shift+g
  2. paste with p
  3. jump back with ctrl+o

Upvotes: 1

Peter Rincker
Peter Rincker

Reputation: 45087

There are a few ways:

Marks

Set a mark, do your paste, then jump back to the mark

m':$pu<cr>``

Visual mode

Visually select your lines, copy them, append, and then restore visual selection (optionally delete)

y:$pu<cr>gv

Append to the file

Visually select your lines, use :w to append to the file, and then reload the file. (Note: will move the cursor to the start of the visually selected lines)

:w >><cr>:e!

Create your own command/mapping

You can create your own command and/or mapping that will use winsaveview() and winrestview() to append then restore the cursor.

Upvotes: 2

Related Questions