Creating and Modifying a File with Vim

30 minutes
  • 6 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a new file

Using the vim text editor, create a new file called notes.txt in the cloud_user home directory. Enter the text "Beginning of Notes File" at the top of the document. Leave two blank lines under this top line. Save and close the file.

The process is:

vi notes.txt
i (insert)
  Text - Beginning of Notes File
(2 blank lines)
:wq!
Send Data to the notes.txt file

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.

This can be accomplished via:

 cat /etc/redhat-release >> notes.txt
Modify the notes.txt File

Open the notes.txt file for editing again. Place the cursor before the openening parentheses around the word "Core." Using a keyboard shortcut, delete the text from the cursor position to the end of the line. Leave two blank lines under this line. Save and close the file.

The following process will meet the criteria:

vim notes.txt
(cursor to 'core')
SHIFT D (or d$) remove cursor to end of line
o - blank line
enter (2nd line)
:wq!
Send More Data to the File, and Modify Its Contents

Using the free -m command and output redirection, send the output of the command to the end of the notes.txt file, taking care to append the contents so as to not overwrite the file.

Open notes.txt for editing. Move the cursor to the line that begins with "Swap." Using a keyboard shortcut, delete this entire line. Leave two blank lines after the line that begins with "Mem".

Follow this process:

free -m >> notes.txt

vim notes.txt
move cursor to Swap line
dd (delete line)
o - blank line
enter (2nd)
SHIFT: 3 (3rd line of file)
i - insert
enter (blank line)
:wq!
Enter New Text into the File

Using a keyboard shortcut, jump to line 3 of the file, then enter the text: "This is a practice system." Leave a blank line after this text has been entered. Save and close the file.

You can accomplish this via:

 vim notes.txt
 SHIFT: 3 (moves to 3rd line)
 i (insert)
 This is a practice system
 (enter for blank line)
 :wq!
Finalize the Notes File

Using the dbus-uuidgen --get command, send the output of this command (which will retrieve the system’s dbus unique ID) to the end of the notes.txt file. Take care to append the contents so as to not overwrite the file. Open notes.txt in vim, and use a keyboard shortcut to jump to the end of the file. At the beginning of the line that contains the dbus ID, enter in the following text: "Dbus ID = " so that there is a space between the equal sign and the Dbus ID number. Save and close the file. Hand the system over for grading.

dbus-uuidgen --get >> notes.txt

vim notes.txt
SHIFT G (end of file)
i - insert
Dbus ID = (before ID)
:wq!

Additional Resources

Keep the Vim cheat sheet that is available with the LPIC-1 Exam 101 course on hand if you need to quickly look up some keyboard shortcuts. ( Note: to write and quit vi/vim you use the :wq )

You are preparing a system report for your audit department. Your report should contain the following characteristics:

  • Create a file in /home/cloud_user called notes.txt to contain the information for submitting.
  • At the top of the new file, add the text 'Beginning of Notes File' followed by two blank lines, save and close the file.
  • Add the information from the /etc/redhat-release file, appending it to notes.txt.
  • Edit that new information, removing the '(Core)' value at the end of the line, and add two more blank lines right after. Then save and close the file.
  • Next, capture the free memory on your system and append that information to notes.txt.
  • Edit that new information: delete the entire line beginning with 'swap', and then add two blank lines under it.
  • Before saving, place your cursor on the 3rd line of the file on your screen, add the text 'This is a practice system', add a blank line right under it, then save and close the file.
  • Finally, append the output of the dbus-uuidgen --get to notes.txt.
  • Edit that new information, and at the beginning of the UUID line, place 'Dbus ID = '. Then save and close the file.

Once you verify the file contains everything instructed, you can turn it over to your team for audit.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?