Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Linux

How to Exit Vim/ Vi in Linux {3 Commands}

Introduction

Whether you want to write codes, take notes, or simply edit a piece of text, a text editor is what you need. Vim, or Vi (basically the improved version of Vim), is one such text editor for you. This editor is used in the Linux terminal, which is a perfect platform to establish interaction with your computer system. While you can work on a Linux terminal without a text editor, it is a great tool to create and edit text-based files.

This comprehensive piece of writing is all about the powerful text editor Vim and the ways to exit Vim. We will focus on different ways to quit Vi.

Exit Vim in Terminal

The terminal can be launched through the applications menu in almost all Linux distributions. You can also press Ctrl+Alt+T to launch it. Once you have opened the terminal, simply type “vim” and press the enter key to start working with the text editor. You can even type the name of a specific file through “vim filename” to start working on the chosen file. On the other hand, you can create a new file through the “vim newfile.txt” command. 

Now that you have learned to start and edit a file through this text editor, it’s time to know how to exit the vi editor. Once you have finished editing and viewing your file, you need to quit the Vi editor and return to the terminal window. We can perform the function in two ways: one is through shortcut keys, and the other is by using commands. Let’s check both ways in the below sections.

Also Read: How to Clear Terminal in Linux?

How to exit Vi through shortcut keys and commands?

Shortcut keys cannot be used to exit certain modes like “insert.” However, such a combination of keys can be easily used to quit Vi. Take a look at the following keys:

  • ZZ (Shift + Z twice): In normal mode, this key combination saves any due changes and lets the user exit the text editor without any hassle.
  • :wq + Enter: Typing “:wq” in Command-Line mode helps retain all the changes and quit vi.
  • 😡 + Enter A very similar command to “:wq,” it allows the user to save any modification before making the final exit.
  • :q! + Enter: It is a very useful combination when the user doesn’t have any need to save the changes.
  • At last, the user can simply press the Esc key to exit Vim.
How to exit Vi through shortcut keys and commands?

Final Words

When working in the terminal, quiting Vim might seem like a daunting task, especially for beginners. However, there are several ways to exit Vim efficiently and effectively. Vim offers various shortcut keys for quick exits, such as “ZZ” to save changes and exit, and the Esc key to exit Vim without saving. Commands, on the other hand, provide more nuanced control over the process. 
For example, “:wq” saves changes before exiting, “:x” is similar, but only saves changes if necessary, while “:q!” exits Vim without saving any changes. These techniques not only make navigating Vim more efficient but also help you to avoid losing important data. With practice, you’ll find exiting Vim in the terminal to be a simple and seamless process that will save you time and frustration.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles