Linux logo

Ways to Exit Linux/Unix Vim

Introduction to Vim: The Powerful Text Editor

Vim, an advanced text editor compatible with Vi, proves invaluable for editing various plain text, especially beneficial for programming. Offering numerous enhancements beyond Vi, such as multi-level reverse, syntax highlighting, and on-line assistance, Vim stands out as a Unix text editor available on Linux, BSD, and macOS.

Known for its speed and efficiency, Vim operates seamlessly in a terminal or graphical user interface, distinguishing itself by its keyboard-centric control. Unlike conventional text editors, Vim relies entirely on keyboard commands, omitting the need for menus or a mouse.

Unix administrators and programmers globally utilize Vim to edit configuration files, scripts, and more, navigating and executing commands with precision through keyboard inputs.

So, How to Exit Vim?

Enough of Vim intro, that you might already know, let us come to the topic of this tutorial; how to exit Vim with or without saving.

Exiting Vim without saving

Follow these steps to exit the vim without saving the changes:

Step 1:

Press : <Esc>

(You should remain in insert or append mode if not, just begin typing on a blank line to get in that mode).

Step 2:

Press: <colon>

The cursor must come back at the lower left corner of the screen next to a colon prompt. The colon shows that what follows is a Vim command.

Step 3:

Type this:


and press Enter.

This will close the editor, and abandon all modifications you have made; all modifications to the file will be lost.

Save then Exit the Vim

In order to first save the file and then exit, follow these steps:

Step 1:

Press <Escape>

(You need to be in insert or append mode if not, just start typing on a blank line to enter that mode).

Step 2:

Press : <colon>

The cursor ought to reappear at the lower-left corner of the screen beside a colon prompt. The colon shows that what follows is a Vim command.

Step 3:

Type the following:


The “w” indicates that the file must be written, or saved which will overwrite the existing file and the “q” shows that vim must quit, or exit.

Further Reading:  Rename Directory/Folder in Linux

Finally, press the Enter.

This will quit the editor, and write all modifications you have actually made; all changes to the document will be conserved.

How to exit Vim using shortcut keys?

You may also close the Vim editor by using shortcut keys.

Press Esc >Shift + ZZ

This will save the file contents and then exit.

For quitting without saving the contents:

Press Esc > Shift + ZX

Summarizing the options (above + more) for quitting Vim

Here’s a list of commands for existing Vim:

Command Description
Esc Switch to command mode.
:w Write out modifications that were made.
:q Exit Vim.
:q! Exit without saving the changes made in the file.
:wq Save the changes and exit Vim.
: x Save the file contents and close Vim.
How to see changes made in file?

After exiting you can display the contents of the file by using this command.

$ cat filename

Best Practices for Exiting Vim in Linux/Unix

Exiting Vim might be a bit tricky for beginners, so here are some best practices to smoothly exit Vim in Linux/Unix:

  1. Switch to Command Mode:
    • Press Esc to ensure you are in command mode before executing any exit command.
  2. Basic Exit Commands:
    • :q – Exit Vim (only works if there are no unsaved changes).
    • :wq – Save changes and exit Vim.
    • :x – Similar to :wq, save changes and exit.
  3. Forceful Exit Without Saving:
    • :q! – Exit Vim without saving changes. Useful if you want to abandon modifications.
  4. Shortcut Keys for Quick Exit:
    • To save changes and exit: Press Esc, then Shift + ZZ.
    • To exit without saving: Press Esc, then Shift + ZX.
  5. Combining Commands:
    • Combine commands to save time:
      • ZZ – Equivalent to :wq, saves changes and exits.
      • ZQ – Equivalent to :q!, exits without saving.
  6. Verify Changes:
    • After exiting Vim, confirm changes using: $ cat filename.


Troubleshooting and FAQs: Exiting Vim in Linux/Unix

1. Q: I pressed Esc, but nothing happened. What went wrong?

  • A: Ensure you are not in insert or append mode. Pressing Esc in these modes won’t work. Type i or a to enter insert mode, then press Esc.
Further Reading:  What is grep Command in Linux?

2. Q: I accidentally pressed the wrong key, and now I’m stuck. What should I do?

  • A: Press Esc to ensure you are in command mode. Then, type :q! and press Enter to exit Vim without saving.

3. Q: I saved changes with :w, but how do I exit now?

  • A: After saving with :w, you can exit by typing :q and pressing Enter. Alternatively, use :wq to save and exit in one command.

4. Q: What if I want to save changes but not exit immediately?

  • A: Save changes with :w and continue editing. When you’re ready to exit, type :q and press Enter.

5. Q: I used :q!, and now my changes are lost. Can I recover them?

  • A: Unfortunately, using :q! abandons all changes. Regularly save your work with :w to prevent accidental data loss.

6. Q: How can I exit Vim using shortcut keys?

  • A: Press Esc to enter command mode, then use Shift + ZZ to save and exit. For exiting without saving, use Shift + ZX.

7. Q: Is there a way to see the changes I made in the file after exiting Vim?

  • A: Yes, use the command $ cat filename in the terminal to display the contents of the file and verify the changes.

8. Q: Vim is unresponsive. What should I do to force it to exit?

  • A: Press Esc to ensure you are in command mode. Then, type :x! and press Enter to save changes and exit forcefully.

9. Q: I’m new to Vim, and the commands are confusing. Any tips for beginners?

  • A: Practice basic commands like i for insert mode and Esc for command mode. Familiarize yourself with :w, :q, and :wq for saving and exiting.

10. Q: Can I customize shortcut keys for exiting Vim?

  • A: Yes, Vim allows customization. You can add key mappings to your Vim configuration file (~/.vimrc) for a personalized experience.

Remember, Vim has a learning curve, and mistakes are part of the process. Regularly save your work, and gradually you’ll become more comfortable with Vim commands.



Further Reading:  How to Delete Directories in Linux

Vim, a powerful Unix text editor, is widely used for editing plain text and programming code. Notable for its efficiency and keyboard-centric control, Vim offers features such as multi-level reverse, syntax highlighting, and on-line assistance.

Exiting Vim without saving changes involves entering command mode by pressing Esc, followed by : and typing q! to abandon modifications. To save and exit, press Esc, :, type wq, and press Enter. Shortcut keys like Shift + ZZ can also be used for quick exits with saving, while Shift + ZX allows quitting without saving.

In summary, key commands for exiting Vim include :q to exit, :q! to exit without saving, :wq to save and exit, and Shift + ZZ for quick saving and exiting. The summary emphasizes the importance of understanding these commands for efficient use of Vim, a staple for Unix admins and programmers worldwide.