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.
Read the remaining 25 paragraphs
Source: How-To Geek