Syntax to Exit and Save from VIM Editor
Press esc key to ensure you are in command mode. Then press the colon key to enter command line mode. Command line mode will have a colon at the bottom left of the screen where you can enter command lines. Then press ‘wq’ and ‘enter’ to indicate exit and save. ‘w’ is short for write indicating save the file contents to disk. ‘q’ is short for quit which indicates exit vim editor.
The screenshot below is of entering the ‘wq’ command in command line mode to instruct vim to exit the program after saving the contents, aka write and quit.
Understanding VIM Modes and How to use Them
Vim’s primary modes of operation are Insert Mode, Command Mode, Command Line Mode and Visual Mode. The editor will be in one of these modes at any time, and interacting with the vim editor using keystrokes will take different actions depending on which mode the editor is currently in. If you try to enter ‘wq’ to save and exit, when in insert mode it will not in fact save the contents you are working on nor exit the vim editor. So therefore you need to be aware when using vim which mode you are in.
Insert Mode: in this mode you can actually type content into a file. You are doing editing and not instructing Vim the editor what to do other than what text to enter into a file contents. You can enter Insert Mode from the Command Mode by hitting the ‘i’ key which stands for insert. You can also hit the ‘a’ key which will also enter the insert mode, but after moving the cursor after the current position, which is why ‘a’ in this case stands for append.
Command Mode: this is the mode where keystrokes are used to instruct vim what to do and how to function but not for entering new text or editing text in a file. To enter Command Mode hit the ‘esc’ key on the keyboard.
Command Line Mode: a little mini command line prompt appears at the bottom left of the screen and allows you to enter commands such as we have shown in this tutorial, you can use ‘w’ which stands for writing a file and you can use ‘q’ which stands for quitting the editor. These can be combined into a single instruction and you will exit the editor and save the file.
Visual Mode: visual mode allows you to highlight text and then use that selected text in copy and paste or other common operations on selected text. All of this can be done from the keyboard without a mouse unlike GUI based editors such as Notepad or Visual Studio.
How to Save Your Contents but not Exit the Editor
If you want to save your work in the file you are editing but not exit the vim editor just use the ‘w’ command to write the file and omit the ‘q’ command as shown below.
How to Exit the Editor But Not Issue a Save
If you want to exit the editor only, and not issue a save command, then just issue the ‘q’ command in the command line mode as shown below. This command assumes you have not made any changes so far to the content or else you will get an error as shown in the second screenshot below.
No error above.
Error above because exit command was issued without a save file command.
How to Exit the Editor But and Not Save Changes
If you have made changes, but you DO NOT want to save these changes to disk and want to revert to the previous content at the time of the last save issue the ‘q’ command with the ‘!’ command at the command line mode as shown below.
Conclusion
All of the information above may seem like too much complexity just to exit the editor and save the contents of the file you are working on, but there are reasons for it and everything is logical. The vim editor can be operated almost entirely from the keyboard without use of the mouse which is ergonomically and speed wise usually more efficient for programmers and experience IT professionals. Therefore all of the instructions must be specified with different key combinations and not the use of a graphical mouse pointer. In order to accommodate rapid programming and usage of VIM the different modes of operation were introduced so the same keys can have different actions depending on the current mode. So when you want to do a quick simple command to exit the editor while at the same time saving the contents of the file you are doing, it’s just a few keystrokes on the editor and not browsing through menus with a clumsy mouse. Learn the tricks of VIM and you will be on the path to being an elite coder or systems administrator.
More VIM Education
- Buy the O’Reilly book on VIM on Amazon called Learning the VI and VIM Editors
- Browse the online tutorial by Daniel Miessler called Learn vim For the Last Time: A Tutorial and Primer
- Browse the org project home page