We learned about inserting and selecting text in the insert mode and visual mode sections. In this section we will learn the basics of working with text.
Most basic editing operations are performed using a combination of normal mode for navigating the buffer, visual mode for selecting text to edit, and insert mode for inserting text. Other more specific operations are executed in command-line mode.
In this section we summarize various commands that execute basic editing operations, such as deleting, copy and paste, moving text, etc.
As a refresher, here are some ways switch from normal mode to other modes:
To | key |
---|---|
Insert | i |
Visual | v |
visual-line | V |
visual-block | C-V |
then return to normal mode from any of these modes using Esc.
Deleting
commands related to deleting content generally use the d prefix, followed by a motion. Many commands also take an optional count.
Here is a summary common of commands for deleting text in a buffer:
Command | Action |
---|---|
dd | Delete the current line |
dw | Delete from the cursor position to the start of the next word |
d0 | Delete from the cursor position to the start of the current line |
d$ | Delete to end of current line |
dgg | Delete from the cursor position to the start of the document |
dG | Delete from the cursor position to the end of the document |
Yanking
commands related to yanking content generally use the y prefix, followed by a motion. Many yank commands also accept a count.
Here is a summary of common commands for yanking text in a buffer:
Command | Action |
---|---|
yy | Yank the current line |
yw | Yank from the cursor position to the end of the current word |
y0 | Yank from the cursor position to the start of the current line |
y$ | Yank to end of current line |
Y | Yank to end of current line |
ygg | Yank from the cursor position to the start of the document |
yG | Yank from the cursor position to the end of the document |
Pasting
Pasting content generally takes two forms, where the content is insert either before or after the current cursor position:
Command | Action |
---|---|
p | put the text [from register x] after the cursor N times |
P | Paste before the cursor |
Joining Lines
Editing documents inherently causes lines to shrink and grow as words are added and/or deleted. It doesn't take long for lines of paragraphs of text to start looking ragged.
Vim provides commands for joining two lines together, as well as for formatting entire paragraphs to get the text looking nice and neat again.
Command | Action |
---|---|
J | Join N lines; default is 2 |
gJ | join lines without inserting space |
gwip | Reflow the current paragraph |