How To Copy, Cut, and Paste Into Vim? (How To Paste in Vim)

When it comes to working with text files in Vim, an understanding of copying, cutting, and pasting text is essential. These three functions (cut, copy, and paste) are the most frequently used and basic tasks in the Vim/Vi editor. 

how to copy, cut, and paste into vim? (how to paste in vim)

In this article, we'll walk you through the process of how to copy, cut and paste into Vim Editor using Vim commands. In addition, we will also explore different Vim modes that empower you to navigate and manipulate text easily.

What Is Vim? 

Vim is a freely available text editor that caters to both graphical user interface (GUI) and command-line environments. It's free and open-source, representing an enhanced version of the Vi editor. 

The improved features distinguish Vim from its predecessors, making it a preferred choice for many users. It can be installed on various operating systems, including Mac OS, Linux, and Windows. With its well-documented features and robust community support, Vim offers a wealth of plugins and customization options, making it adaptable to individual preferences and needs. Its portability allows users to seamlessly transfer configurations between systems, facilitating collaboration and sharing within the community.

One of Vim's standout qualities is its efficiency and resource-friendliness, making it ideal for handling large codebases and remote operations. Its intuitive key bindings enable users to perform tasks swiftly solely using the keyboard. 

With built-in support for multiple file formats and programming languages, Vim caters to diverse development needs. Furthermore, Vim's extensive capabilities, such as registers, macros, and text manipulation tools, empower users to accomplish complex editing tasks with ease. Beyond its utility, Vim offers a rewarding learning experience, making text editing interesting and enjoyable.

Vim is useful for multiple purposes, for example, you can edit existing code and create new code. To do these functions, Vim editor offers several modes, each serving specific purposes. Some most commonly used modes are Normal mode, insert mode, Visual mode, replace mode and command mode.

Continue reading to discover different Vim modes and how to cut, copy, and paste in Vi/Vim editor.

How to Cut, Copy, and Paste In Vi/Vim Using Normal Mode? 

When you open Vim, you start in normal mode, where you can execute commands and move around the file.

To return to normal mode from any other mode, press the Esc key.

If you are using Vim, you should also know what terminologies are used for copying, cutting, and pasting text into Vim:

  • Copying text in Vim is known as "yank" (y).
  • Cutting text is referred to as "delete" (d).
  • Text Paste in vi denoted as "put" (p).

Copy Vim Text (Copy Vim)

You can copy the text in Vim using several options:

To quickly copy Vim text, press y and then hold and move your cursor to select the desired text.

Alternatively, you can use specific text copy Vim commands:

yy: Copies the entire current line.

3yy: Copies three lines starting from the current cursor position.

y$: Copies text from the cursor to the end of the line.

Yw: Copies text from the cursor to the beginning of the next word.

yiw: Copies the word under the cursor.

y%: Copies text enclosed within matching brackets like [], {}, or (). This is handy for copying text between such pairs.

Cut Vim (Deleting Text)

In Vim's normal mode, the "d" key is used to cut or delete text. Simply position the cursor where you want to start cutting, then press "d," followed by a movement command. Here are some useful delete commands:

dd: Deletes the current line, including the new line character.

3dd: Deletes three lines starting from the current cursor position.

d$: Deletes text from the cursor to the end of the line.

The same movement commands used for copying (yanking) also apply for deleting. For instance, dw deletes to the start of the next word, and d^ deletes text from the cursor to the beginning of the line.

Paste in Vi

To paste the text that you've copied or cut, place the cursor where you want to insert the text and press "p" to paste the text after the cursor or "P" to paste the text before the cursor.

How to Cut, Copy, and Paste Into Vim Using Visual Mode 

The visual mode in Vim functions similarly to selecting text by clicking and dragging with a mouse. It enables users to make text selections, which can then be manipulated with commands like copy, delete, and paste in vi/vim, applying these actions only to the selected text.

To enter a visual mode in Vim, press the "v" key while in normal mode. This allows you to select text using the arrow keys. Additionally, you can use other commands, such as "V" to select entire lines or "Ctrl+v" to select a block of text. Once the text is selected, you can perform various actions like copying, cutting, and pasting.

Copy Vim:

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

  • Press "v" in normal mode to enter visual mode.
  • Use the arrow keys to highlight the text you want to copy.
  • Once selected, press the "y" key to copy the text.

Cutting Vim text:

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

  • Press "v" in normal mode to enter visual mode.
  • Use the arrow keys to highlight the text you want to cut.
  • Once selected, press the "d" key to cut the text.

How To Paste in Vim (Paste in Vi)

To paste it into Vi in visual mode, use the "p" command. Here's how to do it:

  • Press "v" in normal mode to enter visual mode.
  • Use the arrow keys to highlight the location where you want to paste the text.
  • Once selected, press the "p" key to paste the text.

Some Useful Vim Commands and Keyboard Shortcuts (Move, Cut, Copy, and Paste in Vi/Vim)

Vim's efficiency is greatly attributed to its extensive use of keyboard shortcuts, enabling users to perform various editing tasks swiftly without reaching for the mouse. Below, we'll explore some of the most commonly used Vim keyboard shortcuts.

How To Navigate Text in Vi/Vim

The following commands are exclusive to command mode in Vim and are used for navigating the cursor:

Cursor Movement Commands:

  • "h" moves the cursor one character to the left.
  • "l" moves the cursor one character to the right.
  • "k" moves the cursor up one line.
  • "j" moves the cursor down one line.
  • "nG" or ":n" moves the cursor to the specified line "n." For example, "10G" moves to line 10.
  • "^F" (CTRL + F) scrolls forward by one screenful.
  • "^B" scrolls backward by one screenful.
  • "^f" scrolls forward by one page.
  • "^b" scrolls backward by one page.
  • "^U" scrolls up by half a screen.
  • "^D" scrolls down by half a screen.
  • "$" moves the cursor to the end of the current line.
  • "0" (zero) moves the cursor to the beginning of the current line.
  • "w" moves forward one word.
  • "b" moves backward one word.

How To Delete and Replace Text in Vi/Vim 

  • Press "x" to delete the character under the cursor.
  • "dd" deletes the entire current line.
  • "dw" deletes the current word.
  • Typing "r" replaces the character under the cursor.

How To Use Undo and Redo in Vim

  • "u" undoes the last change.
  • Pressing "Ctrl+r" redoes the last change.

Copy Vim, Cut, and Paste In Vim:

  • Use "y" to copy selected text.
  • "d" cuts selected text.
  • "p" pastes copied or cut text.

How To Search and Replace Text in Vim

  • Typing "/" initiates a search for a string.
  • Using ":%s/string1/string2/g" replaces all occurrences of "string1" with "string2."

How To Save Changes and Exit Vim

  • ":w" to save changes to the current file.
  • ":wq" to save changes and exit Vim.
  • ":q" exits Vim.
  • ":q!" exits Vim without saving changes.

Conclusion

In this guide, we demonstrated the essential functions of cutting, copying, and pasting text within Vim, a powerful text editor. Vim is a highly versatile text editor for Linux systems, renowned for its ability to handle various content management tasks and enhance productivity. If you're a beginner, you can familiarize yourself with Vim by exploring the interactive tutorial available on the Open Vim website. The BlueVPS hosting offers a Simple, scalable and customizable environment for users. If you are using Vim/Vi on a Linux VPS Server, you can use the same commands to cut, copy and paste text inside Vim.

This guide provides you with a hands-on approach to learning Vim's functionalities in a user-friendly environment. If you want to practice Vim commands, Open the Vim site, where you can improve your proficiency with Vim's commands and become more efficient in editing text.