How to copy, Cut and Paste in Vim/Vi Editor? [VIM Copy Paste]

While working with any text files in VIM; Copy, cut, and paste is the most common task. Throughout this article, you will know How to Copy, Cut and Paste in Vim/Vi Editor.

Updated: 11 May, 23 by Susith Nonis 7 Min

List of content you will read in this article:

Vim is a most powerful Linux text editor that can carry out all kinds of content management tasks; also boost your productivity. In this article, we will be helping you out with How to copy, paste and cut in VIM Editor for any content in a clipboard using Vim commands. 

VIM is one of the best text editors commonly used in Command-line interface and Graphical User Interface environments. It is free and open-source software. It is the modified version of the vi editor. Some significant differences make Vim a better choice compare to the vi editors listed below.

Read the sections below to learn how to copy, copy and paste in Vim/Vi editor.

Go to the desired location in the text editor, press y, and move the cursor to the specified location. Alternatively, you could also make use of the following options to perform the same task in another manner.

  • yy: Replace the current line with the newline character.
  • 3yy: Copy three lines, beginning with the line that has the cursor.
  • y$: Copies everything from the cursor to the line's end.
  • Yw: Copy to the starting of the next word.
  • yiw: Copy the word that the cursor is pointing to.
  • y%: Copy all the matching characters. By default, supported pairs include [], {} and (). This is useful for copying text between matching brackets.

Go to the desired location in the text editor, press d, and move the cursor to the specified location. Alternatively, you could also make use of the following options to perform the same task in another manner.

  • dd: Remove the current line (including the newline character).
  • 3dd: Cut three lines, beginning with the line where the cursor is pointed.
  • d$: Cut everything from the cursor to the line's end.

Press to paste the copied text after the cursor and P to paste it before the cursor.

In visual mode, you can select text by highlighting it, and then apply commands to the selected text. One of the most common commands used in visual mode is copy, cut, and paste. This section will discuss how to use the copy, cut, and paste commands in visual mode in Vim/Vi editor.

To enter visual mode, you need to press the "v" key in normal mode. This will allow you to select text using the arrow keys. You can also use other commands such as "V" to select entire lines or "Ctrl+v" to select a block of text. Once you have selected the text, you can use the following commands to copy, cut, and paste:

Copying text:

To copy text in visual mode, you need to use the "y" command. Here's how to copy text in visual mode:

  1. Enter visual mode by pressing the "v" key in normal mode.
  2. Use the arrow keys to highlight the text you want to copy.
  3. Once the text is selected, press the "y" key to copy the text.

Cutting text:

To cut text in visual mode, you need to use the "d" command. Here's how to cut text in visual mode:

  1. Enter visual mode by pressing the "v" key in normal mode.
  2. Use the arrow keys to highlight the text you want to cut.
  3. Once the text is selected, press the "d" key to cut the text.

Pasting text:

To paste text in visual mode, you need to use the "p" command. Here's how to paste text in visual mode:

  1. Enter visual mode by pressing the "v" key in normal mode.
  2. Use the arrow keys to highlight the location where you want to paste the text.
  3. Once the location is selected, press the "p" key to paste the text.

Let's discuss the difference between normal mode and insert mode in Vim/Vi editor. Normal mode is the default mode of the editor where you can navigate, copy, cut, paste, and execute commands. Insert mode is used to insert text into the file. To switch to insert mode, you need to press the "i" key.

Copying text:

To copy text in normal mode, you need to use the "y" command. The "y" command stands for "yank" and is used to copy text. Here's how to copy text in Vim/Vi editor:

  1. Move the cursor to the beginning of the text you want to copy.
  2. Press the "v" key to enter visual mode.
  3. Use the arrow keys to select the text you want to copy.
  4. Once the text is selected, press the "y" key to copy the text.

Cutting text:

You need to use the "d" command to cut text in normal mode. The "d" command stands for "delete" and is used to cut text. Here's how to cut text in Vim/Vi editor:

  1. Move the cursor to the beginning of the text you want to cut.
  2. Press the "v" key to enter visual mode.
  3. Use the arrow keys to select the text you want to cut.
  4. Once the text is selected, press the "d" key to cut the text.

Pasting text:

To paste text in normal mode, you need to use the "p" command. Here's how to paste text in Vim/Vi editor:

  1. Move the cursor to the location where you want to paste the text.
  2. Press the "p" key to paste the text.

One of the key features that makes Vim so efficient is its use of keyboard shortcuts. Vim's keyboard shortcuts allow you to perform a wide range of editing tasks quickly and easily, without having to take your hands off the keyboard. In this section, we will discuss some of the most commonly used Vim keyboard shortcuts.

Basic movement:

Vim allows you to move the cursor around the screen quickly and easily using the following keyboard shortcuts:

h - move the cursor left

j - move the cursor down

k - move the cursor up

l - move the cursor right

Deleting and replacing text:

Vim allows you to delete and replace text quickly using the following keyboard shortcuts:

x - delete the character under the cursor

dd - delete the current line

dw - delete the current word

r - replace the character under the cursor

Undo and redo:

Vim allows you to undo and redo changes using the following keyboard shortcuts:

u - undo the last change

Ctrl+r - redo the last change

Copy, cut, and paste in Vim Editor:

Vim allows you to copy, cut, and paste text using the following keyboard shortcuts:

y - copy selected text

d - cut selected text

p - paste copied or cut text

Search and replace:

Vim allows you to search and replace text using the following keyboard shortcuts:

/ - search for a string

:%s/string1/string2/g - replace all occurrences of string1 with string2

Saving and exiting:

Vim allows you to save and exit a file using the following keyboard shortcuts:

:w - save changes to the current file

:wq - save changes and exit Vim

:q - exit Vim

:q! - exit Vim without saving changes

In this article, we went through the Copy, Cut, and Paste commands in the Vim text editor. There are several copies and cut options that we can make use of to copy the content in various patterns. The pasting can be done with a single click of a button. If you do have any other methods for Copying, Pasting, and Cutting in Vim editor you can suggest them via the comment section below.

People are also reading:

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.