Author: Dave McKay / Source: How-To Geek
The vi
editor is confusing if you’re not used to it. It takes a secret handshake to escape this application if you’ve stumbled into it. Here’s how to quit vi or vim on Linux, macOS, or any other Unix-like system.
The Quick Answer
If you’re in vi
or vim
and need to get out—with or without saving your changes—here’s how:
- First, press the Esc key a few times.
This will ensure
vi
is out of Insert mode and in Command mode. - Second, type
:q!
and press Enter. This tellsvi
to quit without saving any changes. (If you do want to save your changes, type:wq
instead.)
If you want to learn the Linux command line, you’ll need to know a lot more than that. Read on and we’ll show you just how vi
works and why the instructions for quitting are so unusual. vi
is an important, powerful tool and the learning curve is worth it.
vi, The Ubiquitous Editor
Because vi
is everywhere the chances are you’re going to run up against it. You can even find yourself inside vi
by accident. Perhaps someone asks you to look at their Linux computer for them. You issue a command like crontab -e
, and vi
pops up. Surprise, someone has configured the default editor for crontab
to be vi
.
Perhaps you’re administering a system where vi
is the only editor, or the only one that will work through a remote SSH session, and you need to edit a user’s .bashrc file.
The command to start vi
and open a file is straight forward. Type vi
, a space, and then the filename. Press Enter. The program that is launched might be vi
or it might be vim
, an ‘improved vi
‘. It depends on your Linux distribution—for example, Ubuntu uses vim
. All of the instructions in this article apply equally to vim
.
vi .bashrc
The immediately noticeable difference between vi
and other editors is that when vi
launches you can’t just start typing text. That’s because vi
is a modal editor. Editing is performed in one mode, the Insert mode, and issuing commands is performed in the Command mode. vi
launches into Command mode.
If you’re unfamiliar with the concept of Insert mode and Command mode, it can be baffling. A great many of the commands that you can issue in Command mode affect the file you’re typing. If you are in Command mode but you’re mistakenly trying to type text into your file, it isn’t going to end well. Some of the keystrokes you issue will be recognized as commands. Those commands are liable to delete or split lines, move the cursor around, or delete text.
And, no matter what you type, you can’t find a way to exit or quit from the editor. Meanwhile, your file is getting pretty mangled and the seemingly random beeps are driving you crazy.
Command Mode and Insert…
The post How to Exit the Vi or Vim Editor appeared first on FeedBox.