The ability to work with files and buffers in Vim is just as important as the actual editing in Vim you’ll do so often in your career. Most people can manage to edit a file or a buffer, but having the skills to adroitly handle the sometimes many buffers that will be in use is valuable, to say the least.
In this hands-on lab you’ll gain some practice opening new and existing files, open multiple files and navigate among the buffers handily.
You’ll also be working with saving and exiting, exiting without saving changes, then causing a crash to experience recovering from one. _Knowing how to do this sets you apart from most casual Vim users!_
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Open New and Existing Files, Open Multiple Files at Once, Then Navigate Amongst Open Buffers (File Contents)
Open Vim:
vim
Press the i key and note 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 the Esc key to return to Command Mode
Write the new file to disk with:
:w myfirstnovel.txt
Then exit Vim with:
:q
Next open a read-only (root-only) file with:
vim /etc/hosts
Now "accidentally" change the file by pressing the i key to go to Insert Mode, this will cause an alarming error message to appear:
W10: Warning: Changing a readonly file
Use Esc to return to Command Mode and u to ensure that no changes have been made, indicated by a message at the status line of
Already at oldest change
.And just in case, exit the file with:
:q!
Now open three new named buffers with:
vim file1 file2 file3
Vim will load and you can verify you are in the blank named buffer
file1
.List out the buffers using both of the commands:
:buffers :ls
Rotate through the buffers until you reach
file1
again with::bn
Add a few words to the
file1
buffer with:i A few words of text
Then press Esc
Attempt to switch to the last buffer with:
:blast
You will see an error message:
E37: No write since last change (add ! to override)
Hide the changed buffer and move to the last open buffer with:
:hide blast
List out the open buffers again with:
:ls
- Importing External Command Output, Using External Commands on Buffers, Writing Buffer Contents to External Commands
Note: You must have done Task 1 before continuing with Task 2.
- Switch to the
file2
buffer with::b file2
- Add the same text as you did in
file1
and hit Esc. - Attempt to go to the
file3
buffer, and when you get the error message use::hide b file3
- Add the same text to the
file3
buffer and hit Esc. - Confirm the changed, but not saved, status of all three buffers by inspecting the output of the
:ls
command. - Write the contents of the
file3
buffer to disk with::w
- Confirm the status of
file3
with the:ls
command, there should be no+
in the 9th column. - Now save
file3
as a new file with::saveas file4
- Inspect the
:ls
output to see what happened. - Switch to the
file1
buffer with::b 1
- Write the
file1
buffer contents tofile5
on disk with::w file5
- Inspect the
:ls
output to see what happened. - Run a listing on the directory to see if
file5
exists::!ls -l
Press Enter.
- Verify the status of the
file1
buffer by pressing Ctrl-g and then run:ls
. - Quit all unsaved buffers and exit Vim with:
:qa!
- Switch to the
- Cause and Recover from a Vim Crash
Open Vim with a named buffer using:
vim crashtest.txt
Add some text to the buffer with:
i This is a crash test file
Press Esc
Save the buffer to disk with:
:w
Add another line to the buffer with:
o This is a second line ESC
Select and set all of the file contents to uppercase letters with:
ggVG U
Add a third line to the file with:
:$ read !date
Without saving, kill the Vim session with:
:!pkill -9 vim
Note: This will kill the Vim process and verify this happened by dropping you to the shell and showing the process was
Killed
. There may be a notice that no writes were made since the last change.Verify that Vim is not still active with:
ps aux | grep vim
The output should not list more than the grep for
vim
.Start up Vim again, specifying the previous file:
vim crashtest.txt
You’ll see the dreaded
E325: ATTENTION
error message and have various options at the bottom of the screen.Remembering the contents of the buffer when we killed it, choose the
(R)ecover
option.Read the message, paying close attention to the admonition
You may want to delete the .swp file now
, and press Enter.Save the file to disk and exit with:
:wq
Edit the file again with:
vim crashtest.txt
Upon seeing the now less-dreaded
E325
error, you can now choose to(D)elete
the.swp
file as directed.Alternatively, you can choose to
(Q)uit
and then when out of Vim, verify the existence of, and delete the.swp
file with:ls -l (Verify the file exists) rm -f .crashtest.txt.swp
Now try editing the file again, and you will not see the error message anymore.