8/06/2020

How to exit from VIM (VI) ?

Have you or a loved one ever had to reboot your computer to get out of a read-only file in VIM?

  • Are you responsible for a Linux system or set of systems where a graphical editor or adding new editors is prohibited?
  • Are you taking an online training course where all the labs use the VIM editor, and it’s driving you crazy?
  • Do other Open Source Pros tease you about not knowing VIM?
This short tutorial will help you get past the main issues you might encounter in quitting VIM. 

Use this tutorial to safely quit VIM, without the headaches

First off, you need to know what user you are when you edit files with VIM, so at the command line enter the id command:

IMPORTANT: One of the keys to not getting stuck in VIM is to never edit a root-only file as a regular user.  If you are the system admin, look into using the sudo command to allow editing root-only files from a regular account safely!

If you are in VIM and cannot seem to quit, we’ve got your back!  Relax, grab a drink of water, and let’s walk through a few very common scenarios.
Scenario #1
You are editing a root-only file as a regular user, have made changes to the file/buffer and it won’t let you out, showing you a message like this:
Solution #1
Press the ESC key twice or until you hear the “error bell”, then press :qa! which will force quit all open files/buffers and take you back to the command line.
Scenario #2
While trying to quit VIM with the keystrokes :q, you suddenly find the lower portion of your screen looks like a history list of the commands you’ve issued recently in VIM, and it freaks you out.
Solution #2
You have accidentally used the keystrokes q:, which opens up a Lastline or CommandLine history mode window that you could choose previous commands from to execute them again.  You can close this by carefully pressing :q again, which puts you back into Command/Normal Mode and you can quit as you would normally.
Scenario #3
You have tried to force-quit the file/buffer using :q! and suddenly you find yourself in what appears to be the command line, but it’s telling you that the q: command is not found.
Solution #3
You’ve flipped the keystrokes around and entered :!q, which is telling the VIM editor that you want to run an external command (:!command) and of course it won’t find the q: command, so all you get is an error!  Press ENTER and you’ll be back inside VIM and can exit normally, just be careful to type exactly what you want!

No comments:

Post a Comment

Popular Posts