How to Copy, Cut, and Paste in Vim Vi?

How to Copy, Cut, and Paste in Vim Vi

The Linux screen-based editor Vim (Vi Modified) can be configured to create any type of customized document. The open-source Vim editor offers tools for effectively altering (cutting, copying, and pasting) text. You may alter text quickly and effectively by running this sophisticated editor in the terminal.

The purpose of this tutorial is to show you how to copy, cut, and paste in Vim. In order to do this, the following information is discussed:

  • How Can I Copy Something in Vim?
  • Example 1: Use Vim to Copy a Whole Line
  • Example 2: Use Vim to Copy a Word
  • How Do You Cut in Vim?
  • Example 1: Remove or Cut the Whole Line
  • Example 2: Use Vim to Cut Two Lines
  • Example 3: Copy Text to End of Line Vim from Cursor Word
  • In Vim, how do you paste?
  • Example 1: Copy Text in Vim After the Cursor Line
  • Example 2: Copy and Paste Text in Vim at the Cursor Line
  • Bonus Tip: Use Vim’s Visual Mode to Copy, Cut, and Paste

Now let’s get going!

How Can I Copy Something in Vim?

The shortcut key “y” can be used to copy or pull in the Vim editor. Using the designated shortcut keys and the “y” key, you can copy a single character, word, line, or document. Let’s examine the Vim text copying options:

YeahThe entire line where the cursor is located is copied by this command.

Two yearsIn Vim, the “[number]yy” command is used to copy several lines. Use the “2yy” command to copy two lines—the line that has the cursor on it and the line after that.

y%Use the y% command to copy text between two similar characters, such as (), {}, or [].

ywThe “yw” command is used to copy a specific character to the end of the word.

YiwPress the yiw key on the keyboard to copy the word that is now selected.

y$ This command copies the text to the end of the line from the cursor character.

y^Everything is copied up to the cursor.

ytx”Yank (copy) till x (specified character)” is what “ytx” stands for. to duplicate the text between the pointer and the designated line character.

yfxThis command duplicates the text at the position of a certain character’s cursor.

 

Example 1: Use Vim to Copy a Whole Line

In Vim, hit the “y” key twice to copy the entire line.

Use the “vim ” command to open the requested file in Vim; by default, normal mode will be used. Once you are on the designated line, press the “y” key twice. The full line is copied to the clipboard.

The “p” shortcut can now be used to paste copied lines (as detailed in the next sections).

Note: To switch between the “normal” and “insert” modes, click the “Esc” button.

Example 2: Use Vim to Copy a Word

In Vim, to copy a word, use the “y” key in conjunction with the “w” option (word). To copy a word, move the pointer to it and press the y and w keys:

How Do You Cut in Vim?

In conventional editors, the commands cut and delete are distinct; nevertheless, in Vim, the cut command is referred to as delete. Cutting a line, word, or character is done using the “d” character. The following discusses the potential cutting scenarios:

ddThe entire line where the cursor is located is chopped by this command.

2dd The cursor line and the following line are both chopped by this command, resulting in two lines of text.

The command d%~d% removes characters {} or () and other similar characters from the text.

Dw The word in question is clipped by this command from the character where the cursor is to the end of the word.

diw Use the “diw” command to trim the current word at the cursor position.

d$ To clip the entire line from the left of the cursor to the line’s end.

d To trim the entire text from the cursor to the beginning of that line or to the right.

dtx The text from the cursor is cut before the designated character using this command.

dfx To

Example 1: Remove or Cut the Whole Line

Using “vim ” in the terminal, open the document in Vim and move to the required line. Position the cursor on a line and hit the “d” key twice to remove the text from that line and copy it to the clipboard:

Example 2: Use Vim to Cut Two Lines

Press that number and press the “d” key twice to cut multiple lines. For example, to cut the two lines, move the cursor to the line and type “2dd.” The following line will then be copied to the clipboard and deleted from the document:

Example 3: Copy Text to End of Line Vim from Cursor Word

When you select a word and hit the dollar symbol ($) and “d” keys, the text will be copied to the clipboard and vanish from the document:

In Vim, how do you paste?

In Vim, you can paste the copied text before or after the cursor by using the “p” key.

Example 1: Copy Text in Vim After the Cursor Line

Use the “p” command to paste text in the following line at the designated cursor location.

Example 2: Copy and Paste Text in Vim at the Cursor Line

Use the Capital “P” key to paste the text onto the same line in Vim:

Note: The pasted text will be added to the cursor line and the cursor line will be moved downward.

Let’s copy, paste, and cut while using the “Visual” mode.

Bonus Tip: Use Vim’s Visual Mode to Copy, Cut, and Paste

The arrow keys are used to highlight or select items in visual mode, as the name suggests, whereas the shortcut keys are used in normal mode. This is the preferred operating mode in Vim for seasoned users.

All you have to do to enter the visual mode is press the “v” key (visual). When in visual mode, you will choose the text to copy, paste, and cut by using the keyboard’s arrow keys. The image below displays the Visual mode.

Use the same keyboard shortcuts as in the preceding sections to copy, cut, and paste the desired text.

This concludes the article.

In summary

Use the shortcut keys “y” for copying, “d” for cutting, and “p” or “P” for pasting in both Vim’s normal and visual modes. In Vim, we have various shortcut keys that we may utilize to cut, copy, and paste. Using appropriate examples, this post has explained how to copy, cut, and paste in Vim.

Leave a Reply

Your email address will not be published. Required fields are marked *