FactorPad
Build a Better Process

Four Ways to Save Vim Files - Whether exiting or staying (sane)

In Vim saving and quitting is easy when you answer three questions: Do you want to stay in Vim? Has the file been saved before? And did you make changes?
  1. Stay / existing file - To stay in Vim and save changes to an existing file, type: :w. This and other answers assume you used Esc to return from Insert Mode to Normal Mode.
  2. Stay / non-existing file - To stay in Vim and save changes to a non-existing file, type: :w a space and the newfile name.
  3. Leave / existing file - To leave Vim and save changes to an existing file, type: :wq.
  4. Leave without saving - To leave Vim and not save changes, type: :q!.
face pic by Paul Alan Davis, CFA
Updated: February 25, 2021
That's the quick answer. Worry not, with experience your mind will take over and do this automatically. Press on for more Vim tips.

Outline Back Tip Next

/ factorpad.com / tech / vim / reference / save-vim.html


An ad-free and cookie-free website.


Save Vim Files, Existing or Not, With or Without Quitting

A tutorial on several way to save files in Vim.

Video Tutorial

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)

Video Script

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.

Example 1 - Stay in Vim saving an existing file

In our first example, we will assume we opened an existing file using vim filename at the command line.

$ vim filename

The file looks like this.

1 This is a two-line text file used for several illustrations 2 and saved as "filename". ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "filename" 2L, 85C 1,1 All
Note: learn faster by creating a file like we did here.

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:

"filename" 2L, 85C written

To interpret, "filename" was written with 2 lines, 85 characters. Success!

Example 2 - Stay in Vim saving changes to an unnamed and unsaved file

In the second example, we will open Vim without an already-existing file from the command line.

$ vim

By default when we open Vim with an unnamed and unsaved file it opens in Normal Mode at the default screen.

1 _ ~ ~ ~ ~ VIM - Vi IMproved ~ ~ Version 7.4.576 ~ by Bram Moolenaar et al. ~ Modified by pkg-vim-maintainers@lists.alioth.debian.org ~ Vim is open source and freely distributable ~ ~ Help poor children in Uganda! ~ type :help iccf<Enter> for information ~ ~ type :q<Enter> to exit ~ type :help<Enter> or <F1> for on-line help ~ type :help version7<Enter> for version info ~ ~ 0,0-1 All

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.

1 # saved as newfile ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -- INSERT -- 1,18 All

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.

Example 3 - Quit Vim and Save Vim Changes All at One Time

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.

1 # saved as newfile. ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ --- INSERT --- 1,1 All

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.

$ vim newfile $ _

Example 4 - Edit a file and quit Vim without saving

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.

1 _ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ --No lines in buffer-- 1,0-1 All

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.

$ vim newfile $ vim newfile $ _

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]


Vim Questions and Answers

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!


Related Vim Commands and Procedures


What's Next?

Our YouTube Channel has a growing community of open-source advocates like you. We would love if you joined us. Follow and subscribe now.

Outline Back Tip Next

/ factorpad.com / tech / vim / reference / save-vim.html


save vim
how to save vim
vim examples
how to save and exit vim
Vim quit without saving
vim exit without saving
save vim file
vim wq
vim exit and save
vim save and quit
saving files in vim

A newly-updated free resource. Connect and refer a friend today.