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
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:
| Command | Description |
| 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.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 2 comments… read them below or add one }
Thanks!!!
Needed a quick answer! This rocked!!!
thanks for ur info