Rabu, 25 Oktober 2023

Cut, Copy and Paste in Vim

Cut, Copy and Paste in Vim

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:

Action Description
yiw Copy the current word.
yy Copy the whole line.
diw Cut the current word.
dd Cut the whole line.
p Paste text.

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:

Command Description
nyy or nY Yanks (copies) the current line and the next n-1 lines. For example, 3yy copies the current line and the two lines below it.
yaw Yanks (copies) the current word the cursor is on.
yy or Y Yanks (copies) the entire current line.
y$ Yanks (copies) text from the cursor to the end of the line.
y^ or y0 Yanks (copies) text from the cursor to the beginning of the line.

To yank in Vim, follow 3 simple steps:

  1. Press the Esc key to switch to the normal mode
  2. Navigate to a line or a word that you want to copy
  3. 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 d command:

Command Description
dd Cuts the entire current line.
d$ Cuts text from the cursor to the end of the line.
d^ or d0 Cuts text from the cursor to the beginning of the line.
ndd or dN Cuts the current line and the next n-1 lines. For example, 3dd cuts the current line and the two lines below it.
daw 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:

Cut, Copy and Paste in Vim

How to paste text in the Vim editor

After copying or cutting text in Vim, you can simply paste it by pressing the p key.

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:

Cut, Copy and Paste in Vim

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:

  1. Go to the point from where you want to start the selection
  2. Press Ctrl + v to enable the visual mode
  3. Use arrow keys to make a selection
  4. Press the y key to copy the selected text

For example, here, I copied 4 lines using the visual mode:

Cut, Copy and Paste in Vim

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:

  1. Navigate to a point from where you want to cut lines
  2. Press Ctrl + v to switch to visual mode
  3. Use arrow keys to select the lines you want to cut
  4. Press the d key to cut the selected lines

So let's say I want to cut 4 lines, then, this is how I'd do it:

Cut, Copy and Paste in Vim

Pretty easy. Right?

More on Vim

Did you know that there are multiple modes in Vim? Learn more about different modes in Vim:

What are Vim Modes? How to Change Them?
Vim has three different modes for handling user input and processing them.
Cut, Copy and Paste in Vim

Want to step up your Vim game? Refer to tips and tricks to become a pro-Vim user:

11 Pro Vim Tips to Get Better Editing Experience
You can learn plenty of Vim tips on your own, or you can learn it from others’ experiences.
Cut, Copy and Paste in Vim

I hope you will find this guide helpful.



from It's FOSS https://ift.tt/WxFjq6Z
via IFTTT

Tidak ada komentar:

Posting Komentar