How do I save changes in Vim?

vim commandHow do I save changes in Vim? How can we save a file in Vim and quit the text editor from the CLI?

Vim is a text editor. Very popular among the programmers, sysadmins, and free operating system users such as Linux/FreeBSD and other Unix-like systems such as macOS. One can edit configuration files and programs written in C/C++, Python, Perl, PHP/Ruby with vim. But, how do you save changes in vim? Let us see how to save a file in a vim text editor in Linux or Unix-like systems.

ADVERTISEMENTS

Commands to save changes in vim

The procedure is as follows to save a file in Vim and quit the editor:

  1. Start vim by typing vim filename
  2. To insert text press i
  3. Now start editing text. Add new text or delete unwanted text.
  4. Press Esc key and type :w to save a file in vim.
  5. One can press Esc and type :wq to save changes to a file and exit from vim
  6. Another option is to press :x

Let us see all commands and examples in detail for a vim text editor.

How to save a file in vim

By default, vim is in command mode. In command mode, we can use vim commands and navigate/scroll around the file. Let us start vim text editor:
vim filename
vim is_mounted

How to Save a File in Vim under Linux or Unix
Say, you need type text or add additional program. So yo need to enter the insert mode by pressing the i key:
Vim enter the insert mode by pressing the i key

Saving a file in vim

Now we edited or added some text/code. Hence, it is the time to save changes in Vim by pressing Esc and typing :w followed by the Enter key.
vim save a file using :w command
One can save the file under a different name:

  • Press ESC key.
  • Type :w My_fileName_here
  • Hit Enter key.

In another words, it works as “Save as” feature. We can verify that the file has been saved using the cat command:
cat is_mouted
Sample outputs:

#!/bin/bash
# Purpose: Mount glusterfs at boot time
# Author: Vivek Gite
# --------------------------------------
p='gfs01:/gvol01'
 
mount | grep -wq "^${p}"
 
if [ $? -ne 0 ]
then
	/bin/mount -t glusterfs $p /sharedwww/
fi

Can I save a file and quit in Vim?

Yes, of course. The command to save a file in Vim and quit the editor is :x. All you have to do is:

  • Press ESC key.
  • Type :x
  • Hit Enter key.

How do I save changes in Vim
We can also press ESC key and then hold down the Shift and press zz (press z twice).

Quit Vim without saving the file

WARNING! This exits the vim editor, DISCARDING any changes you have made.

Finally, we can exit or quit the vim text editor without saving the changes to our file. All you have to do is:

  • Press Esc
  • Type :q!
  • Hit Enter key
Exits the vim editor, DISCARDING any changes you have made

Conclusion

You learned how to save changes in Vim using various commands. For more information see vim home page here.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
7 comments… add one
  • Sai Kumar Feb 13, 2005 @ 5:48

    Vim is a very powerful editor that has many commands. I love vim.

  • Tristan Mar 2, 2006 @ 12:42

    Save the file with changes and exit Vim with:

    :wq <ENTER>
    
  • r00t Jul 4, 2009 @ 3:01

    i, use :wq to save a file and exit instead of :x. i learn something new today.

    • vim new user Nov 12, 2013 @ 19:41

      yes, to exit without saving changes made any changes you made
      :q! <ENTER>

  • Todd Mathis Dec 21, 2010 @ 4:03

    Save the changes made to the C program, type :w updated.c. May be you can add some good info like:

    1. Start vim my-code.c
    2. Type :!ls to get a listing of your directory
    3. Look a filename that does not exist yet, such as test.c/data.c
    4. Now type: :w data.c
    5. Where data.c is the filename I chose
    6. This saves the whole file under the new name data.c
    7. We can verify this, type :!ls again to see directory listing
  • Tom Jan 3, 2016 @ 23:19

    Exit vim and trash all changes. type:

    <ESC>   :q!   <ENTER>

    To save the changes, type:

    <ESC>   :wq   <ENTER>

    How hard is that to remember? do we need a blog post for it?

  • Chung-Hee (??) Aug 15, 2017 @ 15:42

    No mention of Vimtutor? The vimtutor command starts the Vim tutor. Go for it. Save yourself time and searching stuff online:
    vimtutor

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.