How to Exit the Vi or Vim Editor

Brief content

  • To get out of VI or VIM without saving changes, press ESC several times to enter the command mode, then enter: Q! And click Enter.

  • To leave VI or VIM and make your changes, press the Escape key to make sure that you are in command mode, then enter: WQ and click Enter.

  • VI is a modal editor, the value for editing is in the insert mode, and the commands are in command mode. Switch between modes with ESC and I keys.

A vi The editor is confusing if you are not used to it. To avoid this application, a secret handshake is required if you came across it. Here's how to quit VI or VIM on Linux, MacOS or any other Unix system similar to UNIX.

How to immediately leave VIM or VI

If you are in vi or vim And you need to go out – with or without the preservation of your changes – this is how:

  • First, press the ESC key several times. This will provide vi It is located outside the insert mode and in command mode.

  • Mainly, type :q! And click Enter. It says vi Go without conservation of any changes. (If you want to save your changes, enter :wq instead of.)

If you want to study the Linux command line, you need to know much more than it. Read on and we will show you how vi It works and why the instructions for caring for themselves are so unusual. vi It is an important, powerful tool, and the learning curve is worth it.

VI, the ubiquitous editor

Because vi Everywhere there is a chance that you will face it. You may even be inside vi accidentally. Perhaps someone asks you to look at their Linux computer for them. You release such a team as crontab -eAnd vi jumps out. A surprise, someone has Set up the default editor for crontab be viField

Perhaps you control a system where vi This is the only editor or the only one that will work through the remote SSH session, and you need to edit The user .bashrc fileField

Start a team vi And open the file is simple. Type vispace, and then the name of the file. Click Enter. A running program can be vi Or it can be vimimproved vi. “It depends on your Linux distribution, for example, Ubuntu uses vimAll instructions in this article are applied equally to vimField

        vi .bashrc
    

Immediately a noticeable difference between vi and other editors is when vi You cannot just start printing the text. This is because vi Modal editor. Editing is performed in one mode, in the insert mode, and the release commands are performed in command mode. vi Starts into command mode.

If you are not familiar with the concept Insert the mode and command modeThis can be confused. Many teams that you can release in command mode affect the file that you type. If you are in command mode, but you are mistakenly trying to enter the text into your file, it will not end well. Some of the output that you release will be recognized as teams. These commands can remove or divide the lines, move the cursor around or delete the text.

And, regardless of what you are printing, you cannot find a way to leave or leave the editor. Meanwhile, your file becomes quite mutilated, and, it would seem, random sound signals drive you crazy.

Bad editing in VI, with random letters added to the file.

Command mode and insert mode

You need to switch vi In the appropriate mode for what you are trying to achieve.

Command mode – default mode when vi launch. If you do not know better, you will begin to try to print. If you happen to press the “I” key or any other 10 keys that cause the insert mode (a, a, C, C, I, O, O, R, S and S), you will suddenly see what you are typing. You are now in the insert mode.

This may seem progress until you reach one of the arrow keys. If you do this, a, b, C or D will appear as the only letter on a new empty line. At the top of the file.

It's all right, we have a top. It is surprisingly easy when you know how. Remember these two keystrokes: ESC leads you into command mode, and “I” leads you into the insert mode.

You must be in command mode and enter the correct team to leave the editor.

Enter the command mode in VI to get to safety

To enter the command mode, press the ESC key. Nothing visible will happen. Hit a few more times. If you hear a sound signal when you click on the Escape key, then you are in command mode. A transparent signal tells you: “Stop pressing ESC, you are already in command mode.” If you hear a sound signal when you get to ESC, we are fine.

Enter the colon, the letter “Q” and the exclamation mark without any places. These three characters should appear in the extreme left corner of the lower part of the terminal. If they do not do this, click the ESC until you hear the sound signal, and try again. Press the Enter key when you can see them:

:q!
The: Q! The team is used in VI.

In this team q It is an abbreviation for quit The exclamation point is added by the emphasis, so you, as if you are shouting: “Leaving!” V viThe field can make you feel a little better.

The exclamation point also instructs vi In order not to save any changes that you could make to the file. If you have passed the error in vi And not knowing what you are doing, you probably do not want to preserve the chaos that you applied.

As soon as you return to the command line, you may want to check to make sure that the file was not changed. You can do this with the next team:

cat .bashrc | less
The .bashrc file is visible less.

When you go out viIf you see a message that says “there is no record from the moment of the last change”, this means that you missed the exclamation mark from the team. So that you do not leave and lose any changes that you can save, vi gives you the opportunity to save them. Just a reprint :q! command an exclamation mark vi And abandon any changes.

VIM's message

If you are sure, save your changes

If you are satisfied with the changes that you have made to your file, you can go out and save changes using :wq (Write and leave) team. Make sure you are completely satisfied that you want your screen editors to be written to the file before continuing.

Enter the colon, letter W (write) and the letter Q (throw it). Press the Enter key when you can see them at the bottom of the left side of the terminal:

:wq
The WQ command is used in VI.

VI training is worth it.

Using vi A little like the use of the piano. You cannot just sit down and use it; You must put some practice. It’s cold to sit before and try to study on the fly when you are pressure on you, to edit something, this is not a way to do it. This makes the same meaning as sitting on the piano for the first time, just as the curtain rises for your inaugural concert.

Most of the power vi It comes from numerous combinations of pressing tension, each of which performs a common editing task. This is great, but you cannot benefit from them until you remember them, practice them, and they are part of your muscle memory.

Until if you find yourself in vi And looking at an important file, just :q! And gracefully go out. Your important file will thank you.

Leave a Comment