Chat Zalo Chat Messenger Phone Number Đăng nhập
Copy & Paste in Vim / Vi

Copy & Paste in Vim / Vi

Vim copy and paste terminology Keyboard shortcuts for copying,

cutting, and pasting can be reduced to three characters that use Vim-specific terminology

.

Understanding these terms will help you remember the correct keyboard shortcut

. And it means “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 paste.

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

Copy, Cut and Paste in Vim/Vi – The

  1. Basics Press esc to return to normal mode. Any character typed in normal mode will be interpreted as a vim command.
  2. Move the 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 is clearer to see what is happening. However, we have the keyboard shortcuts for normal mode (which achieve exactly the same effect) in the section below.
  4. Press v (lowercase) to enter visual mode. This will start selecting from where the cursor is.
  5. Press V (uppercase) to enter line-of-sight mode. This will select the entire line.
  1. Press CTRL+V to enter visual lock mode
  2. .

  3. Move the cursor to the end of where you want to copy or cut.
  4. Press

  5. y to copy. Press d to cut.
  6. Move the cursor to where you want to paste the selection.
  7. Press P (uppercase) to paste before the cursor. Press p (lowercase) to paste after the cursor.

Keyboard shortcuts

Vim

Using the arrow keys (or if you are an expert Vim user – h, j, k and l) to move around in your vim file can be time-consuming

.

Here are vim keyboard shortcuts to copy and cut if you want to be even more efficient than the basic steps described above.

Copy

(Yanking)

yy:

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

  • ^: Copy everything from the beginning of the line to the cursor
  • .

  • yiw: Copies the current word.

Cut (delete)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

Put (

paste

)P (

  • uppercase): Paste before the cursor p (lowercase):
  • paste after the cursorWhat

happens under the hood?

Vim terms are slightly different from their conceptual counterparts we mentioned earlier because these actions by default do not interact with the operating system’s clipboard. For example, you cannot paste text extracted from Vim into a different application with CMD+V.

Pull, delete, and interact with the notion of Vim “records,” which are basically Vim-specific clipboards. Each record is named with a character, which you can use to interact with that record. For example, you can pull line 50 of the current file to record “a” and pull line 14 of the same file to record “b”, because you intend to paste both line 50 and line 14.

For more information about vim logs, see this stack overflow page.

Conclusion

This should be all you need to start copying, cutting and pasting into Vi. If you didn’t find what you were looking for, it may be worth checking out the official vim documents.

Contact US