- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
Vim Survival Kit
In this short lab, we'll walk through the foundational tasks for new Vim users. In this lab, we'll cover file creation, the editing of system files, Vim navigation, and helpful resources. By the end of this lab, you'll gain essential skills needed to edit files within Linux systems.
Lab Info
Table of Contents
-
Challenge
Creating, Opening, and Exiting a File
Sub-Task 1a - Create a New File, Save and Exit
- Open Vim:
vim - Press the
ikey and note that 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. - When done entering the text, use
ESCto return to Command Mode. - Write the new file to disk with:
:w myfirstnovel.txt - Then exit Vim with:
:qNote: You can specify a filename at the outset, such as in Step 1. Then saving and exiting can skip Step 4, and in Step 5 the command would then be:
:wq
Sub-Task 1b - Open an existing (read-only) file, exit w/o saving changes
- Edit the
/etc/hostsfile with:vim /etc/hosts - Press the
ikey to go to Insert Mode, which will cause aW10: Warning: Changing a readonly fileerror message. - Use
ESCto return to Command Mode anduto ensure that no changes have been made, indicated by a message at the status line ofAlready at oldest change. - Exit the file with:
:q!
- Open Vim:
-
Challenge
Making a Simple Change to a File
Note: The
vim80portion of the command line may bevim81, or evenvim82. Use theTABkey to find what is there.- Copy a Vim documentation file for our use:
cp /usr/share/vim/vim80/doc/help.txt ~/vimhelp.txt - Edit the file with:
vim ~/vimhelp.txt - Navigate to the text VIM - main help file.
- Change the word VIM to Vim by putting the cursor on the I and pressing
cwto change the rest of the word to im. - Then hit
ESCto exit Insert Mode. - Navigate to the m in main and use the
~character to change it to M. - Then do the same for the h in help and the f in file.
- Press
ESCto ensure you are back in Command Mode. - Save and exit with:
:wq
- Copy a Vim documentation file for our use:
-
Challenge
Changing a System File
- Edit the
/etc/hostsfile with:sudo -i vim /etc/hosts - Supply the
cloud_user's password (or the required user's). - Using the cursor keys (if necessary), go to the line that reads:
127.0.0.1 localhost - Press
Awhich enters Insert Mode, and moves you to the end of the line. - Now add a space and
snowblowerto the end of the line so that it reads:127.0.0.1 localhost snowblower - Save and exit the file by first pressing
ESCand thenZZ. - Verify the change was written with:
grep snowblower /etc/hosts - Confirm that the name is resolvable with:
ping -c 4 snowblower - The
pingshould work properly, if not, check the line to ensure there is a space betweenlocalhostandsnowblower.
- Edit the
-
Challenge
Simple Navigation
Note: You must have created the
vimhelp.txtin Task 2 for this to work.- Edit the
~/vimhelp.txtfile:vim ~/vimhelp.txt - Go to the top of the file with
gg. - Go to the bottom of the file with
G. - Go to line 25 with
25G. - Go to 50% of the way through the file with
50%. - Go to the top with
gg. - Press
w5 times to move forward 5, thenggto return to the top of the file. - Press
25wto move forward 25 words, then5bto move backwards 5 words. - Use
h,j,k,lto move around, then use the cursor keys. - Press
ggto go to the top. - Use
/Vim,ENTERto find the first instance ofVim. - Use
nto go to the next and subsequent instances. - Use
Nto go back to previous instances. - Go to the bottom of the file using
G. - Use
?helpto search backwards. - Exit without saving using
q!.
- Edit the
-
Challenge
Inserting, Copying, and Deleting Text
Note: You must have created the
vimhelp.txtin Task 2 for this to work.- Edit the
~/vimhelp.txtfile:vim ~/vimhelp.txt - Go to the top of the file with
ggand then navigate to the line that starts withGet out of Vim. - Press
oto add a line under the current line. - On that line, add the following text, taking care to line up the
:with the other instances of:on other lines:ATTENTION: Do NOT Reboot to Exit Vim! - When added, hit the
ESCkey to return to Command Mode, and use^to go back to the front of the wordATTENTION. Then press0(zero) to go to the very beginning of the line. - Next, place the cursor on the empty line between your current line and the one that reads
Jump to a subject, and delete the empty line by pressingdd. - Now copy the
ATTENTIONline by moving to it and pressingyy. - Go to line that starts with
Jump Backand usepto paste the copied line below it. - Then place your cursor on the blank line in between the current line and the one that begins with
Get specific helpand press theyykeys to copy the blank line. - Next create four more blank lines by using
4p. - Now highlight or select all of the blank lines, except the last one, before
Get specific helpby putting your cursor on the top blank line, pressingVand moving your cursor to the next-to-last blank line (selecting four lines). Delete those lines by pressingdonce. - While on the remaining blank line below the copied line (the one beginning with
ATTENTION:), pressito enter Insert Mode and use the spacebar to insert spaces until the cursor is under theDinDo NOT. Add the textYou'll Regret it!, and pressESCto return to Command Mode. - Move the cursor to the line above the current one, which puts your cursor in between the words
toandExiton that line, and pressJto join the line below to the end of the current line.
- Edit the
-
Challenge
Undoing and Redoing
Note: You must have done the steps in Task 5 for this to work.
- Now undo your changes one at a time by pressing
u. You'll tire quickly of this, so hold down theukey until all changes to the file are reversed. - When you have reached the last undo possible, you will see the status line reflect with the text
Already at oldest change. - Restore all changes to the file by pressing
Ctrl-rrepeatedly, noting what each "change" is made up of, and also what the status line says about how many changes were made. - When all changes have been redone, you'll see
Already at newest change.
Note: For the next task to work properly, ensure that you
redoall the changes. - Now undo your changes one at a time by pressing
-
Challenge
Saving and/or Exiting
Note: You must have done the steps in Task 6 for this to work.
- Save your changed buffer to a new file by typing:
:w ~/changedhelp.txt - Look to see if you are in that buffer, or in the old buffer by pressing:
Ctrl-g - Confirm that there is only a single buffer, and that it's the
vimhelp.txtbuffer, including changes, with::ls - Exit the changed buffer without saving any changes with:
:q!
- Save your changed buffer to a new file by typing:
-
Challenge
Resources for Getting Help
- Run
vimwith no file argument:vim - In Command Mode, use
:helpto get into Help Mode. - Exit Help Mode with
:q, just to show Vim you can! - Invoke help for how to move around with
:h motion. - Peruse the text, then press the
Ctrl-fkey combo to move a screen forward, then useCtrl-bto move back a screen. - While in the
motionhelp page, bring up the help forCtrl-fwith:h Ctrl-fand read about that. - Look up the help for how to load Vim with just its defaults, so when something goes wrong you can troubleshoot it with
:h --clean. - When done, quit out of Help Mode with
:qand completely out of Vim with by pressingESCto ensure you're in Command Mode, then:q!to completely exit. - Navigate to the Vim help documentation directory with:
cd /usr/share/vim/vim80/doc # Remember, it may be vim81! - Use the following command to find what files in the directory have the "motion" keyword in them:
grep -wn motion *.txt - Note the results. The file
visual.txtshould be the last one listed. - Edit the
visual.txtfile and go straight to line 295 (or the line number indicated) where motion is located with:vim +295 visual.txt - Note that you are placed on the beginning of line 295.
- Exit the
visual.txtfile with::q! - Rerun the command to edit
visual.txtwith a search for motion instead:vim +/motion visual.txt
Note: You may have a message about it being read-only, but hit
ENTERto continue.- When the file is loaded, turn search highlighting on with
:set hlsearchand you'll see that motion is highlighted. You can ignore it if there are other highlighted words than motion, those are not actual highlights, just color codes. - Exit Vim with
:q!and rerun the previousgrepcommand. But add| lessto the end of it so that the output will go into thelesspager, and you can scroll through and see what other places your search term is found:grep -wn motion *.txt | less - Repeat this any time you need to find a term in the Vim help files. It's a marvelous source of good information for the curious Vim-ster!
- Run
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.