Vim is a command line file editor for Linux systems. In this article, we’ll show you one of the most basic functions you’ll need to know for vi and vim, which is how to quit a file with or without saving changes to it.
In this tutorial you will learn:
- How to save a file in vim
- How to save and exit a file in vim
- How to exit a file without saving in vim
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux disto |
Software | vim |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Saving a file
Saving a file in vim is known as “writing changes,” so the command to save a file is w
. To quickly save changes to an open file, press Esc
then type :w
and press Enter
.
You may not always need to press Esc
first, but it doesn’t hurt, and it will ensure that you are not still in insert mode or in the middle of typing some other command.
Save and exit a file
Saving changes involves the w
(write) command, and exiting a file uses the q
(quit) command. Simply combine both of these commands to exit and save changes at the same time. Press Esc
then type :wq
and press Enter
.
An alternative method is to use the Shift
+ zz
keyboard shortcut. This can be a little faster and easier, but it really just boils down to personal preference. Press Esc
, then hold down Shift
while pressing zz
.
Exit a file without saving changes
If you’ve made some undesirable changes to a file and don’t want to write the changes, you’ll need to use the q
command along with a !
exclamation mark. Press Esc
, type :q!
, and press Enter
.
If you’ve opened a file in vim and haven’t made any changes at all, it can be exited with simply :q
, and the exclamation mark won’t be necessary. However, it also doesn’t hurt to include it.
Conclusion
Vim is a very powerful text editor, but it takes some getting used to. Once you have the essentials down, like how to save changes to files, you’ll find it much more convenient than other default editors. In this guide, we learned how to exit a file in vim with and without saving changes to the file. To learn more about vim, check out our full vim tutorial and customizing vim for development guide.