HowTo: Save File in Vi / Vim Text Editor

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

in

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 itG+ itDownload PDF versionFound an error/typo on this page?

{ 13 comments… read them below or add one }

1 paulywill December 2, 2012 at 9:27 pm

Thanks!!!

Needed a quick answer! This rocked!!!

Reply

2 sudheer April 15, 2013 at 11:36 am

thanks for ur info

Reply

3 Curtis June 7, 2013 at 5:38 pm

You really nailed it! Thanks for the Clear instructions.

Reply

4 Brandi July 9, 2013 at 5:35 pm

Really helpful.

Reply

5 sivakoti January 19, 2014 at 9:39 am

Thanks, clear clarification given

Reply

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.

Reply

7 priyanka February 26, 2014 at 6:48 am

thanks for the clear basic understanding.

Reply

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.

Reply

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

WOOOOOOOOOOOOW IT”S USE FULL VERY MUTCH

Reply

10 Kunal August 26, 2014 at 6:01 am

Liked the answer

Reply

11 Dylan October 1, 2014 at 9:00 pm

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

Reply

12 Kapil January 27, 2015 at 8:32 am

How to just save file without closing it

Reply

13 tobs January 29, 2015 at 8:26 am

It rocks! Tnx very kindly

Reply

Leave a Comment

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

Previous Faq:

Next Faq: