Home computickets How do I exit the Vi or Vim editor?

How do I exit the Vi or Vim editor?

Author

Date

Category

Worked on the command line. I got into a strange text editor in which the mouse does not work. I see the message

“type: quit to quit VIM”

But the command is written directly to a text file and is not executed.

What to do, how to log out?


Answer 1, authority 100%

Welcome to Vim!

Before entering the command, press Esc . Press Enter to confirm the command later.

The Esc key switches Vim to normal mode … If in this mode you press : (check that the English layout is set and CapsLock is unchecked, then simultaneously press Shift and ; ), then the : will appear at the bottom of the screen and the editor will go to command line mode . This ensures that you are entering the command right now and not editing the file. Remember that the case of the commands you enter is important.

Most commands have shorter versions. The optional part is given in square brackets: to [command] .

Commands given in italicized are only suitable for Vim (not implemented in Vi).

Safe exit (does not work if there are unsaved changes)

  • : q [uit] Quit the current Vim window . If this window is & nbsp; – the last, then exit Vim. If there are unsaved changes in the current buffer , the command will not work.
  • : qa [ll] Close all windows and exit Vim. Will not work if there are unsaved changes in at least one buffer.

Exit with confirmation (prompts for confirmation if there are no saved changes)

  • : conf [irm] q [uit] Close all windows and exit Vim. Asks for confirmation if there are buffers with unsaved changes.
  • : conf [irm] xa [ll] Save all changes, close all windows and exit Vim. Asks for confirmation if any buffers cannot be saved.

Save (save) changes and exit

  • : wq Write the current buffer to its corresponding file (even if there were no changes) and close the window. : wqa [ll] for all windows.
  • : wq! Same, but writes even read-only files. : wqa [ll]! for all windows.
  • : x [it] , ZZ (but with some features ). Save file only if there are changes and exit, : xa [ll] for all windows.

Undo (and lose) changes and exit

  • : q [uit]! ZQ Exit without saving, including when there are changes in visible buffers. Will not work if there are changes in hidden buffers as well.
  • : qa [ll]! , : quita [ll] [!] Quit without saving, undoing all changes in both visible and hidden buffers.

Press Enter to execute the command you entered.

This answer still does not cover all possible ways to exit Vim. A complete list can be found in the Vim documentation .

At the time of publication, all links are correct and lead to the English-language version of the documentation, but the site is temporarily unavailable. When it comes up, I’ll add links to the Russian version.

Vim also has built-in documentation. To open it: Esc : help Enter . To use the Russian-language embedded documentation, you must enable Russian localization in the Vim settings .


This is a translation of my own answer to EN.SO . This was written using the Vim documentation and, as a guide and source of inspiration, responses from other users, especially @dirvine.


Answer 2, authority 9%

Exit and save changes to file:

  • Press Esc first
  • then: wq!
  • then enter

If you want to exit without saving changes , then instead of: wq! you must write : q!


Answer 3, authority 7%

Better leave like this:

Esc then : q! and confirm with Enter .

Replace the editor with the one in which the exit will not cause difficulties:

export EDITOR = notepad.exe

After that, repeat the command, after which you got into an “unfamiliar” editor.


And only if you are absolutely sure, then exit with saving changes:

Esc then : x and confirm with Enter .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions