Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | vi / vim |
Time | Less than 1 min |
Vim Save And Quit The Editor Command
The procedure to save a file in vim / vi and quit the editor is as follows:
- Open the terminal application in Linux or Unix
- Next, open a file in vim / vi, type: vim filename
- To save a file in Vim / vi, press Esc key, type :w and hit Enter key
- One can save a file and quit vim / Vi by pressing Esc key, type :x and hit Enter key
Let us see all commands and examples in details.
How to open a file in Vim / Vi
You can start vi/vim by typing the following command:
vim file
vi file
vim [options] filename
vim helloworld.sh
Understanding vim modes
When we start vim/vi text editor, we are in normal mode. This mode allows use to use vim commands and navigate around the file as per our needs. In order to edit text, you need to enter the insert mode by pressing i key. This way, we can edit our program or configuration files such as /etc/nginx/nginx.conf.
Of course, you can go back to normal mode by pressing Esc key again.
How to save and quit the vi or vim text editor
To save and quit the vi or vim editor with saving any changes you have made:
- If you are currently in insert or append mode, press Esc key.
- Press : (colon). The cursor should reappear at the lower left corner of the screen beside a colon prompt.
- Enter the following command (type :x and press Enter key):
:x
OR
:x!
OR
:wq!
- Press ENTER key
- This will quit the editor, and all changes you have made to the document will be saved to the file.
Use x to save a file and exit:
How to trash all changes in vi/vim
If you do not want to save any changes, first press Esc key. To exit Vim without saving changes press :q! followed by ENTER key.
Getting more help about vim commands
Want to learn vim text editor? Start the vim tutor by typing the following command at the Linux, macOS, and Unix-like system:
$ vimtutor
$ vimtutor it
$ vimtutor fr
vimtutor in action
Summary of vim/vi commands
Command | Description |
---|---|
Press the ESC key Type :q! Press the ENTER key |
Exit vim without saving changes i.e. discards any changes you made. |
Press the ESC key Type :wq Press the ENTER key |
Save a file and exit. |
Press the ESC key Type :x Press the ENTER key |
Save a file and exit. |
Conclusion
You learned how to save a file in vim and exit the text editor using various vim commands. See the vim site or the following pages for more information.
- How To Exit From top Command In Linux / Unix / BSD / OS X
- How To Exit Vim Text Editor Command
- Vi / Vim: Save And Quit The Editor Command
- How to reload .vimrc file without restarting vim on Linux/Unix
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 15 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Hi,
thanks
w!= save without exiting
q!= exit without saving
To save and quit:
If you are currently in insert or append mode, press Esc key.
Press: (semi-colon) The cursor should reappear at the lower left corner of the screen beside a colon prompt.
Enter the following command (type wq! and press Enter key).
:)
Press: (colon) The cursor should reappear at the lower left corner of the screen beside a colon prompt.
colon is right.
none of these commands are working
Hit the ‘esc’ key and it will take you out of the cursor mode and give you a cursor at the lower left hand of the screen. Now enter wq! and it will save and quit.
Worked for me (Ubuntu 16.4). Thanks
Thanks for sharing knowledge!
Worked just perfectly!
worked for me too on Ubuntu. thank you!
The theme become so popular in the entire Www, so I’ve created fun page
Can’t believe I clicked your link :)
Thx. It helped me.
thanks so much
No problem.
no work
:wq
:wq!
wq!
Stop using :wq! to save and exit!
:wq is save and quit, the ! tells it to ignore errors and exit. This could potentially leave you with exiting a file unsaved due to an error.
:q! is exit without saving
:wq is “save and exit”