Cutting, copying, and pasting text is one of the most basic tasks in text editing and we all know Vim has a different way of doing things.
This means until you get a hang of it, you fear it and once you get a grip, it is nothing but a rabbit hole.
While I’ll address cut, copy, and paste in detail, here’s a basic summary of this tutorial to get things started:
||Copy the current word.|
||Copy the whole line.|
||Cut the current word.|
||Cut the whole line.|
Don’t worry, Vim gives you much more options than what I’ve mentioned above.
In this tutorial, I will walk you through the following:
- How to copy text in Vim
- How to cut text in Vim
- How to paste text in Vim
- How to cut and copy text in Vim using Visual mode
So let’s start with the first one.
How to copy the text in the Vim editor
While we use the term copy, Vim has a different term called
yank so from now on, I will be using Yank instead of copy.
As I mentioned earlier, you get multiple ways to yank text in Vim, and here are some useful ones:
||Yanks (copies) the current line and the next
||Yanks (copies) the current word the cursor is on.|
||Yanks (copies) the entire current line.|
||Yanks (copies) text from the cursor to the end of the line.|
||Yanks (copies) text from the cursor to the beginning of the line.|
To yank in Vim, follow 3 simple steps:
- Press the
Esckey to switch to the normal mode
- Navigate to a line or a word that you want to copy
- Press the relative command from the above table and your text will be copied
Want to learn the interactive way to copy lines? Skip to the last section of this tutorial.
How to cut text in the Vim editor
In Vim, you don’t have any option to delete the text. Instead, you cut the text so you the delete and cut the text are similar things in Vim.
To cut text in Vim, you press the
d command. But you never use the
d command without any opinions. You always add something to it to get more out of it.
So here are some practical ways you can cut the text using the
||Cuts the entire current line.|
||Cuts text from the cursor to the end of the line.|
||Cuts text from the cursor to the beginning of the line.|
||Cuts the current line and the next
||Cuts the current word the cursor is on.|
Let’s say I want to cut the first 4 lines from my file, then I need to use
4dd and here’s how I did it:
How to paste text in the Vim editor
After copying or cutting text in Vim, you can simply paste it by pressing the
You can press the
p key multiple times to paste text multiple times or use
np where the
n is the number of how many times you want to paste the text.
For example, here, I pasted lines that I copied previously three times:
It’s that easy!
How to cut and copy text by selecting it
If you are coming from GUI text editors, you must be used to copying and cutting text by selecting it.
So let’s start with how you copy text by selecting it in Vim.
Copy the text by selecting it
To copy the text in visual mode, follow 3 simple steps:
- Go to the point from where you want to start the selection
Ctrl + vto enable the visual mode
- Use arrow keys to make a selection
- Press the
ykey to copy the selected text
For example, here, I copied 4 lines using the visual mode:
If you notice, once I pressed the
y key, it showed how many lines were yanked (copied). In my case, 4 lines were yanked.
Cut the text by selecting it in Vim
To cut the text in Vim in visual mode, all you have to do is follow 4 simple steps:
- Navigate to a point from where you want to cut lines
Ctrl + vto switch to visual mode
- Use arrow keys to select the lines you want to cut
- Press the
dkey to cut the selected lines
So let’s say I want to cut 4 lines, then, this is how I’d do it:
Pretty easy. Right?
More on Vim
Did you know that there are multiple modes in Vim? Learn more about different modes in Vim:
Want to step up your Vim game? Refer to tips and tricks to become a pro-Vim user:
I hope you will find this guide helpful.