How do I save changes in Vim?

last updated in Categories , , , ,

vim commandHow do I save changes in Vim?

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 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.

Commands to save changes in vim

The procedure is as follows:

  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

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

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

7 comment

  1. 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
  2. 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?

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

    Still, have a question? Get help on our forum!