Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

File and Buffer Operations with Vim

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!_

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Apr 03, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

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

    1. Open Vim:

      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:

      :q
      
    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:

      :q!
      
    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:

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

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

      i
      A few words of text
      

      Then press Esc

    15. Attempt to switch to the last buffer with:

      :blast
      
    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:

      :ls
      
  2. Challenge

    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:
      :w
      
    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:
      :qa!
      
  3. Challenge

    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:

      i
      This is a crash test file
      

      Press Esc

    3. Save the buffer to disk with:

      :w
      
    4. Add another line to the buffer with:

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

      ggVG
      U
      
    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:

      :wq
      
    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans