Skip to main content
Racing Turtles

Vim (Advanced Insert) - Finer Text Insertion

Learn how to control where you want to insert text.




Introduction #

In the last tutorial, you learned how to begin using Vim. You learned that Vim is a modal editor. Vim begins in Normal mode, but to edit text, you need to enter Insert mode.

The last tutorial detailed how to enter Insert mode with the i key in Normal mode. There are many ways to enter Insert mode and insert text in Vim. This tutorial will detail more ways you can insert text in Vim.

Requirements #

Lack Requirements?

If you lack the requirements, check the previous tutorial for instructions on how to install and get started using Vim.

Getting Started #

Open Vim. This can be done in a terminal if your system lets you run the vim command. Otherwise, you should look for gvim.

$ vim

The screen should look similar to this:

Vim start screen

If you haven't specified a file name, enter the following command:

:write insert.txt

Disclaimer

You might not be able to copy and paste the :write insert.txt command in Vim. Type the command in its entirety then press <Enter> to execute it.

This command saves the current buffer to a file named insert.txt (as covered in the previous tutorial).

Standard Insert #

The simplest way to enter insert mode is to press i. Enter the following:

i

This places the cursor directly before the character that the cursor currently highlights. For example, in this setup, pressing i will enter Insert mode and let you insert characters between the k and d in markdown:

Vim text says markdown and cursor is over the letter
d

In an empty file, this will let you insert text into the current buffer. Enter the following text in the current buffer:

this is text

Press <Esc> to leave Insert mode and go back to Normal mode. The cursor should highlight the last letter in the line, like this:

Vim text says this is a line and cursor highlights last t in
text

Cursor Movement

To move the cursor, use the <Up>, <Down>, <Left>, and <Right> arrow keys to move the cursor one character up, down, left, and right respectively.

Move the cursor to the space between is and text as shown:

Vim text says this is text and cursor on space between is and
text

Enter Insert mode with i. Insert the word "only" after "is" as shown:

Vim text says this is only text and cursor on space between only and
text

Press <Esc> to return to Normal mode. The cursor should highlight the last letter of "only" as shown:

Vim text says this is only text and cursor on last letter of
only

Insert at Beginning #

To insert a character directly before the first non-whitespace character of a line, type I (<Shift-i>).

Enter insert mode and move the cursor to directly before the first non-whitespace character in the line by typing I (<Shift-i>). The cursor should be placed on the "t" in "this" as shown:

Vim text says this is only text and cursor on first character of the
line

Type "I think" followed by a space. The line should look like the following:

Vim text says I think this is only text and cursor on t in
this

Press <Esc> to switch to Normal mode. The line should look like this:

Vim text says I think this is only text and cursor on space between think and
this

Append #

To append text in Vim, use a or A (<Shift-a>). Both commands are different and place the cursor at different positions.

End of Line #

To add text to the end of the line, press A. The line should look like this:

Vim text says I think this is only text and cursor directly after last letter
on line

Add a period (.) to the end of the sentence by typing .. The line should look like this:

Vim text says I think this is only text period and cursor directly after
period

Press <Esc> to return to Normal mode. The line should look like this:

Vim text says I think this is only text period and cursor on
period

After Any Character #

To add text after any character, highlight the character with the cursor in Normal mode then press a.

You will add don't after I in the beginning of the sentence. The line should look like this:

Vim text says I think this is only text period and cursor on
period

To move the cursor to the first letter of the line, press 0. The line should look like this:

Vim text says I think this is only text period and cursor on first
I

Press a to go into insert mode with the cursor placed directly after the first "I" in the sentence. The line should look like this:

Vim text says I think this is only text period and cursor on space between I
and think

Type a space followed by don't. The line should look like this:

Vim text says I don't think this is only text period and cursor on space
between don't and think

Press <Esc> to switch to Normal mode. The line should look like this:

Vim text says I don't think this is only text period and cursor on t in
don't

Open Line #

To open a line below or above an existing line, use o and O (<Shift-o>). Both commands are different and open a line in different places.

You will add two sentences on separate lines. The line should look like this:

Vim text says I don't think this is only text period and cursor on t in
don't

Open Line Below #

Press o to enter Insert mode and add a line directly below the current line. The line should look like this:

Vim text says I don't think this is only text period and cursor on new line
below

Type This is a text file followed by a period (.). The lines should look like the following:

Vim text says I don't think this is only text period new line This is a text
file period and cursor directly after period of second
line

Press <Esc> to return to Normal mode. The lines should look like this:

Vim text says I don't think this is only text period new line This is a text
file period and cursor directly after period of second
line

Open Line Above #

Press O to enter Insert mode and add a line directly above the current line. The line should look like this:

Vim text says I don't think this is only text period new line new line This is
a text file and cursor on blank line

Type This is much more than text followed by a period (.). The lines should look like this:

Vim text says I don't think this is only text period new line This is much
more than text period new line This is a text file and cursor directly after
period at end of second line

Press <Esc> to return to Normal mode. The lines should look like this:

Vim text says I don't think this is only text period new line This is much
more than text period new line This is a text file and cursor on period at end
of second line

Conclusion #

In this tutorial, you learned how to enter insert mode in Vim. You learned how to place your cursor before or after a highlighted character, at the beginning or end of a line, and in a new line directly above or below. With this knowledge, you can control where you want to insert text into a text file, which will enable you to spend fewer keystrokes inserting text.

Vim empowers you to improve your edit workflow. Edits require fewer keystrokes and complexity. Combined with Vim's language-like keybinding design, Vim accelerates and eases your edit workflow.

In future tutorials, you'll learn how to leverage Vim for even more advanced insertion and many more.

License #

This work is licensed under CC BY 4.0.