:w
. This and other answers
assume you used Esc
to return
from Insert Mode to Normal Mode.
:w
a space and the
newfile
name.
:wq
.
:q!
.
/ factorpad.com / tech / vim / reference / save-vim.html
An ad-free and cookie-free website.
A tutorial on several way to save files in Vim.
If you prefer, this topic is also covered in video.
Videos can also be accessed from our Vim Reference Playlist on YouTube.
Save Vim - Learn vim save and quit with four vim commands (8:23)
Beginner
Sadly, the first opportunity to save for many Vim beginners is their last. The Vim and Vi editors are modal editors, requiring users to perform actions in one of several modes. The first three we learn are:
It is this concept of modes that makes editing fast once you memorize the keystrokes. On top of that, the lack of a menu system and a tricky Vim help system, steepens the learning curve. For those who stick with it, the benefits outweigh the costs.
A reason web development professionals are willing to invest time in Vim in the first place is because it sits on almost every Unix and Linux distribution. Since Linux cloud servers host most of web traffic today, Vim is pervasive.
Also, web developers who push html documents out into the digital ocean can get by with learning one text editor very well. Since time is precious to web engineers and programmers often that choice is the editor Vim because it sits on nearly every cloud hosting provider, also known as a cloud vps hosting service.
In our first example, we will assume we opened an existing file using
vim filename
at the command line.
The file looks like this.
For now, we will assume the file existed and we modified it. To save
the file and stay in Vim, first we have to make sure we leave
Insert Mode by hitting the
Esc
, returning to
Normal Mode.
Next, type :w
(which appears in the
bottom left) and after you hit Enter you will see a message like this:
To interpret, "filename" was written with 2 lines, 85 characters. Success!
In the second example, we will open Vim without an already-existing file from the command line.
By default when we open Vim with an unnamed and unsaved file it opens in Normal Mode at the default screen.
The cursor is placed at the top left position and if you start typing here you may get unexpected outcomes because we are in Normal Mode which is for navigation.
There is no sense in saving a file without making changes, so we will
first edit the file by entering Insert Mode. We can
accomplish this one of several ways and an easy way to remember is
with the letter i
and follow that with
a comment # saved as newfile
as shown
here.
Then hit the Esc
key, switching back
to Normal Mode. Next, type
:w newfile
followed by Enter. This
writes the file after the :
character
shifted us to Command Mode.
From there you can proceed with other edits to the file. Each time from
here forward you will not need to re-type the file name, so simply
:w
will work, as in example 1.
In our third example, open that new text file using the
vim newfile
at the command line.
Now make one more edit, switching from
Normal Mode to Insert Mode using
capital A
which navigates to the end
of the first line and automatically changes modes. Here let us add a
period .
to the end of the sentence.
Okay, now to complete this example, hit
Esc
, to go from
Insert Mode to Normal Mode, then
:wq
going into
Command Mode and telling Vim to quit with saving.
You can imagine that the document is now in the digital ocean, sitting
on a cloud server somewhere in the world. In the meantime, you will
quietly return to your command prompt.
Of course, at times you do not want to save your changes.
In our fourth example, open that same text file again by typing
vim newfile
. This time we will
intentionally make a mistake, like all Vim beginners do. After it
opens, hit dd
and woops the line is
gone like below.
Okay, take a deep breath, this will not cause the cloud server to go
down. Before you panic, there are a number of
things we can do, but an easy and harmless one is to quit without
saving changes. Everything will be okay. To do this type
:q!
telling Vim to go to
Command Mode, quit and the exclamation tells Vim
you don't want to be reminded that the file was not saved.
Your system will return to the command line.
If you want to confirm for yourself, open the file and verify that nothing was modified.
All is right with the world wide web. The cloud servers are in line and
the digital ocean is calm and peaceful. More importantly, you kept
your job. 8]
Q: Why does it have to be so confusing?
A: Rather than call it confusing, call it new.
You do the same thing in a word processor when you memorize shortcuts.
Plus Vim is much faster than saving with a mouse and menus.
Q: In the video (only) why did you type
$ rm newfile
after the fourth
example in the introduction?
A: Because later in the video where I created the
new file newfile
, I did not want to
explain an error message about a file already existing, so
I deleted it with the Linux rm
command. If you can follow that logic at this beginner stage then you
have a good grasp on opening, saving and quitting Vim. Nice work!
Our YouTube Channel has a growing community of open-source advocates like you. We would love if you joined us. Follow and subscribe now.
/ factorpad.com / tech / vim / reference / save-vim.html
A newly-updated free resource. Connect and refer a friend today.