How To Save a File in Vi / Vim Text Editor

I am a new Linux or UNIX like operating system user. I just wanted to create a file called demo.txt and save & close it using vi text editor. How do I save a file using vi? How can I save the file in Unix?

Vim is a text editor. It is an open source software for Linux and Unix like systems. This page shows how to save a file in vi or vim.

How to save a file in Vi / Vim editor in Linux

The procedure is as follows:

  1. Open vim in Linux. For example, type: vim test.c
  2. Write code or programe
  3. Save file in vim by pressing ESC and ZZ
  4. This will save and exit in vim or vi text editor running on Unix or Linux

Let us see some more examples.

Save a File in Vim Text Editor

The vi or vim is a text editor who has three modes: command mode, input mode, and ex mode. When starting, vim or vi begins in command mode. One can press Esc key to return to command mode and issue various commands. To create a new file, open a terminal and then type:
$ vi demo.txt
How to Save a File in Vi or Vim Editor in Linux
vi / vim always begins in command mode. You can press [Esc] key anytime to return to command mode. Press i to insert text. To save and exit from vi / vim, press [Esc] key and type ZZ:
To see list of your saved file use the cat command. Run:
$ ls
$ cat demo.txt

To just save file and not to exit to shell prompt, press [Esc] key and type w

vi ex Mode File Saving Commands

To get into the ex mode, press [Esc] key and then : (the colon). For example, to exit from vi saving changes, press [Esc], : (colon) and type wq:

Fig.01: vi / vim write and quit command in action

Fig.01: vi / vim write and quit command in action

You can also quit, discard all changes by pressing [Esc], : (colon) and q!.

Common Vi / Vim File Savings Related Commands (ex mode)

You need to press [Esc] key followed by the colon (:) before typing the following commands:

Command Description
q Quit
q! Quit without saving changes i.e. discard changes and quit file
r fileName Read data from file called fileName
w Save file and continue editing
wq Write and quit (save and exit)
x Same as wq command i.e. write and quit (save and exit)
w fileName Write to file called fileName (save as)
w! fileName Overwrite to file called fileName (save as forcefully)

See Vi editor command keys wiki page for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 21 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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
21 comments… add one
  • paulywill Dec 2, 2012 @ 21:27


    Needed a quick answer! This rocked!!!

  • sudheer Apr 15, 2013 @ 11:36

    thanks for ur info

  • Curtis Jun 7, 2013 @ 17:38

    You really nailed it! Thanks for the Clear instructions.

  • Brandi Jul 9, 2013 @ 17:35

    Really helpful.

  • sivakoti Jan 19, 2014 @ 9:39

    Thanks, clear clarification given

  • y. çıncalık Feb 2, 2014 @ 12:46

    VIM is a very gibberish text editor. You open a file and change many things, then you try to save and exit. But you see all the changes you have made have gone.

  • priyanka Feb 26, 2014 @ 6:48

    thanks for the clear basic understanding.

  • sakthi Apr 25, 2014 @ 15:16

    super,now only i have to learn how to save as new file in vi editor mode.

  • BAVA SHIEK BAREETH Jul 22, 2014 @ 10:53


  • Kunal Aug 26, 2014 @ 6:01

    Liked the answer

  • Dylan Oct 1, 2014 @ 21:00

    I get the following when I do that:
    E492: Not an editor command: zz

    • nathan Nov 4, 2015 @ 16:14

      The Zs are capital, not lowercase. You typed zz, you should have typed ZZ

    • Digvijay Nov 19, 2016 @ 8:06

      use shift+zz

  • Kapil Jan 27, 2015 @ 8:32

    How to just save file without closing it

  • tobs Jan 29, 2015 @ 8:26

    It rocks! Tnx very kindly

  • BOB May 18, 2015 @ 16:27

    lets you write without exiting

  • sandy Jul 22, 2015 @ 18:21

    thanks for suggestion……..!

  • AGANA Aug 16, 2015 @ 12:31

    Thanks… very helpful

  • vinod Dec 4, 2015 @ 2:14

    iam not able to save the file in vi editor after pressing Esc :wq! enter.It i sasking to press enter or continue

  • chinnaduraisd Mar 29, 2016 @ 6:16

    i am studying MCA pg programme, and i would like to thank for your nixcraft website because i really pround it pls send more unix command through my gmail account

  • mazixo Jun 11, 2017 @ 5:36


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum