HowTo: Save File in Vi / Vim Text Editor

by on April 24, 2010 · 11 comments· LAST UPDATED April 24, 2011


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.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 paulywill December 2, 2012 at 9:27 pm


Needed a quick answer! This rocked!!!


2 sudheer April 15, 2013 at 11:36 am

thanks for ur info


3 Curtis June 7, 2013 at 5:38 pm

You really nailed it! Thanks for the Clear instructions.


4 Brandi July 9, 2013 at 5:35 pm

Really helpful.


5 sivakoti January 19, 2014 at 9:39 am

Thanks, clear clarification given


6 y. çıncalık February 2, 2014 at 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.


7 priyanka February 26, 2014 at 6:48 am

thanks for the clear basic understanding.


8 sakthi April 25, 2014 at 3:16 pm

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


9 BAVA SHIEK BAREETH July 22, 2014 at 10:53 am



10 Kunal August 26, 2014 at 6:01 am

Liked the answer


11 Dylan October 1, 2014 at 9:00 pm

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


Leave a Comment

Tagged as: , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: