How to cut, copy and paste in Vim

picture

Table of contents

Preface

How to copy text in Vim editor

How to cut text in Vim editor

How to paste text in Vim editor

How to cut and copy text by selecting it

Copy by selecting text

Cut text by selecting text in Vim


Preface

In this quick Vim tip, you'll learn about cutting and copying and pasting.

Cutting, copying and pasting text is one of the most basic tasks in text editing, and we all know that Vim handles it differently.

This means that until you master it, you'll be afraid of it, and once you master it, it's just a rabbit hole.

While I'll cover cutting, copying, and pasting in detail, here's a basic summary of the tutorial to get you started:

<If the display is not complete, please slide left or right>

button describe
yiw Copy the current word.
yy Copy the entire line.
diw Cut the current word.
dd Cut off the entire row.
p Paste the text.

Don’t worry, Vim gives you many more options than what I mentioned above.

In this tutorial, I'll 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 text in Vim editor

Although we use the term "copy", Vim has a different term called "yank", so from now on I will use "yank" instead of "copy".

As I mentioned before, you can use a variety of methods to extract text in Vim, here are some useful methods:

<If the display is not complete, please slide left or right>

Order describe
nyy or nY Extract (copy) the current line and the following  n-1 lines. For example, 3yy copy the current row and the two rows below it.
yaw Extract (copy) the current word where the cursor is.
yy or Y Extract (copy) the entire current line.
y$ Extract (copy) text from the cursor to the end of the line.
y^ or y0 Extract (copy) text from the cursor to the beginning of the line.

To export in Vim, follow these 3 simple steps:

1. Esc Press the button to switch to normal mode

2. Move to the line or word you want to copy

3. Press the relevant command in the above table and your text will be copied

Want to learn an interactive way to copy rows? Skip to the last part of this tutorial.

How to cut text in Vim editor

In Vim, you don't have any option to delete text. Instead, the text is cut, so deleting and cutting text is similar to the operations in Vim.

To cut text in Vim, press  d command. But you never use a command without any options  d . You're always adding something to do more.

So here are some practical ways you can cut text using  d commands:

<If the display is not complete, please slide left or right>

Order describe
dd Cut the entire current line.
d$ Cut text from cursor to end of line.
d^ or d0 Cut text from cursor to beginning of line.
ndd or dN Cut the current line and the following  n-1 lines. For example, 3dd cut the current line and the two lines below it.
daw Cut the current word under the cursor.

Let's say I want to cut the first 4 lines from a file, then I need to use  4dd, this is how I do it:

picture

How to paste text in Vim editor

After copying or cutting text in Vim, just p press a key to paste it.

You can paste the text multiple times p by pressing the key  multiple times, or you can use npwhere  n is the number of times you want to paste the text.

For example, here I've pasted the line I copied three times before:

picture

It's that simple!

How to cut and copy text by selecting it

If you've ever used a GUI text editor, you're no doubt used to copying and cutting text by selecting it.

Let's start with how to copy text by selecting it in Vim.

Copy by selecting text

To copy text in visual mode, follow these 3 simple steps:

1. Move to the place where you want to start selecting

2. Press  Ctrl + v to enable visual mode

3. Use the arrow keys to make a selection

4. y Press the key to copy the selected text

For example, here I copied 4 rows using visual mode:

picture

If you notice, when I press  y the key, it shows how many lines have been copied out (copied). In my case, 4 rows were copied.

Cut text by selecting text in Vim

To cut text in visual mode in Vim, all you have to do is follow 4 simple steps:

1. Move to the position where you want to cut

2. Press  Ctrl + v to switch to visual mode

3. Use the arrow keys to select the row to cut

4. Press d the key to cut the selected row

Let's say I want to cut off 4 rows, then I would do this:

picture

Quite easy. right?

Guess you like

Origin blog.csdn.net/m0_68662723/article/details/134622231