- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
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.
Lab Info
Table of Contents
-
Challenge
Work with Saving and Loading Sessions
- Create a directory to store your sessions in:
mkdir -p ~/.vim/sessions - Open up Vim:
vim - Save a default session for troubleshooting purposes:
:mksession ~/.vim/sessions/defaultvim.session - Quit Vim:
:qa! - Open up two files in split windows:
vim -O /etc/hosts /etc/resolv.conf - In the
/etc/hostswindow, turn on line numbers for just this buffer::setlocal number - Navigate to the other window:
Ctrl-w, l (or use the left cursor key) - Move the cursor to line 10 and navigate back to the
/etc/hostswindow:Ctrl-w, h - Save a working session for this window and file configuration:
:mksession ~/.vim/sessions/2upnetwork.session - Exit Vim without saving any changes:
:qa! - Now start up Vim with the 2upnetwork session active:
vim -S ~/.vim/sessions/2upnetwork.session - Source in the default session:
:source ~/.vim/sessions/defaultvim.session - See if the other files are still active:
:ls - Exit Vim without saving any changes:
:qa!
- Create a directory to store your sessions in:
-
Challenge
Working with Tabs, and Working with Split Windows
-
Create four example files to use in windows:
touch file{1..4} -
Open the files in vertically-split windows in Vim:
vim -O file* -
Quit without saving anything:
:qa! -
Open the files in horizontally-split windows in Vim:
vim -o file* -
Quit without saving anything:
:qa! -
Open the four files in simple buffers:
vim file{1..4} -
Confirm that all four files are loaded in buffers:
:ls -
Display the tab page list:
:tabsNote: There is only one tab page listed, and it shows only one window, which is currently displaying the buffer for
file1. -
Display
file2in the current window::b file2 -
Split the window, adding
file1into a vertical split::vsplit file1 -
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 forfile1. -
Practice moving between the windows using the
Ctrl-wand the cursor keys,Ctrl-wand thehjkldirection keys, and finally rotate between the windows usingCtrl-w, w. Then reverse the direction of travel withCtrl-w, W. > Note: You can also practice changing window sizes usingCtrl-w, +/-for sizing vertically, andCtrl-w, < >for sizing horizontally, and try using numeric multipliers such asCtrl-w, 5+. -
Now swap the window positions on screen with
Ctrl-w, r, and restore them to the original positions.
> Note: UsingCtrl-w, ris equivalent to navigating to each of the windows and explicitly setting a given buffer to display in that window. -
Restore the split windows to display the buffer for
file1on the left andfile2on the right, usingCtrl-w, wand:bnto cycle through the buffers. -
Focus on the left window, where the buffer for
file1is displayed, and display the buffers list with::buffers (or :ls)> Note: The buffer list shows that onlyfile1andfile2as active, as they are the only ones being displayed in windows. -
Display the tab page list:
:tabs -
Now break off the window displaying the buffer for
file1to its own tab:Ctrl-w, T -
Discover all the tab management commands by typing
:taband then tapping the physicalTABkey to sequentially display each of the possibletabcommands. -
For simplicity, move between the two tabs using the
gtkeys for now. -
Use
gtto select the first (left-most) tab, or move there::tabfirst -
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. -
Add a new line of text to
file4with::read !date> Note: Notice that there is now a+displayed on that tab's title, indicating an unsaved buffer on that tab. -
Use
gtto select, or move to, the second tab::tablast -
Split a new vertical window on tab page 2:
:vsplit file4> Note: Notice that the newly-opened window displaying the buffer forfile4contains exactly the same contents as the window on tab page 1 displaying the buffer forfile4. -
Save a session that you can reload easily with:
:mksession ~/.vim/sessions/2tab4win.session -
Close the current windows and tabs without saving any changes with:
:qa! -
Reload the most recent session with:
vim -S ~/.vim/sessions/2tab4win.session -
Notice your layout is restored, but the file contents not written are lost.
-
Quit without saving anything with:
:qa!
-
-
Challenge
Comparing Two Files with vimdiff, and Setting Common Options in .vimrc
- Create a working file (and open it for editing) with:
vim :help windows :w ~/vimwindows.txt :q :e ~/vimwindows.txt- Turn on line numbers and navigate through the
vimwindows.txtbuffer and change the words on the right to the words on the left usingcwand Insert mode.
:set number On line 7: multiple -> several On line 13: explained -> detailed On line 29: does not -> doesn't :set nonumber- Save the changed buffer off to a new file with:
:saveas ~/vimwindows.chg- Use the
:bncommand to switch over to the originalvimwindows.txtfile and notice that no changes were kept, as the changed buffer was saved to the new file. Use:bnto RETURN TOvimwindows.chg! - Do a vertical split and add the original
vimwindows.txtbuffer as a window on the left with:
:vsplit vimwindows.txt- Now turn on diff mode for the two files with:
:windo diffthis- Notice the highlighted differences between the files, the original or master on the left, the proposed or changed on the right.
- Ensure you are focused on the left pane, and navigate to the line that contains the first difference with:
gg ]c- You can use
/stringto speed this up, or usewtwice to reach the word. - With the cursor on the red-highlighted word
multiple, put the master file's change over to the changed file with:
:diffput- This puts the change to the right-side file, and unmarks the line and difference.
- Use
Ctrl-w, lto set the focus to the right pane, the proposed change file, and highlight the worddetailand get the change from the master file with:
:diffget- Highlight the next change, the word
doesn'tand put that change over to the master file with:
:diffput- Now drop all changes from both files and quit Vim with:
:qa!- Load both files in Vim using diff mode with:
vim -d vimwindows.txt vimwindows.chg- Quit without saving anything with:
:qa!- Reload the diff session again using
vimdiffwith:
vimdiff vimwindows.txt vimwindows.chg- Set a few useful settings in your
~/.vimrcfile with:
vim ~/.vimrc- Make the following additions to the
.vimrcfile, then save the file:
syntax on set scrolloff=3 set hlsearch highlight Search ctermbg=grey ctermfg=red- After saving the file, reload it into the current session with:
:source ~/.vimrc- Verify the setting are in place by searching for something and seeing if the grey/red search highlight is present:
/set ENTER- Quit Vim with:
:qa!
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.