Vim - Editing
Vim - Essential Editing Commands
Section titled “Vim - Essential Editing Commands”Vim’s power shines in its editing capabilities, primarily driven by commands in Normal mode that transition into Insert mode. This chapter covers fundamental commands for inserting, changing, and manipulating text.
- Insert text
- Append text
- Open a new line
- Substitute characters or lines
- Change text
- Replace characters
- Join lines
Most editing commands are initiated from Normal mode. Remember to press Esc to return to Normal mode from Insert mode.
Insert Text Before Cursor
Section titled “Insert Text Before Cursor”To insert text at the current cursor position:
- Press
i(lowercase) in Normal mode. - Vim switches to Insert mode, and you can start typing.
- The text will appear before the character the cursor was on.
i " Enter Insert mode before the cursorInsert Text at the Beginning of the Line
Section titled “Insert Text at the Beginning of the Line”To quickly move to the beginning of the current line and start inserting text:
- Press
I(uppercase) in Normal mode. - The cursor moves to the first non-blank character of the line (or column 0 if the line is empty or consists only of whitespace, depending on configuration), and Vim enters Insert mode.
I " Enter Insert mode at the beginning of the lineAppend Text After Cursor
Section titled “Append Text After Cursor”To append text immediately after the current cursor position:
- Press
a(lowercase) in Normal mode. - The cursor moves one position to the right, and Vim switches to Insert mode.
a " Enter Insert mode after the cursorAppend Text at the End of the Line
Section titled “Append Text at the End of the Line”To quickly move to the end of the current line and start appending text:
- Press
A(uppercase) in Normal mode. - The cursor moves to the end of the current line, and Vim enters Insert mode.
A " Enter Insert mode at the end of the lineOpen New Line Below Cursor
Section titled “Open New Line Below Cursor”To insert a new blank line below the current line and start typing on it:
- Press
o(lowercase) in Normal mode. - A new line is created below the current line, the cursor moves to it, and Vim enters Insert mode.
o " Open a new line below and enter Insert modeOpen New Line Above Cursor
Section titled “Open New Line Above Cursor”To insert a new blank line above the current line and start typing on it:
- Press
O(uppercase) in Normal mode. - A new line is created above the current line, the cursor moves to it, and Vim enters Insert mode.
O " Open a new line above and enter Insert modeSubstitute Text
Section titled “Substitute Text”Substitute commands delete text and then enter Insert mode.
- To substitute a single character under the cursor: Press
s(lowercase) in Normal mode. The character under the cursor is deleted, and Vim enters Insert mode. - To substitute the entire current line: Press
S(uppercase) or useccin Normal mode. The entire line is deleted, and Vim enters Insert mode on a blank line.
s " Delete character under cursor and start insertS " Delete current line and start insert (same as cc)Change Text
Section titled “Change Text”Change commands are powerful as they combine deletion with entering Insert mode. They typically take a motion.
cc(orS): Delete the current line and enter Insert mode.C(orc$): Delete from the cursor position to the end of the line and enter Insert mode.cw: Change word. Deletes from the cursor to the end of the current word and enters Insert mode.c<motion>: Change the text covered by<motion>. For example,cawchanges ‘a word’,ciwchanges ‘inner word’,c2jchanges the current line and the next two lines.
Example: If the cursor is on the ‘b’ of “brown fox”:
cw changes “brown” and lets you type a replacement.
C changes “brown fox” and lets you type a replacement for the rest of the line.
Replace Text
Section titled “Replace Text”Replace commands overwrite existing text without shifting subsequent characters.
- To replace a single character under the cursor: Press
r(lowercase) in Normal mode, then type the character you want to replace it with. Vim stays in Normal mode. - To enter Replace mode: Press
R(uppercase) in Normal mode. Vim enters Replace mode (indicated at the bottom). Characters you type will overwrite existing characters. PressEscto return to Normal mode.
r<char> " Replace single character under cursor with <char>R " Enter Replace modeIn Replace mode, typing ‘X’ over ‘abc’ would result in ‘Xbc’. The status line will typically show -- REPLACE --.
Join Lines
Section titled “Join Lines”To join the current line with the next line:
- Press
J(uppercase) in Normal mode. - This joins the current line with the line below it, removing the indentation of the second line and usually inserting one space (or two if the first line ends with a period, question mark, or exclamation mark).
- To join lines without inserting any space, use
gJ.
J " Join current line with the next, adding a spacegJ " Join current line with the next, without adding a space