How To Exit From Vim?

Vim is a popular command-line text editor. As a command-line editor, Vim is managed and used with commands and keyboard shortcuts. Exiting from vim can be done in different ways. In this tutorial, we examine different exit methods like save and exit, exit without saving, exit by saving specific changes for the specified lines, and exit by killing the vim process.

Exit By Saving Changes

As a command-line editor, Vim provides different ways of exit where one of them is the exit by saving changes. You can use the following steps;

Exit with :wq

  • Press ESC key in order to change normal mode.
  • Use the :wq and then push Enter in order to save changes. The w is the write command which will write or save changes and then the q is used to exit or quit from vim.
Exit By Saving Changes

Exit with 😡

Another alternative is using a single command : x like below. This saves the changes and exit from the vim editor.

  • Press the ESC key in order to change normal mode.
  • Type and execute the : x command and press enter.

Exit with ZZ

Also, the ZZ command can be used to save and quit from vim. This saves the changes and exit from the vim editor.

  • Press the ESC key in order to change normal mode.
  • Use the ZZ keyboard shortcut.

Exit with :write and :quit

Alternatively, the long form of these commands can be used with the following steps. This saves the changes and exit from the vim editor.

  • Press ESC key in order to change normal mode.
  • Type and execute the :write command to save changes.
  • Type and execute the :quit command exit.
READ  How To Send/Set HTTP Header with curl Command?

Exit Without Saving Changes

You may want to exit from Vim but without saving the changes after the last save or open the file. You can follow these steps without a problem.

  • Press ESC key in order to change normal mode.
  • Type and execute the :q! command and press enter.

Save Specific Lines Changes and Quit

As command advanced command-line text editor Vim provides a lot of features. For example by using the following command only specified lines can be saved and then exit from Vim.

  • Press ESC key in order to change normal mode.
  • Type and execute the :1,10wq command and press enter. This will save lines between 1 and 10.

Exit By Killing Vim Process

Up to now, we have talked about exiting vim by using conventional and reliable methods. Alternatively, we can kill the vim process which closes the vim completely but this may create problems like unsaved changes, etc. The pkill command is used to kill the vim process. Also, the process name which is vim is provided as a parameter. Keep in mind that this kills all processes with the name vim.

pkill vim

Leave a Comment