Skip to content

Linux Foundations / Find your way around Linux ยท Step 52 of 63

Lesson: Vim Inserting and Appending TextSwitch
โ† Linux Foundations

Shell and files

Vim Inserting and Appending Text

Choose the right way into Insert mode so you can add text exactly where you need it.

There is more than one door into Insert mode. Each key puts you in a different place, so picking the right one saves extra cursor movement. Press all of these from Normal mode, and press Esc when you have finished typing.

Insert or append at the cursor

  • i inserts before the character under the cursor.
  • a appends after the character under the cursor.

Suppose the line is ct and the cursor is on c. Press a, type a, then press Esc:

text
cat

Pressing i at the same spot would have given act.

Start or end of the line

  • I inserts before the first non-blank character of the line.
  • A appends at the end of the line.

A is very useful for adding something to a configuration line, wherever the cursor currently is on it.

text
PATH=/usr/bin

Press A, type :/opt/tools/bin, press Esc:

text
PATH=/usr/bin:/opt/tools/bin

Open a new line

  • o opens a new line below the current one and starts Insert mode.
  • O opens a new line above it.

You do not need to move to the end of the line first.

Replacing instead of inserting

R enters Replace mode, where typing overwrites existing characters. To change a single character, press r followed by the new one. After r you stay in Normal mode.

Repeating what you typed

A count before an insert command repeats the text. Type 40i- and press Esc to get a line of forty dashes.

The . command repeats your last change. If you used A; and Esc to add a semicolon, move to another line and press . to add one there too.

When things look wrong

If your typing moves the cursor or deletes text, you were still in Normal mode. Press Esc, then u to undo.

In some very small vi setups, arrow keys pressed in Insert mode may type letters such as A or B. Leave Insert mode before moving, or install full Vim.

Pasting from the clipboard

When you paste into Vim through your terminal, enter Insert mode first. Otherwise the pasted characters may be run as commands. If auto-indent makes pasted code drift to the right, :set paste turns that off, and :set nopaste restores it.

Try it

  1. Run vim practice.txt.
  2. Press i, type second, press Esc.
  3. Press O, type first, press Esc.
  4. Press j, then o, type third, press Esc.
  5. Press A, type line, press Esc.
  6. Press k, then ., and see the same text appended there.
  7. Save and quit with :wq, then run cat practice.txt.

Check your understanding

Vim Inserting and Appending Text

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.