How to Copy, Cut, and Paste Text in Vim? [All Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The Vim editor is a great tool for users to edit text files whenever necessary. While editing (copy, cut, and paste) a text file in Vim, first, press ESC to switch to the normal mode. Then,

  1. To copy the text, press yy after placing the cursor in the appropriate position.
  2. To cut a line, first place the cursor at the intended line in the normal mode. Then,  press dd.
  3. To paste the copied text in Vim, press p. It will paste the text after the cursor. Press P to paste before the cursor.

In this article, I will explore different approaches to do these. So, without further delay, let’s get started.

How to Copy in Vim?

To copy text in Vim, first, place the cursor to the intended position in the normal mode, then press yy. It will copy the text line of the current cursor to the clipboard. In this section, I will discuss a few more approaches to copying in Vim:

1. Copy an Entire Line in Vim

To copy an entire line, first, switch to the normal mode. Then, place the cursor in the start position. Now, type yy. This will copy the entire line:Line 2 is copied in Vim.The yy command has copied line number 2 in Vim.

2. Copy Multiple Lines in Vim

To copy multiple lines in Vim, first, place the cursor to the intended start position then type <line number>yy from the normal mode. In my case, I will copy three lines. For that, I will type 3yy after placing the cursor position in the right position:Initial cursor position in Vim.It will copy three lines starting from the current position in Vim:Three lines has been copied in Vim.

3. Copy a Word With its Trailing Whitespace

To copy a word with its trailing whitespace, place the cursor in the intended position in normal mode. Then type yaw:Word with trailing whitespace has been copied.The image shows that the yaw command has copied the “read” word with its trailing whitespace.

4. Copy Everything Right of the Cursor

To copy everything right from the cursor, first place the cursor to the specific position in normal mode. Then type y$. It will copy everything till the end of the line:Line 7 has been copied starting from the current cursor position.The image shows that the y$ command has copied the line till the end of the line.

5. Copy Everything Left of the Cursor

To copy everything left of the cursor position, first, switch to the normal mode. Then, type y^. It will copy the current line till the beginning of the line:Cursor initial position.The command has copied the line from the cursor till the beginning of the line:Line till the beginning has been copied.

6. Copy Between Cursor and a Specified Character (Excluding)

To copy everything between the cursor and a specific character type yt<character> in the normal mode. Here, I will copy till the first “d” character is found from the current cursor position, excluding the specified character. To do this, I will type ytd in the normal mode:"emin" has been copied.The image shows that “emin” (letters from the cursor position until the specified character) has been copied.

7. Copy Between Cursor and a Specified Character (Including)

To copy everything between the cursor and a specific character in Vim, type yt<character> in the normal mode. Here, I will copy until the “d” character from the current cursor position, including the specified character. To do this, I will type yfd in the normal mode:"emind" has been copied.The image shows that the yfd command has copied the “emind” (letters from the cursor position to the specified character).

How to Cut in Vim?

To cut text in Vim, press dd in normal mode. It will cut the current line. In this section, I have explored various ways to cut text in Vim:

1. Cut the Entire Line

To cut the entire line, first place the cursor at the intended line in the normal mode. Then,  press dd from the normal mode. Here, I will cut line 4 so first, I will move the cursor to line 4:Cursor at line 4.Now, I will press dd. It will cut the line 4 entirely:Line 4 cut.The image shows that line 4 is deleted.

2. Cut Multiple Lines

To cut multiple lines in Vim, first switch to the normal mode. Now, place the cursor in the initial position. Then, type  <line number>dd to delete the specific number of lines. Here, I will delete three lines. To do so, I will first switch to normal mode. Then, I will place the cursor to the intended position:Initial cursor position.Now, I will press 3dd. It will cut three lines from the current cursor position:3 lines cut.The image shows that cutting three lines is successful.

3. Cut Everything Right of the Cursor

To cut everything right from the cursor, type d$ in the normal mode. Here, I will put the cursor at the beginning of line 4 in the normal mode:Initial cursor position.Now, I will press d$. It will cut everything after the cursor:The line 4 cut.The image shows that the d$ command has cut everything after the cursor.

How to Paste in Vim?

To paste copied or cut text from the clipboard in Vim, simply press p. For example, first, type 4yy to copy line number 4 to the clipboard:Line 4 is copied in Vim.The image shows that the line 4 is yanked (copied) in Vim successfully. Now, move the cursor to the end of the file. Then, press p to paste the copied lines after the current cursor position:Line paste successful in Vim.The line paste is successful.

Note: If you press P, it will paste the copied or cut text before the cursor position.

Conclusion

In this article, I have thoroughly explored all possible ways to copy, cut, and paste in Vim. I hope this will be a good guide for the young learners. If you have any valuable thoughts about this article, please put them in the comment box. Have a nice day!

People Also Ask

How do you paste into Vim command?

To paste into Vim command, press p key. It will paste the copied text from the clipboard to Vim.

How to copy 5 lines in Vim?

To copy 5 lines in Vim, first move the cursor to the intended position in the normal mode. Then, type 5yy. It will type 5 lines in Vim from the current cursor position.

How to use Vim visual mode?

To use Vim visual mode, press v. It will switch to the visual mode. Then, use the arrow to select text.

How do I edit in Vim editor?

To edit in the Vim editor, switch to the insert mode by pressing i. Then, do the editing on whatever is necessary.

Related Articles


<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics

1/5 - (1 vote)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

2 thoughts on “How to Copy, Cut, and Paste Text in Vim? [All Cases]”

  1. Ive looked at so many videos. I need to know how to paste INTO vim from notepad. Sorry did not find any video or website that explains this.

    Reply
    • To paste text from Notepad into Vim, you can follow these steps:

      1. First, copy the text you want from Notepad. You can do this by selecting the text and pressing Ctrl+C.
      2. Next, open your Vim editor. If you’re in command mode (where you can’t type text directly), you can press i to switch to insert mode.
      3. Now, you can paste the text into Vim. If you’re using Vim in a terminal, you can usually do this by right-clicking and selecting Paste, or by pressing Ctrl+Shift+V. If you’re using a GUI version of Vim, you can usually paste with Ctrl+V.
      4. Once you’ve pasted the text, you can press Esc to go back to command mode.
      In command mode, you can type :help for more information. I hope this helps! Let me know if you have any other questions.

      Reply

Leave a Comment