Advanced Operations in Vim

45 minutes
  • 3 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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


    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:

  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:

  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:

  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:

  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:

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
  2. 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
  3. Save the changed buffer off to a new file with:
    :saveas ~/vimwindows.chg
  4. 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!
  5. Do a vertical split and add the original vimwindows.txt buffer as a window on the left with:
    :vsplit vimwindows.txt
  6. Now turn on diff mode for the two files with:
    :windo diffthis
  7. Notice the highlighted differences between the files, the original or master on the left, the proposed or changed on the right.
  8. Ensure you are focused on the left pane, and navigate to the line that contains the first difference with:
  9. You can use /string to speed this up, or use w twice to reach the word.
  10. With the cursor on the red-highlighted word multiple, put the master file’s change over to the changed file with:
  11. This puts the change to the right-side file, and unmarks the line and difference.
  12. 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:
  13. Highlight the next change, the word doesn't and put that change over to the master file with:
  14. Now drop all changes from both files and quit Vim with:
  15. Load both files in Vim using diff mode with:
    vim -d vimwindows.txt vimwindows.chg
  16. Quit without saving anything with:
  17. Reload the diff session again using vimdiff with:
    vimdiff vimwindows.txt vimwindows.chg
  18. Set a few useful settings in your ~/.vimrc file with:
    vim ~/.vimrc
  19. 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
  20. After saving the file, reload it into the current session with:
    :source ~/.vimrc
  21. Verify the setting are in place by searching for something and seeing if the grey/red search highlight is present:
  22. Quit Vim with:

Additional Resources

You've come a long way since first encountering Vim, and have learned the basics. Now it's time to jump headfirst into how to use Sessions, tabs, split windows, and Vim's diff option to take your productivity to the next level.

With this knowledge, you'll now be that lightning-fast user of Vim who uses its features to advantage, who makes it all look so easy, and who gets a lot done in a very short time!

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!