Cut and Paste Text in vi or vim

Cutting and pasting text in an editor is a basic requirement. This tech-recipe describes the cut and paste functions of vi and vim.

Cutting text in vi/vim is achieved with the various delete commands which are summarized below:

dd Delete current line
D Delete from cursor to end of line
d$ Delete from cursor to end of line
d0 Delete from cursor to beginning of line
dw Delete from cursor to end of current word
db Delete from cursor to beginning of current word

These commands must be given in command mode in vi/vim, not insert mode. All of these commands can be preceded by a number that is interpreted as the number of operations to perform. For example, 5dd is interpreted as delete 5 lines.

When a deletion occurs, the deleted text is stored temporarily in a buffer. This buffer can be pasted into the document relative to the current cursor position by using the p (lowercase p) paste after the current cursor position or the P (upper case P) paste before the current cursor position commands.

Beware that some commands in vi/vim will empty the buffer. Cursor movement is safe. However, since there is only one buffer that is used for both cut/copy/paste/undo/redo, any function that uses the buffer (insert, replace, etc.) will send your cut text to limbo.


About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

  • steven hildenburg

    Does this work for external text?
    Say, i want to copy from my pc notpad file into an SSH window that is running VIM – do i select my text in notepad, press CTRL+V , then put my cursor in VIM and press p (in command mode).

    I only ever use linux from a distance to manage my website’s config files – so i always feel quite lost.

    • steven hildenburg

      oops typo…i meant …”press CTRL+C” – in notepad

    • Mikael Gunnarsson

      No, the text you copy in windows isn’t automagically put into VI’s buffer.

      To copy text from windows to the remote host you need to go into insert mode in VI and then use your SSH client’s paste function.

      • Anonymous

        For example (with PuTTY), you would press Control+C on the text you want to copy, then switch to PuTTY, press i to insert text, then right click to have PuTTY paste the text in.

  • Matteosistisette

    d for delete works, but p doesn’t paste anything.

    Oh well the article is a few years old, maybe vim has changed.

    • Matteosistisette

      Oh sorry, I was trying to paste writing a :p command, just like you use a :d command for deletion. Instead, you just have to press the p key to paste

  • regdoug

    Actually, vi has 9 buffers in which deleted text is stored. Every time you cut text it moves the text “back” in the buffers.

    When you first cut (or yank) something, it is placed in buffer #1
    When you do it again, buffer #1 is moved to #2 and the new text is placed in #1
    and so on until buffer #9. The contents of #9 are lost on new edits

    the p command retrieves text from buffer #1 but you can retrieve text from other buffers by using “2p, “3p, or similar (note the double quotes)