Pictures worth a thousand UNIX commands and options:
I draw this to my students each semester and they seem to grasp VI afterwards.
VI is a finite state machine with only three states.
======== ======
VIM has 3 modes of Operation:input mode, Command mode & Ex mode.
Input Mode -everything that's type, all keystrokes is echoed on the screen.
Command mode or Escape mode -everything the type in this mode is interpreted as a Command.
ex Mode -This is another editor, ex. It's a line editor. It works per line or based on a range of lines. In this mode, a :
appears at the bottom. This is the ex editor.
In order to exit Vim, you can exit while your is in either the ex mode or in the command mode. You cannot exit Vim when your is in input mode.
Exiting from ex Mode
You need to is sure that is in the Command mode. To does that, simply press the Escape key.
Go to the ex mode by pressing the :
key
Use any of the following combinations on ex mode to exit:
:q
-Quit :q!
-quit without saving :wq
-save & Quit or write & quit :wq!
-Same as WQ, but force write incase File permissions is readonly :x
-write & Quit :qa
-quit all. Useful when multiple files is opened like:vim abc.txt xyz.txt
Exiting from Command mode
Press the Escape key. You probably has done this already if you is in command mode.
Press Capital ZZ ( shift zz
)-Save & Exit
Press Capital ZQ ( shift zq
)-Exit without saving.
Upon starting, vi goes into COMMAND mode, where can type short, few character commands, blindly. You know what is doing, this isn ' t for amateurs.
When you want to actually edit the text, you should go to INSERT mode with some one-character command:
- I:go to INSERT in the the the cursor
- I:go to INSERT mode at the beginning
- A:append after the cursor
- A:append at the end of line
- O: Open A new line below the
- O: Open A new line in the place of the
Now, answering the question:exiting.
You can exit VI from EX mode:
- Q:if Haven ' t made any modifications, or saved them beforehand
- q!: Ignores any modifications and quit
- Wq:save and quit
- X:this is equal to Wq
W and x Accept the file name parameter. If you started vi with a filename, you need not give it here again.
At last, the most important:how can you reach EX mode?
EX mode is for long commands so can see typing at the bottom line of the. From COMMAND mode, you push colon,: and a colon would appear at the bottom line, where can type the above commands.
From INSERT mode, you need to push ESC, i.e. the Escape button, going to COMMAND mode, and then:to go to EX mode.
If you is unsure, push ESC and that would bring you to command mode.
So, the robust method is Esc-:-x-enter which saves your file and quits.
Vim Usage Tips