6. Vim Inserting and Appending Text

In Vim, you'll primarily work in two modes: Normal mode for executing commands and Insert mode for typing text. To switch from Insert mode back to Normal mode, simply press the Esc key.

There are several commands to enter Insert mode, each placing the cursor at a different starting position for your text entry.

Basic Insert Commands

The most fundamental way to start typing is with the i command.

  • iinsert text before the current cursor position.

This command switches you to Insert mode, allowing you to type directly into the file.

Vim Append vs Insert

A common point of comparison is vim append vs insert. While both enter Insert mode, their starting points differ relative to the cursor. Understanding the vim insert vs append distinction is key to efficient movement and editing.

  • aappend text after the current cursor position.
  • IInsert text at the beginning of the current line.
  • AAppend text at the end of the current line.

Using a instead of i saves you a keystroke (moving the cursor one space to the right before inserting). Similarly, A is a powerful shortcut to immediately start typing at the end of a line. Mastering the vim append commands is a significant step in improving your editing speed.

How to Vim Add Line

When you need to add new lines of text, you don't have to manually press Enter at the end of a line. Vim provides dedicated commands to open lines and immediately enter Insert mode.

  • oopen a new line below the current line and enter Insert mode.
  • OOpen a new line above the current line and enter Insert mode.

These commands are extremely useful when you need to quickly vim add line while coding or writing.

Tip: You can prefix these commands with a number to repeat them. For example, typing 3o in Normal mode will open three new blank lines below the current one and place you in Insert mode on the first of these new lines.

Sign in to save your learning progress

Sign in

Exercises

Practice is essential for mastering Vim. The following lab provides a hands-on environment to reinforce your understanding of Vim's text editing capabilities.

  1. Edit Text Files in Linux with Vim and Nano - Practice creating files, editing text, saving files, and navigating with both vi/vim and nano. This lab will help you master the fundamental skills of using Vim's Normal and Insert modes.

Applying these concepts in real scenarios will help you build confidence with text editing in Linux using Vim.

Quiz

Which key enters Insert mode before the cursor? Please answer with a single lowercase English letter.