Signup/Sign In
PUBLISHED ON: FEBRUARY 16, 2023

Cut, Copy, Paste in Vim

Vim is a code editor software based on ed, and an improvement to the editor VI, hence the name vim - "VI Improved". vim is used via the terminal, by running the command vi / vim, hence a terminal editor. Any changes made are not autosaved, instead, it is like loading the file as a copy, and working on it, hence, the concept of buffers. The last point is what makes vim one of the most powerful, and frequently used editors, and why even after 30 years, and endless new options such as Atom, we still have vim.

Being a modal editor means that vim has various modes. When vim starts up, it loads into a mode known as normal mode, which is the root of all frustration for vim beginners. normal mode is where we can use "vim grammar", enter other modes using predefined keys, such as command-line mode where a user can run built-in commands, or just as in modern editors, commands from plugins that have been installed by the user, or commands that the users have created.

On most *nix-based operating systems, vi/vim comes pre-installed, if not, you would find it available in your operating system package repos, or it can be downloaded from this site. Vim copying is mostly done in normal, and visual mode.

Copying Text in Vim

  1. In normal mode, we press y ("vim verb") followed by a motion (:h {motion} in command mode for more information on motions), also known as "vim grammar". For example:
    • To copy till the start of the next word use yw
    • To copy the current, and next 4 lines, use y4j
    • To copy to the beginning of the previous word, (that is 2 before current), use y2b
    • To copy the entire line, and the next two lines (3 lines), use 3yy or 3Y
  2. In visual mode, we first enter it using either of v (text selection), V (selecting whole lines), Ctrl + V (selecting in rectangular blocks). Once in visual mode, and select our text using motions, pressing y or Y is enough to copy the text without any other motions.

To see the content you have copied run the command

reg 0

Hence, on pressing y2b when the cursor is on line 11, and the letter a, we get

yanking (copying in vim)

Cutting (deleting) Text in Vim

  1. In normal mode, we press d followed by a motion, just as we did for copying. For example:
    • To cut till the start of the next word use dw
    • To cut the current, and next 4 lines down, use d4j
    • To cut to the beginning of the previous word, (that is 2 before current), use d2b
    • To cut the entire line, and the next two lines (3 lines), use 3dd or 3D
  2. In visual mode, we first enter it using either of v (text selection), V (selecting whole lines), Ctrl + V (selecting in rectangular blocks). Once in visual mode, and selected our text using motions, pressing d or D is enough to copy the text without any other motions.

Deleted text is placed in the unnamed vim register, accessible by using "". To get the content that was last deleted we run

reg "

Using v (text selection mode), and then deleting text, we get the following,

deleting (cutting) in vimdeleting in result

Pasting in Vim

Pasting is done only in normal mode. We use the verb p and move our cursor to the desired spot, and then press p, to paste the text. If prepended by a number, the text is pasted many times. Hence, taking the text we deleted in our last example and moving to the end of the file. On pressing p we get,

pasting in vim

Conclusion

This tutorial covered how to cut, copy, and paste in vim, and how we can modify the area which we will be copying using vim motions, or visual mode.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.