Skip to main content

Advanced Editing with Vim

Hands-On Lab

 

Photo of Ross Brunson

Ross Brunson

Linux Training Architect II

Length

00:30:00

Difficulty

Beginner

The Vim editor is extremely capable and has many features that merit attention, chiefly to enhance the productivity of its users. While there are alternatives in the open and proprietary worlds, it's still the most-shipped and included editor in the world of Unix and Linux. In this lab, we'll use more advanced features of the editor, including additional search and replacement of text, as well as a visual mode that allows multi-dimensional selection of lines, text, and columns. We'll also learn about the use of Vim windows, how to split the display into windows, and navigate and configure them to suit your style. Additionally, we'll get an awareness of the diff command, how diff mode is incorporated into Vim, how to invoke it to resolve changes between files, and how to use vimdiff mode, as well as how to learn even more about Vim.

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.

Advanced Editing with Vim

Introduction

The Vim editor is extremely capable and has many features that merit attention, chiefly to enhance the productivity of its users. While there are alternatives in the open and proprietary worlds, it's still the most-shipped and included editor in the world of Unix and Linux. In this lab, we'll use more advanced features of the editor, including additional search and replacement of text, as well as a visual mode that allows multi-dimensional selection of lines, text, and columns. We'll also learn about the use of Vim windows, how to split the display into windows, and navigate and configure them to suit your style. Additionally, we'll get an awareness of the diff command, how diff mode is incorporated into Vim, how to invoke it to resolve changes between files, and how to use vimdiff mode, as well as how to learn even more about Vim.

Solution

Log in to the lab server using the credentials provided:

ssh cloud_user@<PUBLIC IP ADDRESS>

Use the Vim Editor for Advanced Search/Replace, Select Items Visually, and Use and Navigate Windows

Conduct Advanced Search/Replace

  1. Copy the necessary file packages:

    cp /usr/share/doc/packages/zip/README ./zip-readme.txt
  2. Open the file:

    vim zip-readme.txt
  3. Search for Zip:

    /Zip
  4. Globally replace all instances of Zip with ZIPPY:

    :%s//ZIPPY/g

    We should see it did 33 substitutions on 26 lines.

  5. Enter u to undo the changes.

  6. Search for Zip and replace it with ZIPPY. This time, include a c at the end of the command in order to confirm each change:

    :%s/Zip/ZIPPY/gc

    We'll see the following options:

    • y: Yes, change the one we are on
    • n: No, do not change the one we are on
    • a: Change them all, and then quit
    • q: Quit
    • l: Only change the one we are on, and then quit
    • ^E: Scroll down
    • ^Y: Scroll up
  7. Globally search for any line that starts with Zip and change it to STARTSWITHZIP:

    :g/^Zip/s/Zip/STARTSWITHZIP/g

    What we'll see is it didn't just change it in the beginning — it also changed any instances of Zip in the entire line.

  8. Enter u to undo the changes.

  9. Search for any line that starts with Zip and onlyh change the first word to STARTSWITHZIP:

    :g/^Zip/s/Zip/STARTSWITHZIP/
  10. Search and replace Zip with RANGE between lines 1 and 40:

    :1,40s/Zip/RANGE/g
  11. Type gg to get back to the top of the file.

Select Items Visually

  1. Type v to enter visual mode. This highlights individual words.

  2. Arrow over to highlight the words Testing for Zip.

  3. Type y to "yank" (i.e., copy).

  4. Arrow down to the space above the copyright line.

  5. Type p, which will paste the text into the space.

  6. Type V and select the entire Testing for Zip... paragraph. V highlights entire lines.

  7. Type y to "yank" (i.e., copy).

  8. Type p to paste it.

  9. Scroll down to the Q&A portion of the file.

  10. Hit Ctrl+V, which allows you to select columns.

  11. Select all the first few sets of Q and A.

  12. Enter x to delete them.

  13. Hit Ctrl+V and select the spaces they left.

  14. Enter x to delete the spaces.

  15. Enter u to undo deleting the spaces and the Qs and As.

  16. Hit Ctrl+V and select the first few sets of Q and A.

  17. Hit Shift+~ to make them all lowercase.

  18. Quit without saving any changes:

    :q!

Use and Navigate Windows

  1. Open three sample files horizontally in window mode:

    vim -o FILE1 FILE2 FILE3

    Note: Using -o opens the files in a horizontal layout, while -O opens them in a vertical layout.

  2. Load the zip-readme.txt file:

    :e zip-readme.txt
  3. Hit Ctrl+WW to move to the FILE2 window.

  4. Load .profile:

    :e .profile
  5. Hit Ctrl+WW to move to the FILE3 window.

  6. Load .bashrc:

    :e .bashrc
  7. Hit Ctrl+WW to move to the FILE1 window.

  8. Hit Ctrl+W and then Shift+H to split the windows to the orientation of the first window changes to a portrait layout while the other two become stacked to the right.

  9. Hit Ctrl+WW to move to the second window.

  10. Hit Ctrl+WR to rotate them so they switch spots. Now, .profile should be on the bottom and .bashrc should be on the top.

  11. In the .profile window, type v to enter visual mode.

  12. Select the export line.

  13. Hit y to copy.

  14. Hit Ctrl+WW to move to the zip-readme.txt window.

  15. Scroll down to the blank space above the copyright line.

  16. Hit p to paste it in.

  17. Hit :qa! to quit out of all the open files without saving any changes.

  18. Open three sample files vertically in window mode:

    vim -O FILE1 FILE2 FILE3
  19. In order to get rid of each window/buffer individually, you can hit :wq. Then, hit q to quit out of the last one.

Use Vim's diff Mode to Display, Compare, and Resolve Differences Between Files

  1. Open the zip-readme.txt file:

    vim zip-readme.txt
  2. Change "update" in the first sentence to "change".

  3. Change "fully" in the second paragraph to "even".

  4. Enter the following to save a copy of the buffer as a new file:

    :w zip-changed.txt
  5. Even though we saved a copy, we're still in the same original file. Quit and override changes:

    :q!
  6. Compare the files:

    diff zip-readme.txt zip-changed.txt

    This will show us what was changed where.

  7. Run the following:

    ls -l `which vimdiff`

    This will populate the ls -l with the full path and file name to vimdiff, which tells us it's a symlink to vim.

  8. Open zip-readme.txt:

    vim zip-readme.txt
  9. View both files:

    :vsplit zip-changed.txt
  10. See the differences between the windows:

    :windo diffthis
  11. With the first change highlighted, enter the following:

    :diffput

    We should see it retires the other line, making it match the change in the window we're on.

  12. With the next change highlighted, enter the following:

    :diffget

    We should see this retires the line we were on, making it match the other change.

  13. Write and quit all files:

    :wqa
  14. Run the following:

    diff zip-readme.txt zip-changed.txt

    Since there aren't any changes, nothing will happen.

  15. See more information about Vim:

    vimtutor

Conclusion

Congratulations on successfully completing this hands-on lab!