Vim or its precursor Vi comes preinstalled on macOS and almost all Linux distributions. Knowing the basics of Vim is helpful in a situation where your favorite editor is not available.
When you launch the Vim editor, you’re in the normal mode. In this mode, you can run Vim commands and navigate through the file.
To go back to normal mode from any other mode, just press the
Vim has its own terminology for copying, cutting, and pasting. A copy is called yank (
y), cut is called delete (
d), and paste is called put (
To copy text, place the cursor in the desired location and press the
y key followed by the movement command. Below are some helpful yanking commands:
yy– Yank (copy) the current line, including the newline character.
3yy– Yank (copy) three lines, starting from the line where the cursor is positioned.
y$– Yank (copy) everything from the cursor to the end of the line.
y^– Yank (copy) everything from the cursor to the start of the line.
yw– Yank (copy) to the start of the next word.
yiw– Yank (copy) the current word.
y%– Yank (copy) to the matching character. By default supported pairs are
. Useful to copy text between matching brackets.
In normal mode,
d is the key for cutting (deleting) text. Move the cursor to the desired position and press the
d key, followed by the movement command. Here are some helpful deleting commands:
dd– Delete (cut) the current line, including the newline character.
3dd– Delete (cut) three lines, starting from the line where the cursor is positioned,
d$– Delete (cut) everything from the cursor to the end of the line.
The movement commands that apply for yanking are also valid for deleting. For example
dw, deletes to the start of the next word and
d^ deletes everything from the cursor to the start of the line.
To put the yanked or deleted text, move the cursor to the desired location and press
p to put (paste) the text after the cursor or
P to put (paste) before the cursor.
Vim’s visual mode allows you to select and manipulate text.
vto enter the visual mode.
Vto enter visual line mode, where the text is selected by line.
Ctrl+vto enter visual block mode. In this mode, the text is selected by rectangle blocks. Entering the visual mode also marks a starting selection point.
yto copy, or
dto cut the selection.
Pto paste the contents before the cursor, or
pto paste it after the cursor.
In this guide, we have shown you how to copy, cut, and paste in Vim.
If you are new to Vim, visit the Open Vim site where you can practice Vim with an interactive tutorial.