如何在 Vim 中剪切、复制和粘贴
Vim 编辑器
![](/data/attachment/album/202311/07/172330q49ttt4ee9r86u39.png)



> 
> 在本篇 Vim 快速技巧中，你将学习到剪切和复制粘贴的相关知识。
> 
> 
> 


剪切、复制和粘贴文本是文本编辑中最基本的任务之一，我们都知道 Vim 有不同的处理方式。


这意味着，在你掌握它之前，你会害怕它，一旦你掌握了它，它就只是一个兔子洞。


虽然我将详细介绍剪切、复制和粘贴，但这里是本教程的基本摘要，以帮助你开始使用：




| **按键** | **描述** |
| --- | --- |
| `yiw` | 复制当前单词。 |
| `yy` | 复制整行。 |
| `diw` | 剪切当前单词。 |
| `dd` | 剪掉整行。 |
| `p` | 粘贴文本。 |


别担心，Vim 为你提供的选项比我上面提到的要多得多。


在本教程中，我将引导你完成以下内容：


* 如何在 Vim 中复制文本
* 如何在 Vim 中剪切文本
* 如何在 Vim 中粘贴文本
* 如何使用可视模式在 Vim 中剪切和复制文本


那么让我们从第一个开始。


### 如何在 Vim 编辑器中复制文本


虽然我们使用术语“复制”，但 Vim 有一个不同的术语，称为 “<ruby> 扽出 <rt>  yank </rt></ruby>”，因此从现在开始，我将使用“扽出”而不是“复制”。


正如我之前提到的，你可以使用多种方法在 Vim 中扽出文本，以下是一些有用的方法：




| 命令 | 描述 |
| --- | --- |
| `nyy` 或 `nY` | 扽出（复制）当前行和接下来的 `n-1` 行。例如，`3yy` 复制当前行及其下面的两行。 |
| `yaw` | 扽出（复制）光标所在的当前单词。 |
| `yy` 或 `Y` | 扽出（复制）整个当前行。 |
| `y$` | 扽出（复制）从光标到行尾的文本。 |
| `y^` 或 `y0` | 扽出（复制）从光标到行首的文本。 |


要在 Vim 中扽出，请执行以下 3 个简单步骤：


1. 按 `Esc` 键切换到正常模式
2. 移动到要复制的行或单词
3. 按上表中的相关命令，你的文本将被复制


想学习交互式复制行的方式吗？ 跳到本教程的最后一部分。


### 如何在 Vim 编辑器中剪切文本


在 Vim 中，你没有任何删除文本的选项。取而代之的是剪切文本，因此删除和剪切文本与 Vim 中的操作类似。


要在 Vim 中剪切文本，请按 `d` 命令。但你永远不会在没有任何选项的情况下使用 `d` 命令。你总是会添加一些东西来做更多操作。


因此，你可以使用以下一些实用方法使用 `d` 命令剪切文本：




| 命令 | 描述 |
| --- | --- |
| `dd` | 剪切整个当前行。 |
| `d$` | 将文本从光标剪切到行尾。 |
| `d^` 或 `d0` | 将文本从光标剪切到行首。 |
| `ndd` 或 `dN` | 剪切当前行和接下来的 `n-1` 行。例如，`3dd` 剪切当前行及其下面的两行。 |
| `daw` | 剪切光标所在的当前单词。 |


假设我想从文件中剪切前 4 行，然后我需要使用 `4dd`，我是这样做的：


![](/data/attachment/album/202311/07/172432w9s9ve9u4v468csf.gif)


### 如何在 Vim 编辑器中粘贴文本


在 Vim 中复制或剪切文本后，只需按 `p` 键即可粘贴它。


你可以多次按 `p` 键多次粘贴文本，也可以使用 `np`，其中 `n` 是要粘贴文本的次数。


例如，在这里，我粘贴了之前复制了三遍的行：


![](/data/attachment/album/202311/07/172432c41w8aaq1lxjv1p1.gif)


就是这么简单！


### 如何通过选择文本来剪切和复制文本


如果你使用过 GUI 文本编辑器，那么你肯定习惯于通过选择文本来复制和剪切文本。


让我们从如何通过在 Vim 中选择文本来复制开始。


#### 通过选择文本复制


要在可视模式下复制文本，请执行以下 3 个简单步骤：


1. 移动到要开始选择的地方
2. 按 `Ctrl + v` 启用可视模式
3. 使用箭头键进行选择
4. 按 `y` 键复制所选文本


例如，在这里，我使用可视模式复制了 4 行：


![](/data/attachment/album/202311/07/172432coxvtncbjmnv0v97.gif)


如果你注意到，当我按下 `y` 键，它就会显示有多少行被扽出（复制）。就我而言，有 4 行被复制。


#### 在 Vim 中选择文本来剪切文本


要在 Vim 中以可视模式剪切文本，你所要做的就是遵循 4 个简单步骤：


1. 移动到要剪切的位置
2. 按 `Ctrl + v` 切换到可视模式
3. 使用箭头键选择要剪切的行
4. 按 `d` 键剪切选定的行


假设我想剪掉 4 行，那么我会这样做：


![](/data/attachment/album/202311/07/172432ocy7myhmsu5m5u45.gif)


挺容易。是吧？


### 更多关于 Vim 的内容


你知道 Vim 有多种模式吗？ [了解有关 Vim 中不同模式的更多信息](https://linuxhandbook.com/vim-modes/)。


想提高你的 Vim 水平吗？请参阅 [成为 Vim 专业用户的提示和技巧](https://linuxhandbook.com/pro-vim-tips/)。


我希望本指南对你有所帮助。


*（题图：MJ/3555eed2-ab14-433d-920f-17b80b46ce74）*




---


via: <https://itsfoss.com/vim-cut-copy-paste/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

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:

- Press the
`Esc`

key 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 `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 multiple lines in the Vim editor](https://itsfoss.com/content/images/2023/10/Cut-multiple-lines-in-the-Vim-editor.gif)

## 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:

![paste lines in Vim](https://itsfoss.com/content/images/2023/10/paste-lines-in-Vim-editor.gif)

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
- Press
`Ctrl + v`

to enable the visual mode - Use arrow keys to make a selection
- Press the
`y`

key to copy the selected text

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

![Copy lines in vim by selecting them](https://itsfoss.com/content/images/2023/10/Copy-lines-in-vim-by-selecting-them.gif)

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
- Press
`Ctrl + v`

to switch to visual mode - Use arrow keys to select the lines you want to cut
- 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 lines in Vim by selecting them](https://itsfoss.com/content/images/2023/10/Cut-lines-in-Vim-by-selecting-them.gif)

Pretty easy. Right?

## More on Vim

Did you know that there are multiple modes in Vim? [Learn more about different modes in Vim:](https://linuxhandbook.com/vim-modes/)

[What are Vim Modes? How to Change Them?Vim has three different modes for handling user input and processing them.](https://linuxhandbook.com/vim-modes/)![](https://linuxhandbook.com/content/images/2022/04/vim-modes.png)


![](https://linuxhandbook.com/content/images/2022/04/vim-modes.png)

Want to step up your Vim game? Refer to [tips and tricks to become a pro-Vim user](https://linuxhandbook.com/pro-vim-tips/):

[11 Pro Vim Tips to Get Better Editing ExperienceYou can learn plenty of Vim tips on your own, or you can learn it from others’ experiences.](https://linuxhandbook.com/pro-vim-tips/)![](https://linuxhandbook.com/content/images/2022/08/pro-vim-tips.png)


![](https://linuxhandbook.com/content/images/2022/08/pro-vim-tips.png)

I hope you will find this guide helpful.