:q
. Hit
Esc
first if you don't know your mode.
:q!
from
Normal Mode, or
Esc :q!
from any other mode.
Esc
then
:wq
.
/ factorpad.com / tech / vim / reference / exit-vim.html
An ad-free and cookie-free website.
A tutorial on clearing up one of Vim's most confusing challengs for beginners.
If you prefer, this topic is also covered in video.
Videos can be accessed from our Vim Reference Playlist on YouTube.
Exit Vim - Three ways to quit vim including with and without saving (6:18)
Beginner
What makes the Vi and Vim editors unique is the concept of modes which makes editing fast once you gain a comfort level. If you are familiar with other text editors or word processors you will also notice that Vim lacks menus. So yes, it is very different.
In our first example, we'll open an existing text file using
vim filename
at the command line.
By default, Vim opens files in Normal Mode. The Vim and Vi editors have several modes, but for now just remember this mode plus two others: Insert Mode (for editing) and Command Mode (to tell Vim what to do, like quit).
We can see information in the footer like
"filename"
for the name of the file,
2L 85C
for the
number of lines and characters. By default Vim opens the file in
position row 1, column 1, so 1,1
, and
All
is what portion of the file is
showing.
Let's close this file using instructions from above. Since we are
in Normal Mode we can simply type
:q
followed by the
Enter
key and you will have quit Vim
and will return to the command prompt. Here, in Linux, it shows your
last command and the cursor indicating that the system is patiently
waiting for your next request, like any well-behaved computer.
In our second example, we'll open that same text file again using the
vim filename
convention.
Now imagine you made a typo right away and inadvertenly pushed the
s
key. This erased the first
letter of the file where the cursor was and switched you from
Normal Mode to Insert Mode as shown
below.
Afraid that we may have done something permanently wrong (we haven't),
let's close this file and quit without saving. Hit
Esc
, to go from
Insert Mode to Normal Mode, then
:q!
going into
Command Mode and telling Vim to quit without saving.
You will return to the command prompt and it will likely look like
this. Everything is okay, no damage done.
In our third example, we will open that same text file again by typing
vim filename
. This time we will edit
the file intentionally. Below is how it looks unchanged.
Your cursor will be in row 1, column 1. Now to move down one row, or
line, type j
, and then
capital A
which does two things.
First, it takes us to the end of the second line, and second it
switches us from Normal Mode to
Insert Mode as indicated by the
-- INSERT --
in the bottom left
section (see below). Now, we are finally ready to edit the file.
Now type (Wow, that's original!)
as
indicated below.
Next, let's write and save these changes as instructed in step 3 above.
Hit Esc
to go from
Insert Mode to Normal Mode. Notice
how -- INSERT --
in the bottom left
went away (assuming you have standard settings and are following along
at home).
Now we can save, or write and quit with
:wq
. And because here you are writing
before quitting you won't get an error message, and will return to
the command line.
Q: What does the error message
No write since last change (Add ! to
override)
mean?
A: This is Vim's way of reminding you that you
didn't save changes to an edited file. In this case
decide if you want to keep the changes
(use :wq
) or not
(use :q!
).
Q: Why are there three
$ vim filename
lines at the end of
Example 3?
A: Because we opened the same file three
times. In Linux, if you'd like to clear the screen use the
clear
command.
We have hundreds of videos on YouTube. Get updates by subscribing, and follow @factorpad on Twitter.
/ factorpad.com / tech / vim / reference / exit-vim.html
A newly-updated free resource. Connect and refer a friend today.