How to Cut, Copy and Paste in Vim / Vi

April 6, 2020

Introduction

Vim is a commonly used open-source text editor installed by default on most Unix distributions. You can use the text editor in two modes, from a command-line interface or as an independent application in a GUI.

While working in Vim, copying, cutting and pasting text are frequently used shortcuts.

In this tutorial learn how to copy, cut and paste in Vi / Vim editor.

guide on how to copy and paste using Vim

Note: If you don’t have Vim on your system, you may want to check out our guides on How to Install Vim on Ubuntu or How to Install Vim on CentOS 7.

Copy, Cut and Paste in Normal Mode

Before you start, make sure you are in normal mode (text editing/command mode). The best way to do so is to press Esc. This mode allows you to move through the text easily.

example of a Vim text editor in normal mode

Copying in Vim

Copying text in Vim is also referred to as yanking. Use the y key on the keyboard when performing this operation.

There are a number of yank commands, mainly differing on the amount of text you want to copy.

Once in normal mode, move the cursor to the needed place and use the appropriate command.

  • To copy an entire line, place the cursor at the beginning of the line and type:
yy
  • To copy three (3) lines, move the cursor from where you want to begin copying and type:
3yy
  • To copy a word with its trailing whitespace, copy the cursor at the beginning of the word and type:
yaw
  • To copy a word without its trailing white-space, navigate the cursor at the beginning of the word and type:
yiw
  • To copy everything right of the cursor to the end of the line, use the command:
y$
  • To copy everything left of the cursor to the start of the line, type:
y^
  • To copy everything between the cursor and a specified character in the line, use the command:
ytx

The command stands for “yank till x”. Replace x with the character to which you want to copy to. The character x will not be included.

  • To copy everything between the cursor and a specified character in the line (including that character), use the command:
yfx

This instructs Vim to “find x”.

Cutting in Vim

Cutting text is referred to as deleting in Vim. Use the d key when performing this operation.

If you are using Vim in normal mode, you can easily cut or delete text using the d command. Here are some ways you can cut content:

  • To cut the entire line in which the cursor is located type:
dd
  • To cut three (3) lines, starting from the one where the cursor is located use:
3dd
  • To cut everything right of the cursor to the end of the line use the command:
d$

Pasting in Vim

Once you have selected text in Vim, no matter whether it is using the yank or the delete command, you can paste it in the wanted location.

In Vim terminology, pasting is called putting and the function is utilized with the p command.

You can paste (or put) text by moving the cursor to the wanted position and pressing:

p

Using this command pastes the selected text after the cursor.

To add text before the cursor, type the capitalized command instead:

P

Note: Refer to our tutorial on how to show lines in Vim to learn how to display absolute, relative and hybrid line numbers.

Copy, Cut and Paste in Visual Mode

Alternatively, you can copy and edit text using the visual selection feature. This mode allows you to select text by navigating around.

Use Esc to exit out of the previously used mode and enable visual selection by pressing:

  • v (lowercase) to start selecting individual characters
  • V (uppercase) to select the entire line
  • Ctrl+v to select by block

After you have selected the wanted text you can press:

  • y to yank (copy) the content
  • d to delete (cut) the content
  • p to put (paste) the content
Vim copying, cutting and pasting in visual mode

Once you have edited in Vim, make sure to save the file before you exit.

Conclusion

Now you know how to copy, cut and paste in Vim. You may often find yourself needing to make configuration changes to your packages. Most of these can quickly be done in Vim using cut & copy paste.

Next, you can explore the many options VIM to offer, such as Vim color schemes.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Install Vim 8.2 on CentOS 7
September 10, 2019

Official repositories struggle to keep pace with updates and new software versions. This article provides...
Read more
How to Change and Use Vim Color Schemes
March 9, 2020

Vim color schemes are a handy feature for setting syntax highlighting. You can choose from a wide variety of...
Read more
How to Show or Hide Line Numbers in Vim / Vi
February 28, 2022

You can activate line numbering in Vim by showing absolute, relative or hybrid lines. Displaying or hiding...
Read more
How to Undo and Redo Changes in Vim / Vi
April 28, 2020

Mastering basic Vim commands includes learning how to undo and redo changes in this text editor. Knowing how...
Read more