the vi
editor is confusing if you are not used to. It takes a secret handshake to escape this app if you have come across it. Here's how to quit vi or vim on Linux, macOS or any other Unix-like system.
The Quick Response
If you are in vi
O vim
and need to get out, with or without saving changes, that's how:
- First, press the Esc key several times. This will ensure
vi
you are out of Insert mode and in Command mode. - In second place, scribe
:q!
and press Enter. This saysvi
to exit without saving any changes. (If you want to save your changes, scribe:wq
instead of.)
If you want to learn Linux command line, you will need to know much more than that. Read on and we'll show you how vi
it works and why quit instructions are so unusual. vi
it is an important and powerful tool and the learning curve is important to note.
we, the ubiquitous editor
Because vi
is everywhere there are chances that you will face him. You can even find yourself inside vi
accidentally. Maybe someone will ask you to look at your Linux computer for them. Issue a command like crontab -e
, and vi
appear. Surprise, someone has set the default editor to crontab
be vi
.
Maybe you're managing a system where vi
is the only publisher, 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 get started vi
and opening a file is simple. Writes vi
, a space and then the file name. Press enter. The program that starts can be vi
or it could be vim
, and improved vi
‘. Depends on your Linux distribution; as an example, Ubuntu uses vim
. All instructions in this post apply equally to vim
.
vi .bashrc
The immediately noticeable difference between vi
and other editors is that when vi
start, can't just start typing text. That is because vi
it's a modal editor. Editing is done in a mode, Insert mode, and issuing commands is done in Command mode. vi
starts in command mode.
If you are not familiar with the concept of Insert mode and Command mode, may be disconcerting. Many of the commands you can run in Command mode impact the file you are writing. If you are in command mode but you are trying to write text by mistake in your file, it won't end well. Some of the keystrokes you issue will be recognized as commands. Those commands can delete or split lines, move cursor or delete text.
AND, no matter what i write, can't find a way out or out of the editor. In the meantime, your file is pretty trashed and the seemingly random beeps are driving you crazy.
Command mode and insert mode
You need to change vi
in the appropriate mode for what you are trying to achieve.
Command mode is the default mode when vi
spear. Unless you know better, will start trying to write. If you press the 'i' key, or any of the others 10 keys that invoke Insert mode (a, A, c, C, I, O, O, R, and S), suddenly he will see what you are writing. You are now in Insert mode.
This may seem like progress until you press one of the arrow keys. If it does, A, B, C or D will appear as the only letter on a new blank line. At the top of the file.
It's okay, we support you. This is surprisingly easy when you know how. Remember these two keystrokes: Esc takes you to Command mode and “i” takes you to Insert mode.
You must be in Command mode and enter the correct command to exit the editor.
From command mode to security
To enter command mode, press the Esc key. Nothing visible will happen. Hit it a few more times. If you hear a beep when you press the Escape key, then you are in command mode. The beep tells you “Stop pressing Esc, you are already in Command mode”. If you hear a beep when you press Esc, we are fine.
Write a colon, the lyrics “q” and an exclamation mark, no spaces. These three characters should appear on the far left of the bottom line of the terminal. If that is not the case, press Esc until you hear a beep and try again. Hit the Enter key when you can see them:
:q!
In this command q
is an abbreviation of quit
. The exclamation point adds emphasis, so it's like you're screaming “Shut up!” a vi
. That might make you feel a little better.
The exclamation point further instructs vi
for no save any of the changes you have made to the file. If you've gotten into vi
and without knowing what he is doing, you probably don't want to save the chaos you've created.
Once you're back on the command line, you may want to recheck to make sure the file has not been altered. you can do this with the following command:
cat .bashrc | less
When you're dating vi
, if you see a message that says “do not write since the last change”, means that you did not see the exclamation mark of the command. To prevent you from quitting smoking and losing the changes you want to keep, vi
is giving you the chance to save them. Just reissue the :q!
command with exclamation mark in place to exit vi
and abandon any change.
If you are sure, save your changes
If you are satisfied with the changes you have made to your file, you can exit and save the changes using the :wq
(write and exit) command. Make sure you are absolutely satisfied that you want your screen edits written to the file before continuing.
Write a colon, la letra w (to write) and the letter q (get out). Hit the Enter key when you can see them at the bottom left of the terminal:
:wq
The learning curve is worth it
Using vi
it's a bit like using a piano. You can't just sit back and use it; you must put some practice. Sitting cold and trying to learn on the fly when the pressure is on you to edit something is not the way to do it.. It makes as much sense as sitting at a piano for the first time just as the curtain rises on their opening concert..
Much of the power of vi
comes from its many keybindings, each of which performs a common editing task. That's great, but you can't benefit from them until you've memorized them, practiced and are part of your muscle memory.
Until then, if you are in vi
and looking at an important file, solo :q!
and leave gracefully. Your important file will thank you.