Search code examples
linuxunixvi

How to copy (yank) from file 1 and paste (put) in file 2 in vi using buffer?


I want to know the process for copying data from file 1 and paste the data into file 2 in vi editor using a buffer.

Can you please tell me the step by step process.

How to do that?


Solution

  • From vi manual:

    6.5.6.3 Using Named Buffers

    To repeatedly insert a group of lines in various places within a document, you can yank (or delete) the lines into a named buffer. You specify named buffers by preceding a command with double quotes (") and a name for the buffer. For example, to yank four lines into the named buffer a, type "a4yy. You can use several different buffers. For example, you might also delete text from one location and add it to several others. To delete 12 lines into the named buffer b, type "b12dd.

    To insert the text, precede the p or P command with n, where n is the named buffer. For example, to insert the lines saved in buffer b, type "bP.

    You can overwrite named buffers with new lines. The buffers are saved until you exit vi.

    When you use named buffers, you can safely delete and yank other text without affecting the lines you have already saved in the named buffers -- unless, of course, you purposely overwrite the named buffer.