≡ 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:
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:
: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!.
: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:

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

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 14 comments… add one }

  • paulywill December 2, 2012, 9:27 pm

    Thanks!!!

    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

    WOOOOOOOOOOOOW IT”S USE FULL VERY MUTCH

  • 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

  • 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

    :w
    lets you write without exiting

Leave a Comment