≡ Menu

HowTo: Save File in Vi / Vim Text Editor

I'm new to Linux or UNIX like operating system. 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?

The vi or vim is a text editor which has three modes: command mode, input mode, and ex mode. To create a new file open a terminal and then type:
$ vi demo.txt
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, enter:
$ ls
$ cat demo.txt

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:

q Quit
q! Quit without saving changes i.e. discard changes
r fileName Read data from file called fileName
wq 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.

{ 17 comments… add one }

  • paulywill December 2, 2012, 9:27 pm


    Needed a quick answer! This rocked!!!

  • sudheer April 15, 2013, 11:36 am

    thanks for ur info

  • Curtis June 7, 2013, 5:38 pm

    You really nailed it! Thanks for the Clear instructions.

  • Brandi July 9, 2013, 5:35 pm

    Really helpful.

  • sivakoti January 19, 2014, 9:39 am

    Thanks, clear clarification given

  • y. çıncalık February 2, 2014, 12:46 pm

    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 February 26, 2014, 6:48 am

    thanks for the clear basic understanding.

  • sakthi April 25, 2014, 3:16 pm

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

  • BAVA SHIEK BAREETH July 22, 2014, 10:53 am


  • Kunal August 26, 2014, 6:01 am

    Liked the answer

  • Dylan October 1, 2014, 9:00 pm

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

    • nathan November 4, 2015, 4:14 pm

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

  • Kapil January 27, 2015, 8:32 am

    How to just save file without closing it

  • tobs January 29, 2015, 8:26 am

    It rocks! Tnx very kindly

  • BOB May 18, 2015, 4:27 pm

    lets you write without exiting

  • sandy July 22, 2015, 6:21 pm

    thanks for suggestion……..!

  • AGANA August 16, 2015, 12:31 pm

    Thanks… very helpful

Leave a Comment

   Tagged with: , , , , , , , , , , , , , , ,