File and Buffer Operations with Vim

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

The ability to work with files and buffers in Vim is just as important as the actual editing in Vim you’ll do so often in your career. Most people can manage to edit a file or a buffer, but having the skills to adroitly handle the sometimes many buffers that will be in use is valuable, to say the least.

In this hands-on lab you’ll gain some practice opening new and existing files, open multiple files and navigate among the buffers handily.

You’ll also be working with saving and exiting, exiting without saving changes, then causing a crash to experience recovering from one. _Knowing how to do this sets you apart from most casual Vim users!_

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Open New and Existing Files, Open Multiple Files at Once, Then Navigate Amongst Open Buffers (File Contents)
  1. Open Vim:

  2. Press the i key and note the bottom left says you are in Insert Mode. Type in some text such as: Mimsy were the borogroves or It was a dark and stormy night.

  3. When done entering the text, use the Esc key to return to Command Mode

  4. Write the new file to disk with:

    :w myfirstnovel.txt
  5. Then exit Vim with:

  6. Next open a read-only (root-only) file with:

    vim /etc/hosts
  7. Now "accidentally" change the file by pressing the i key to go to Insert Mode, this will cause an alarming error message to appear:

    W10: Warning: Changing a readonly file
  8. Use Esc to return to Command Mode and u to ensure that no changes have been made, indicated by a message at the status line of Already at oldest change.

  9. And just in case, exit the file with:

  10. Now open three new named buffers with:

    vim file1 file2 file3
  11. Vim will load and you can verify you are in the blank named buffer file1.

  12. List out the buffers using both of the commands:

  13. Rotate through the buffers until you reach file1 again with:

  14. Add a few words to the file1 buffer with:

    A few words of text

    Then press Esc

  15. Attempt to switch to the last buffer with:

  16. You will see an error message:

    E37: No write since last change (add ! to override)
  17. Hide the changed buffer and move to the last open buffer with:

    :hide blast
  18. List out the open buffers again with:

Importing External Command Output, Using External Commands on Buffers, Writing Buffer Contents to External Commands

Note: You must have done Task 1 before continuing with Task 2.

  1. Switch to the file2 buffer with:
    :b file2
  2. Add the same text as you did in file1 and hit Esc.
  3. Attempt to go to the file3 buffer, and when you get the error message use:
    :hide b file3
  4. Add the same text to the file3 buffer and hit Esc.
  5. Confirm the changed, but not saved, status of all three buffers by inspecting the output of the :ls command.
  6. Write the contents of the file3 buffer to disk with:
  7. Confirm the status of file3 with the :ls command, there should be no + in the 9th column.
  8. Now save file3 as a new file with:
    :saveas file4
  9. Inspect the :ls output to see what happened.
  10. Switch to the file1 buffer with:
    :b 1
  11. Write the file1 buffer contents to file5 on disk with:
    :w file5
  12. Inspect the :ls output to see what happened.
  13. Run a listing on the directory to see if file5 exists:
    :!ls -l

    Press Enter.

  14. Verify the status of the file1 buffer by pressing Ctrl-g and then run :ls.
  15. Quit all unsaved buffers and exit Vim with:
Cause and Recover from a Vim Crash
  1. Open Vim with a named buffer using:

    vim crashtest.txt
  2. Add some text to the buffer with:

    This is a crash test file

    Press Esc

  3. Save the buffer to disk with:

  4. Add another line to the buffer with:

    This is a second line
  5. Select and set all of the file contents to uppercase letters with:

  6. Add a third line to the file with:

    :$ read !date
  7. Without saving, kill the Vim session with:

    :!pkill -9 vim

    Note: This will kill the Vim process and verify this happened by dropping you to the shell and showing the process was Killed. There may be a notice that no writes were made since the last change.

  8. Verify that Vim is not still active with:

    ps aux | grep vim
  9. The output should not list more than the grep for vim.

  10. Start up Vim again, specifying the previous file:

    vim crashtest.txt
  11. You’ll see the dreaded E325: ATTENTION error message and have various options at the bottom of the screen.

  12. Remembering the contents of the buffer when we killed it, choose the (R)ecover option.

  13. Read the message, paying close attention to the admonition You may want to delete the .swp file now, and press Enter.

  14. Save the file to disk and exit with:

  15. Edit the file again with:

    vim crashtest.txt
  16. Upon seeing the now less-dreaded E325 error, you can now choose to (D)elete the .swp file as directed.

  17. Alternatively, you can choose to (Q)uit and then when out of Vim, verify the existence of, and delete the .swp file with:

    ls -l
    (Verify the file exists)
    rm -f .crashtest.txt.swp
  18. Now try editing the file again, and you will not see the error message anymore.

Additional Resources

You're feeling very good about your Vim skills, and want to ensure that you pick up how to edit multiple files and switch between them for cut and paste operations, etc.

You're also concerned that you have seen crashes occur in Vim and the messages are confusing, to say the least. You want to ensure that you know how to recover from crashes, both for yourself and to help co-workers.

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?