- Lab
- A Cloud Guru
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.
Path 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/hosts
window, 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/hosts
window: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:
:tabs
Note: There is only one tab page listed, and it shows only one window, which is currently displaying the buffer for
file1
. -
Display
file2
in the current window::b file2
-
Split the window, adding
file1
into 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-w
and the cursor keys,Ctrl-w
and thehjkl
direction 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, r
is 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
file1
on the left andfile2
on the right, usingCtrl-w, w
and:bn
to cycle through the buffers. -
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 onlyfile1
andfile2
as 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
file1
to its own tab:Ctrl-w, T
-
Discover all the tab management commands by typing
:tab
and then tapping the physicalTAB
key to sequentially display each of the possibletab
commands. -
For simplicity, move between the two tabs using the
gt
keys for now. -
Use
gt
to 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
file4
with::read !date
> Note: Notice that there is now a+
displayed on that tab's title, indicating an unsaved buffer on that tab. -
Use
gt
to 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 forfile4
contains 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.txt
buffer and change the words on the right to the words on the left usingcw
and 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
:bn
command to switch over to the originalvimwindows.txt
file and notice that no changes were kept, as the changed buffer was saved to the new file. Use:bn
to RETURN TOvimwindows.chg
! - Do a vertical split and add the original
vimwindows.txt
buffer 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
/string
to speed this up, or usew
twice 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, l
to set the focus to the right pane, the proposed change file, and highlight the worddetail
and get the change from the master file with:
:diffget
- Highlight the next change, the word
doesn't
and 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
vimdiff
with:
vimdiff vimwindows.txt vimwindows.chg
- Set a few useful settings in your
~/.vimrc
file with:
vim ~/.vimrc
- 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
- 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!
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.