Editing


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:

CommandAction
ddDelete the current line
dwDelete from the cursor position to the start of the next word
d0Delete from the cursor position to the start of the current line
d$Delete to end of current line
dggDelete from the cursor position to the start of the document
dGDelete 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:

CommandAction
yyYank the current line
ywYank from the cursor position to the end of the current word
y0Yank from the cursor position to the start of the current line
y$Yank to end of current line
YYank to end of current line
yggYank from the cursor position to the start of the document
yGYank 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:

CommandAction
pput the text [from register x] after the cursor N times
PPaste 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.

CommandAction
JJoin N lines; default is 2
gJjoin lines without inserting space
gwipReflow the current paragraph