乐闻世界logo
搜索文章和话题

How to duplicate a whole line in Vim?

1个答案

1

Copying an entire line in Vim is straightforward. You can follow these steps:

  1. Enter Normal mode: First, ensure you are in Vim's Normal mode. If you just opened Vim or are currently in Insert mode, press Esc to return to Normal mode.

  2. Move to the line to copy: Use the up arrow key (k) or down arrow key (j) to navigate to the line you want to copy.

  3. Copy the line: Use the yy command to copy the current line. Here, y represents yank (Vim's term for copying), and yy copies the entire current line in one step.

  4. Paste the line: After copying, use the p command to paste the content below the current line, or use P to paste it above the current line.

Example: Suppose you are editing a text file containing the following content:

shell
First line Second line Third line

If you want to copy "Second line", move the cursor to that line, use yy to copy it, then use p or P to paste it at your desired location.

This method is highly efficient and quick, making text editing more productive. Vim's modal editing approach is powerful; once you're familiar with these basic commands, you can quickly perform various text editing tasks.

2024年7月22日 09:49 回复

你的答案