Ubuntu

​​How to Exit/Quit Linux Vi Editor With or Without Saving Changes in Ubuntu 22.04?

How to Exit Quit Linux Vim Editor With or Without Saving Changes in Ubuntu 22.04

Vim (Vi Improved) is a free, open-source, text editor available in Linux, Unix, Windows, and macOS. Vim can be used both in GUI (Graphical User Interface) and the command line. Due to Vim’s flexibility and reliability, it is a very popular text editor. This article will demonstrate how to exit the Vim text editor without saving changes and with saving changes on Linux/Ubuntu 22.04 LTS.

What are the Modes in Vim Editor?

Vim editor furnishes three modes to manage files:

  • Command Mode: Command mode is enabled by default. It supports commands like redo, undo, replace, etc. We can exit command mode by clicking the “Esc” key.
  • Insert Mode: is enabled by “i”. It enables text insertion. We can exit command mode by pressing the “Esc” key.
  • Visual Mode: is enabled by “Ctrl+V”. It is used for selecting text for copy, cut, delete, paste, and other operations.

The installation of Vim is discussed in the next section.

How to Install Vim on Ubuntu 22.04?

Updating the system repository before installing any software/tools such as Vim Editor ensures a smooth installation process:

$ sudo apt update

After the system repository is updated, Vim can be installed by running the following command:

$ sudo apt install vim

From the below image, it can be seen that Vim is installed successfully:

How to Exit/Quit Linux Vim Editor Without Saving Changes in Ubuntu 22.04?

While editing files, sometimes, it is realized that the changes in the file are not required due to which it is decided to exit the file without saving the changes that were made. For instance, a file “5g.txt” can be accessed using the Vim text editor as:

$ vi 5g.txt

The file “5g.txt” is launched with the Vim text editor:

Next, some text is inserted in the file by entering “insert mode” by pressing “i” and typing text. Later, If it is required to exit the Vim editor without saving the file, we will switch back to command mode by pressing “Esc” and typing “:qi” as shown below:

The changes will be discarded and the file will be saved in the previous state. This can be verified by launching the file in the Vim editor:

$ vi 5g.txt

The below image shows that the changes are discarded and the file is in a previous state.

How to Exit/Quit Linux Vim Editor With Saving Changes in Ubuntu 22.04?

Most of the time when files are edited, they need to be saved so that data is not lost. A file “5g.txt” can be accessed using the Vim text editor as:

$ vi 5g.txt

The file “5g.txt” is launched with the Vim text editor. Next, some text is inserted in the file by entering the “insert mode” by pressing “i” and typing text. The file can be saved and exited by switching to command mode by pressing “Esc” and type “:wq” as shown below:

The changes in the file will be saved in the current state and the edit will be closed. This can be verified by launching the file in the Vim editor:

$ vi 5g.txt

From the below image, it can be seen that the edited line is saved in the file.

Conclusion

Vim Editor can be exited with or without saving changes by entering “:wq” and “:q!” respectively in command mode in Ubuntu 22.04. This article has demonstrated how to exit the Vim text editor without saving changes and with saving changes on Linux/Ubuntu 22.04 LTS.

Similar Posts