How To Copy, Cut, And Paste Words And Lines In Vim (2024)

[#terminology]Vim Copy &Paste Terminology[#terminology]

The keyboard shortcuts to copy, cut, and paste can be boiled down into three characters that utilize Vim-specific terminology.

Understanding these terms will help you recall the correct keyboard shortcut.

  • Y stands for “yank” in Vim, which is conceptually similar to copying.
  • D stands for “delete” in Vim, which is conceptually similar to cutting.
  • P stands for “put” in Vim, which is conceptually similar to pasting.

I deliberately use the phrase “conceptually similar to” because these actions are not one and the same. If you want to dive deeper into this explanation, scroll down to the section below titled “What Happens Under the Hood?”

[#ccp]Copy, Cutting, and Pasting in Vim/Vi - The Basics[#ccp]

  1. Press esc to return to normal mode. Any character typed in normal mode will be interpreted as a vim command.
  2. Navigate your cursor to the beginning of where you want to copy or cut.
  3. To enter visual mode, you have 3 options. We suggest using visual mode because the selected characters are highlighted, and it’s clearer to see what’s happening. However, we have the keyboard shortcuts for normal mode (which achieve the exact same effect) in the section below.
  4. Press v (lowercase) to enter visual mode. This will start selecting from where your cursor is.
  5. Press V (uppercase) to enter visual line mode. This will select the entire line.
  1. Press CTRL+V to enter visual block mode.
  2. Move your cursor to the end of where you want to copy or cut.
  3. Press y to copy. Press d to cut.
  4. Move the cursor to where you want to paste your selection.
  5. Press P (uppercase) to paste before your cursor. Press p (lowercase) to paste after your cursor.

[#shortcuts]Vim Keyboard Shortcuts[#shortcuts]

Using arrow keys (or if you are an expert Vim user - h, j, k, and l) to move around in your vim file can take a long time.

Here are vim keyboard shortcuts for copying and cutting if you want to be even more efficient than the basic steps outlined above.

[#copying]Copying (Yanking)[#copying]

  • yy: Copy the current line in vi
  • 3yy: To yank multiple lines in vim, type in the number of lines followed by yy. This command will copy (yank) 3 lines starting from your cursor position.
  • y$: Copy everything from the cursor to the end of the line
  • y^: Copy everything from the start of the line to the cursor.
  • yiw: Copy the current word.

[#cutting]Cutting (Deleting)[#cutting]

  • dd: Cut the current line
  • 3dd: Cut 3 lines, starting from the cursor
  • d$: Cut everything from the cursor to the end of the line

Putting (Pasting)

  • P (uppercase): Paste before your cursor
  • p (lowercase): Paste after your cursor

What Happens Under the Hood?

Vim terms are slightly different from their conceptual counterparts that we mentioned above because these actions by default do not interact with the OS clipboard. For example, you can't paste yanked text from Vim into a different application with CMD + V.

Yank, delete, and put interact with Vim's notion of “registers”, which are basically Vim -specific clipboards. Each register is named with a character, which you can use to interact with that register. For example, you might yank line 50 of the current file to register “a” and yank line 14 of the same file to register “b”, because you intend to paste both line 50 and line 14.

To learn more about vim registers, check out this Stack Overflow page.

Conclusion

This should be everything you need to get started to copy, cut and paste in Vi. If you didn’t find what you were looking for, it may be worth checking out the official vim docs.

How To Copy, Cut, And Paste Words And Lines In Vim (2024)

FAQs

How To Copy, Cut, And Paste Words And Lines In Vim? ›

In vim it's possible to use Ctrl+V to select multiple lines and then p to paste something to each of them.

How do you paste the same thing on multiple lines in Vim? ›

In vim it's possible to use Ctrl+V to select multiple lines and then p to paste something to each of them.

How do I copy multiple lines and paste in vi? ›

Go to the first line you want to copy. Press V - as in captial v / shift-v. Use vims navigation/motion keys to move the cursor to the last line you want to copy. Yank the text using y , or if you're planning to do a cut/paste use d to delete the text (which by default stores it in the copy/paste buffer)

How do you copy entire content in vi editor? ›

Move the cursor using the arrow keys while holding down the 'Shift' key. As you move the cursor, you'll see that the text gets selected or highlighted. Keep moving the cursor until you've selected all the text you want to copy.

How do I copy 10 lines in Vim? ›

Vim Keyboard Shortcuts
  1. yy: Copy the current line in vi.
  2. 3yy: To yank multiple lines in vim, type in the number of lines followed by yy. ...
  3. y$: Copy everything from the cursor to the end of the line.
  4. y^: Copy everything from the start of the line to the cursor.
  5. yiw: Copy the current word.

How to copy pasting from Vim? ›

You can also use y or d in VISUAL mode on selected characters. If you select a bunch of characters and press one of these buttons, again it will copy the content and you can use P or p again to paste it somewhere else within Neovim.

How to duplicate text in Vim? ›

In Vim, copying text is also known as yanking. You can use the Y key on the keyboard. Vim offers several “yank” commands that allow you to customize the copy operation. After entering the Normal mode, position the cursor at the start of the text you wish to copy and execute one of the following commands.

How do you select a whole word in Vim? ›

You can use * and/or # to search for the word under the cursor or viw to visually select the word under the cursor.

How do you paste the same text multiple times in Vim? ›

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. It's that easy!

How do I select multiple lines and copy to clipboard in Vim? ›

Once you have verified that clipboard support is installed do the following:
  1. Position your cursor to the first line you want to copy.
  2. Press Shift v to enter visual mode.
  3. Press ↓ to select multiple lines.
  4. Press " + y to copy the selected text to system clipboard.
Feb 6, 2012

How do I copy and paste lines in vi editor in notepad? ›

vi(m) uses “yank” and “paste” (similar to copy/paste in Windows applications). To copy a line in vim first move your cursor to the line you want to copy then press “yy” (yank a line). Now, move your cursor to the line just above where you want to copy and press “p”. The yanked line will appear.

How to copy a word in Vim? ›

Copying text in Vim is also referred to as yanking. Use the y key on the keyboard when performing this operation. There are a number of yank commands, mainly differing on the amount of text you want to copy. Once in normal mode, move the cursor to the needed place and use the appropriate command.

How to copy and paste in vim insert mode? ›

Method 2. Shift + Right-Click & Paste
  1. copy the content of the text from the text file (Ctrl-C or right-click & copy)
  2. open the file you want to edit with the vim editor.
  3. type 'i' to enter the insert mode ( check at the bottom for — INSERT –)
  4. hit this key combination: Shift + Right-click & choose the 'Paste' from the menu.
Sep 18, 2018

How do I select all text in the vi editor? ›

To select all content in Vi Editor, first, enter visual mode by pressing Esc followed by Shift + v. Then, press Shift + g to select from the current cursor position to the end of the file.

How do I insert the same character on multiple lines in Vim? ›

In visual block mode, you can press I to insert text at the same position in multiple lines, and you can press A to append text to each line in a block. As well as inserting or appending text that you type, you can insert or append text from registers, for example, the clipboard.

How do you repeat on multiple lines in Vim? ›

One way to do this is to put the cursor on the first line, then press A<Backspace>Esc . Then in order to repeat the same commands on the next line press j. . Repeat on each line.

How do you copy and paste across sessions in Vim? ›

Copy text you want to share to system clipboard by using "+y command. Switch to other instance of Vim that you want to paste text into. In second instance of Vim, use "+p command to paste text from system clipboard. That's it!

Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6597

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.