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.
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.
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:
- To copy three (3) lines, move the cursor from where you want to begin copying and type:
- To copy a word with its trailing whitespace, copy the cursor at the beginning of the word and type:
- To copy a word without its trailing white-space, navigate the cursor at the beginning of the word and type:
- To copy everything right of the cursor to the end of the line, use the command:
- To copy everything left of the cursor to the start of the line, type:
- To copy everything between the cursor and a specified character in the line, use the command:
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:
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:
- To cut three (3) lines, starting from the one where the cursor is located use:
- To cut everything right of the cursor to the end of the line use the command:
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
You can paste (or put) text by moving the cursor to the wanted position and pressing:
Using this command pastes the selected text after the cursor.
To add text before the cursor, type the capitalized command instead:
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+vto select by block
After you have selected the wanted text you can press:
yto yank (copy) the content
dto delete (cut) the content
pto put (paste) the content
Once you have edited in Vim, make sure to save the file before you exit.
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.