Well when I first started playing with Linux (redhat) I would only use Pico as a text editor. Now pico's great and all, but I had heard VI was a lot more advanced. Not only a lot more advanced, but a lot more confusing. So I did some research and started messing with it.
The biggest thing to comprehend, in my opinion, when it comes to VI is the 3 different modes. These of which are
Master,
Command, and
Text. Now my largest complaint about VI is the fact that it actually doesn't tell you anywhere on the screen what mode you are in.
However, when you first start VI you will be in master mode. You can always get to master mode by hitting [ESC] if it says
"Insert" or if there's a colon (both would be at the bottom of the screen) then hit [ENTER]. You can only switch from Master mode to Command mode, and Master mode to Text mode (insert) or vice versa.
So we'll start by creating a new file called "test_doc". Login to your Linux box, or if you're in front of it and in a GUI, open up the BASH Shell. Type the following command:
This will open up vi (the file won't actually be saved until you tell it to). Now you'll notice that if you type any text, nothing will happen (unless you press
[i]). This is because you're in Master mode as explain previously. So go ahead and press [i]. There should now be "insert" at the base of the screen, as you are now in text mode. Type a few lines of text and then hit [ESC]. By hitting [ESC] in Text mode (remember "insert"?) it will bring you back to Master mode. Now to save the file goto Command mode by typing colon (
[:]), remember you can only goto Command mode from Master mode and not from Text mode. There should now be a colon displayed at the bottom of the screen. Go ahead and type
w and hit enter. This will write the file to disk. Now it will bring you back into Master mode. If you want to leave VI enter Command mode (remember, colon) and type
[Q] and then hit enter. You can also run both the write command and the quit command at the same time by just typing
wq.
I hope this tutorial helped you understand the very useful, but at first confusing, VI tool.
This tutorial will be in a series of basic Linux tutorials that I'm working on and will try to maintain and keep up to date, as far as grammar goes and the technical aspects. Feel free to ask any question in this thread.
History of VI: VI was developed around the same time that UNIX was (well duh, as it is a UNIX based program ;)) (1974). It's basically just like any other text editor such as notepad, but you can do a lot more with it. The draw back with it is that you have to use commands to perform any action (such as writing the file to disk).