Manipulating Text in Vim

30 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Editing text, in a precise, specific, and efficient manner is the hallmark of a good system administrator, developer or anyone who works with text on a daily basis. Being able to get done what needs doing in the most direct and time-saving way is a wonderful skill to possess.

In this hands-on lab, you will gain valuable practice in inserting, copying, pasting, selecting, deleting, changing, and altering text. You also be using undo and redo to bring your text-editing skills up the next level.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Insert and Append Text and Lines, Yank and Paste Text and Lines, Then Delete Text and Lines
  1. Open the Vim editor:
    vim
  2. Create a working file (and open it for editing) with:
    :help
    :w ~/vimhelp.txt
    :q
    :e ~/vimhelp.txt
  3. Go to the top of the file with gg and then navigate to the line that starts with this text:
    Get out of Vim
  4. Press o to add a line under the current line.
  5. On that line, add the following text, taking care to line up the : with the other instances of : on other lines:
    ATTENTION:  Do NOT Use killall -9 vim to Exit Vim!
  6. When added, hit the ESC key to return to Command Mode. Use ^ to go back to the front of the word ATTENTION, then 0 (zero) to go to the very beginning of the line.
  7. Next, place the cursor on the empty line between your current line and the one that reads Jump to a subject, and delete the empty line by pressing dd.
  8. Now copy the ATTENTION line by moving to it and pressing yy.
  9. Go to line that starts with Jump Back and use p to paste the copied line below it.
  10. Then place your cursor on the blank line, in between the current line and the one that begins with Get specific help, and press the yy keys to copy the blank line.
  11. Then create a blank line by pressing P.
  12. Now insert a range of numbers automatically using:
    :put =range(1,10)
Visually Select Text and Lines, Change, Replace and Alter Text, and Use Undo and Redo

Note: You must have completed the steps in Task 1 in order to do Task 2.

  1. Position your cursor on the 4 in the list of newly added numbers, and copy and paste that line with:
    yy
    p
  2. With your cursor on the bottom-most 4, select from there to the number 10 using:
    Ctrl-v
    Down Arrow   (Make sure to select the 0 in 10 too)
  3. Now increment those numbers with Ctrl-a so that the 4 your cursor is on increments to 5, and the list now ends with 11.
  4. Move your cursor to the numeral 1 on the first line of the eleven numbered lines.
  5. Select lines numbered 1-9 by pressing:
    Ctrl-v
    8j
  6. Add a leading zero to the lines numbered 1 through 9 with:
    Shift-i     (aka I)
    0
    ESC
  7. Re-select the nine lines by pressing gv.
  8. Extend the selected area to include all eleven numbered lines, all numbers. (The cursor should be blinking on the last 1 in 11.)
  9. With all eleven lines and all numerals selected, add the same text to all eleven lines with:
    Shift-a     (aka A)
    Space
    This will be 11 numbered lines followed by this text.
    ESC
  10. Now navigate to the line that begins with:
    Get Specific Help...
  11. Position your cursor on the s of specific and change the word to be particular using:
    ESC
    cw
    particular
    ESC
  12. Now join that line and the line below it into a long paragraph with by pressing J.
  13. Move your cursor to the end of the line, with the $ character. Your cursor should be on the . after command.
  14. Now that you have done a number of changes, use the u key to undo several of your changes, the use 3u to undo 3 more changes. Continue to use u until you receive the message:
    Already at oldest change
  15. Then use Ctrl-r to redo your changes until you receive the message:
    Already at newest change
  16. Quit your vimhelp.txt file, saving your changes by pressing:
    ESC ESC
    :x

Additional Resources

Your team leader is impressed with how far you've come since first encountering Vim. But before you're turned loose on any documents or code, they want to see that you can use all the text manipulation tools that Vim has to offer.

They have given you a file to work with, and a set of instructions to follow that will work you through the needed actions. Don't worry, you'll do great!

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?