Vim is a text editor. It is an open source software for Linux and Unix like systems. This page shows how to save a file in vi or vim.
How to save a file in Vi / Vim editor in Linux
The procedure is as follows:
- Open vim in Linux. For example, type: vim test.c
- Write code or programe
- Save file in vim by pressing ESC and ZZ
- This will save and exit in vim or vi text editor running on Unix or Linux
Let us see some more examples.
Save a File in Vim Text Editor
The vi or vim is a text editor who has three modes: command mode, input mode, and ex mode. When starting, vim or vi begins in command mode. One can press Esc key to return to command mode and issue various commands. 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 use the cat command. Run:
$ cat demo.txt
To just save file and not to exit to shell prompt, press [Esc] key and type w
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:
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 without saving changes i.e. discard changes and quit file|
|r fileName||Read data from file called fileName|
|w||Save file and continue editing|
|wq||Write and quit (save and exit)|
|x||Same as wq command i.e. 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.