Black Friday Deals for Linux Users are Now Live!
Learn more
Nov 12, 2022 4 min read

How to Cut, Copy and Paste in Vim

Table of Contents

The functions of copy and paste are a critical part of file editing since it is performed quite often.

You can perform cut, copy and paste in Vim as well. You just need to know the keys for that.

I hope you are familiar with different Vim modes. The methods are different for normal mode and visual mode. I'll show the steps for both.

Cut, copy and paste in Normal mode of Vim

By default, when you launch Vim or open a file in Vim, you will be in the Normal mode. It helps you run all sorts of commands and set options.

If you are not in the Normal mode already, press the Escape (Esc) key and you will be in the Normal mode.

Copy in Vim

The word copy is also referred as "yank" in Vim's terminologies. The key that you use to copy text is the 'y' key.

It can be used in combination of various other keys. Below are a few examples (not exhaustive) of yanking text in Vim.

  • yiw - yank the current word
  • yw - yank from the current cursor location to start of next word
  • yb - yank from current cursor location to the end of the previous word
  • yy - yank the whole line (along with the newline character)
  • 4yy - yank 4 lines (along with the newline character)
  • y$ - yank from the current cursor location to the end of the line

Cut in Vim

Deleting text with 'd' deletes text, but it also copies it to your clipboard. That means whatever text you deleted with the 'd' key, it is also "yanked".

Below are a few examples of cutting text:

  • diw - delete (cut) current word
  • dw - delete (cut) from the current cursor location to the start of the next word
  • db - delete (cut) from the current cursor location to the end of the previous word
  • dd - delete (cut) the whole line (along with the newline character)
  • 4dd - delete (cut) 4 lines (along with the newline character)
  • d$ - delete (cut) from current cursor location to the end of the line
  • d^ - delete (cut) from the current cursor location to the start of the line

Once you have deleted text that you want to cut, you can easily paste it.

Paste in Vim

To paste text from the clipboard, you can use the standard keyboard shortcut "Ctrl + Shift + v", but it isn't the Vim way of doing things.

To paste text from clipboard buffer in Normal mode, press the 'p' key to "put" text. Pressing 'p' will put text after the cursor. If you want to put the text before your cursor, use the 'P' key.

💡
You can paste the same text multiple times by typing a number before pressing 'P'. Say you typed '4p', the same text will be pasted 4 times.

Cut, copy and paste in Visual mode of Vim

Vim's Visual mode is useful when you want to select the text, and also select text in a special rectangular pattern.

You might be wondering, "Why should I use the Visual mode when I can do almost everything from the Normal mode?"

And the answer to that is when you have specific text to select and copy/cut.

As the heading suggests, make sure that you are in the Visual mode. There are two ways of entering Visual mode in Vim.

  • Press 'v' key from Normal mode to enter the usual Visual Mode
  • Press 'Ctrl + v' to enter Visual Block mode
Visual Block mode in Vim

The Visual Block mode allows you to make a rectangular selection of text and manipulate it that way.

From here, you can do whatever you want to select text first to get a visual representation of what will be pasted after copying it or cutting it.

Once you have selected, press the 'd' or 'y' key to cut or delete respectively. After copying/cutting text, Vim will switch back to Normal mode.

Then, you can use the 'p' or 'P' key to paste after cursor or before the cursor, respectively.

How to Delete All Lines of a File in Vim [Quick Tip]
Esc+gg+dG is what you need to do for deleting all the lines of the file in Vim. Here’s how it works.

Conclusion

This article helps you how to use the cut, copy and paste functions in the Normal mode and Visual mode in Vim.

If you are interested in learning more than just the Vim Basics, I highly recommend using this program by Jovica Ilic.

Mastering Vim Quickly - Jovica Ilic
Exiting Mastering Vim Quickly From WTF to OMG in no time
Linux Foundation Training
Team LHB
Team LHB indicates the effort of a single or multiple members of the core Linux Handbook team.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.