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

Advanced Operations in Vim

Vim has a lot of features and options. Some of the more advanced features can be productivity game changers, such as using sessions, split windows, tabs, and the difference mode. In this longer, but feature-filled, hands-on lab, you'll work with saving and loading Sessions to help with switching gears and projects. Then you'll be using tabs to visually manage your editing, and then utilizing split windows to make it easier to see more buffers at once and help you be more productive! Finally, you'll get some practice comparing and merging file contents, and setting some useful options in the Vim resource configuration file.

Google Cloud Platform icon

Path Info

Clock icon Intermediate
Clock icon 45m
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

    Work with Saving and Loading Sessions

    1. Create a directory to store your sessions in:
      mkdir -p ~/.vim/sessions
    2. Open up Vim:
    3. Save a default session for troubleshooting purposes:
      :mksession ~/.vim/sessions/defaultvim.session
    4. Quit Vim:
    5. Open up two files in split windows:
      vim -O /etc/hosts /etc/resolv.conf
    6. In the /etc/hosts window, turn on line numbers for just this buffer:
      :setlocal number
    7. Navigate to the other window:
      Ctrl-w, l  (or use the left cursor key)
    8. Move the cursor to line 10 and navigate back to the /etc/hosts window:
      Ctrl-w, h
    9. Save a working session for this window and file configuration:
      :mksession ~/.vim/sessions/2upnetwork.session
    10. Exit Vim without saving any changes: :qa!
    11. Now start up Vim with the 2upnetwork session active: vim -S ~/.vim/sessions/2upnetwork.session
    12. Source in the default session: :source ~/.vim/sessions/defaultvim.session
    13. See if the other files are still active:
    14. Exit Vim without saving any changes:
  2. Challenge

    Working with Tabs, and Working with Split Windows

    1. Create four example files to use in windows:

      touch file{1..4}
    2. Open the files in vertically-split windows in Vim:

      vim -O file*
    3. Quit without saving anything:

    4. Open the files in horizontally-split windows in Vim:

      vim -o file*
    5. Quit without saving anything:

    6. Open the four files in simple buffers:

      vim file{1..4}
    7. Confirm that all four files are loaded in buffers:

    8. Display the tab page list:


      Note: There is only one tab page listed, and it shows only one window, which is currently displaying the buffer for file1.

    9. Display file2 in the current window:

      :b file2
    10. Split the window, adding file1 into a vertical split: :vsplit file1

    11. Display the tab page list with: :tabs > Note: There is still only one tab page listed, but now it shows two windows, the focus (denoted by the > character) is currently set on the window displaying the buffer for file1.

    12. Practice moving between the windows using the Ctrl-w and the cursor keys, Ctrl-w and the hjkl direction keys, and finally rotate between the windows using Ctrl-w, w. Then reverse the direction of travel with Ctrl-w, W. > Note: You can also practice changing window sizes using Ctrl-w, +/- for sizing vertically, and Ctrl-w, < > for sizing horizontally, and try using numeric multipliers such as Ctrl-w, 5+.

    13. Now swap the window positions on screen with Ctrl-w, r, and restore them to the original positions.
      > Note: Using Ctrl-w, r is equivalent to navigating to each of the windows and explicitly setting a given buffer to display in that window.

    14. Restore the split windows to display the buffer for file1 on the left and file2 on the right, using Ctrl-w, w and :bn to cycle through the buffers.

    15. Focus on the left window, where the buffer for file1 is displayed, and display the buffers list with: :buffers (or :ls) > Note: The buffer list shows that only file1 and file2 as active, as they are the only ones being displayed in windows.

    16. Display the tab page list: :tabs

    17. Now break off the window displaying the buffer for file1 to its own tab: Ctrl-w, T

    18. Discover all the tab management commands by typing :tab and then tapping the physical TAB key to sequentially display each of the possible tab commands.

    19. For simplicity, move between the two tabs using the gt keys for now.

    20. Use gt to select the first (left-most) tab, or move there: :tabfirst

    21. Split a new vertical window on tab page 1: :vsplit file4 > Note: Notice that the tab title shows two windows exist, and displays the name of the focused window.

    22. Add a new line of text to file4 with: :read !date > Note: Notice that there is now a + displayed on that tab's title, indicating an unsaved buffer on that tab.

    23. Use gt to select, or move to, the second tab: :tablast

    24. Split a new vertical window on tab page 2: :vsplit file4 > Note: Notice that the newly-opened window displaying the buffer for file4 contains exactly the same contents as the window on tab page 1 displaying the buffer for file4.

    25. Save a session that you can reload easily with: :mksession ~/.vim/sessions/2tab4win.session

    26. Close the current windows and tabs without saving any changes with: :qa!

    27. Reload the most recent session with: vim -S ~/.vim/sessions/2tab4win.session

    28. Notice your layout is restored, but the file contents not written are lost.

    29. Quit without saving anything with: :qa!

  3. Challenge

    Comparing Two Files with vimdiff, and Setting Common Options in .vimrc

    1. Create a working file (and open it for editing) with:
        :help windows
        :w ~/vimwindows.txt
        :e ~/vimwindows.txt
    1. Turn on line numbers and navigate through the vimwindows.txt buffer and change the words on the right to the words on the left using cw and Insert mode.
        :set number
        On line 7:  multiple -> several
        On line 13: explained -> detailed
        On line 29: does not -> doesn't
        :set nonumber
    1. Save the changed buffer off to a new file with:
        :saveas ~/vimwindows.chg
    1. Use the :bn command to switch over to the original vimwindows.txt file and notice that no changes were kept, as the changed buffer was saved to the new file. Use :bn to RETURN TO vimwindows.chg!
    2. Do a vertical split and add the original vimwindows.txt buffer as a window on the left with:
        :vsplit vimwindows.txt
    1. Now turn on diff mode for the two files with:
        :windo diffthis
    1. Notice the highlighted differences between the files, the original or master on the left, the proposed or changed on the right.
    2. Ensure you are focused on the left pane, and navigate to the line that contains the first difference with:
    1. You can use /string to speed this up, or use w twice to reach the word.
    2. With the cursor on the red-highlighted word multiple, put the master file's change over to the changed file with:
    1. This puts the change to the right-side file, and unmarks the line and difference.
    2. Use Ctrl-w, l to set the focus to the right pane, the proposed change file, and highlight the word detail and get the change from the master file with:
    1. Highlight the next change, the word doesn't and put that change over to the master file with:
    1. Now drop all changes from both files and quit Vim with:
    1. Load both files in Vim using diff mode with:
        vim -d vimwindows.txt vimwindows.chg
    1. Quit without saving anything with:
    1. Reload the diff session again using vimdiff with:
        vimdiff vimwindows.txt vimwindows.chg
    1. Set a few useful settings in your ~/.vimrc file with:
        vim ~/.vimrc
    1. Make the following additions to the .vimrc file, then save the file:
        syntax on
        set scrolloff=3
        set hlsearch
        highlight Search ctermbg=grey ctermfg=red
    1. After saving the file, reload it into the current session with:
        :source ~/.vimrc
    1. Verify the setting are in place by searching for something and seeing if the grey/red search highlight is present:
    1. Quit Vim with:

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