Skip to main content

Creating and Modifying a File with Vim

Hands-On Lab


Photo of Kenny Armstrong

Kenny Armstrong

Linux Training Architect II in Content





Knowing how to modify files at the command line is an essential skill for any Linux administrator. This learning activity will focus on using the Vim text editor to practice creating a new file, adding text to the file, and then modifying that text. We will also practice some of the basic keyboard shortcuts to change the text of this file.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating and Modifying a File with Vim


Welcome to Vim, and a lab where we'll walk through some basics of using it. We're going to create a text file with Vim, and edit it.

Create a New File

We're going to create a new file called notes.txt in /home/cloud_user.

vim notes.txt

Now, to add the text Beginning of Notes File, we need to get into insert mode, by pressing i. We can start typing now once we're in insert mode.

Leave two blank lines after Beginning of Notes File. Now, to save the file and quit Vim, we have to first hit Esc (to get out of insert mode), type wq! (write and quit).

Send Data to notes.txt

Using the cat command and output redirection, send the contents of the /etc/redhat-release file to the end of the notes.txt file, taking care to append the contents so as to not overwrite the file (using >>, not >)

Run this to append notes.txt with the contents of /etc/redhat-release:

cat /etc/redhat-release >> notes.txt

Modify notes.txt

Let's open notes.txt again for editing. We'll place the cursor before the opening parentheses around the word Core, and use a keyboard shortcut to delete the text from there to the end of the line. We'll leave two more blank lines at the end of the file, then save and quit again.

Here are all of the steps to do that:

  1. Open the file:
    • vim notes.txt
  2. Use the arrow keys to moved to the beginning parentheses before Core
  3. Remove text from the cursor's position to end of line:
    • SHIFT D (or d$)
  4. Create a blank line under where the cursor is
    • o
  5. Hit Enter to create the second blank line
  6. Hit Esc to leave insert mode
    • Hitting o added a blank line, but also put us in insert mode
  7. Write and quit:
    • wq!

Send More Data to the File, and Modify Its Contents

Now we're going to send free -m output to the end of notes.txt, edit notes.txt again, delete the last line of the file, and add two more blank lines to the end of the file.

Then, we're going to jump to the third line of the file, enter some text, and make another blank line afterward.

Here are all of the steps to do that:

  1. Append the notes.txt:
    • free -m >> notes.txt
  2. Edit notes.txt:
    • vim notes.txt
  3. Navigate to the Swap line with arrow keys.
  4. Delete the line:
    • dd
  5. Create a blank line under where the cursor is (and put us in insert mode):
    • o
  6. Hit Enter to create the second blank line.
  7. Hit Esc to get out of insert mode.
  8. Get to the 3rd line of file:
    • :3
  9. Get back into insert mode:
    • i
  10. Type This is a practice system.
  11. Hit Enter to make another blank line.
  12. Hit Esc to leave insert mode.
  13. Write and quit:
    • wq!

Finalize the Notes File

We're going to dump one last bit of text into the file, then edit it again. We'll take the output from dbus-uuidgen --get, append it to notes.txt then edit notes.txt so that the text Dbus ID = is in the beginning of the new appended line.

We'll do it like this:

  1. Append the notes.txt:
    • dbus-uuidgen --get >> notes.txt
  2. Edit notes.txt:
    • vim notes.txt
  3. Get right to the end of the file:
    • G (Capital G)
  4. Get into insert mode:
    • i
  5. Type the text "Dbus ID = " (with a space between the equals sign and the dbus-uuidgen --get command's output)
  6. Write and quit:
    • wq!


Well, we've done it. We've learned some nifty maneuvering tricks inside the Vim text editor. Knowing how to do these will come in very handy down the road, when we need to get some work done on text files in a terminal. Congratulations!