How to Exit Vim in Linux (Three Ways)

Vim is a versatile editor that you can use for simple text editing to make huge coding projects, as it comes packed with some great features like custom keybindings, a powerful search and replace tool, etc. All these features make Vim a perfect choice among developers. However, for beginners, Vim can be quite intimidating, especially when it comes to exiting the editor. So, in this article, we will teach you how to exit Vim, so you can spend more time writing code and less time figuring out how to close the editor.

1. How to Save and Exit Vim Editor in Linux

If you want to save your work before exiting the Vim editor, then the wq command will be handy for you. Here’s how you can save files and quit the Vim editor:

  • First, press the ESC key on the keyboard to enter the normal mode in the Vim editor.
  • Next, press the : (colon) key to enter the command mode.
  • Then, type the wq command after the colon and hit the Enter key to save and exit Vim.
command to save the document and exit vim

2. How to Exit Vim Without Saving Changes

Sometimes, you might simply want to quit Vim without saving the changes. In such cases, you should use the q!command in the Linux Vim editor. When this command is executed, Vim is closed without saving the changes in the current file. Here’s how it works:

  • First, press the ESC key on the keyboard to enter the normal mode
  • Then, press the : (colon) key to enter the command mode
  • Finally, type the q! command after the colon and hit the Enter key on the keyboard to exit Vim without making any changes to your existing file.
command to exit vim without  saving the file

Note: Use this method if and only if you are sure you do not want to save the work. Else, you will lose your progress in the Vim editor.

3. Use Keyboard Shortcuts to Exit Vim Editor

In addition to Linux commands, you can also use keyboard shortcuts to quit Vim. There are two shortcuts you can use to save and exit Vim files or quit Vim without saving the current file. These shortcuts come in handy when you’re in a hurry and don’t want to use commands to exit.

Save and Exit the Vim Editor

  • Press the ESC key on the keyboard to switch over to the normal mode
  • Use the combination Shift+zz to save and exit Vim

Quit Vim Without Saving Changes

  • Use Shift+zq to exit Vim without saving your work.

So yeah, if you are new to Vim in Linux, we suggest you get acclimated with these commands and keyboard shortcuts to quickly save your files and exit the editor.

Comments 0
Leave a Reply

Loading comments...