How to vi text editor

The text editor vi, is supplied with all Linux distributions. It is a difficult editor to come to grips with initially, but once you do the rewards are vast. The editor is quite intuitive once you get the hang of it. The main thing to remember when using it is that the very last line on the screen (Or window that the editor is running in), is the status line, much like the status bar at the bottom of Netscape, or *cough* Internet Exlporer. It will tell you there what mode your in, and what operations have been completed - such as save or quit.

Lets start by firing up the editor with a blank text file. From the command line, type vi blah. Another cool thing about Linux is that you dont need to supply the legacy ".xxx" extensions given to us by DOS, and Windows. The editor will appear on-screen, with a tilde, ~, on evey line, and at the bottom of the screen it says "blah" [new file]. If you want to put anything into this file, you have to put vi into insert mode. Do this by typing the letter 'i' on its own. "-- INSERT --" appears on the bottom of the screen. Now you can type in anything you want. Try typing "Hello", and then on a new line "World". Now try going back up to the line above, using the cursor keys and try to delete Hello. You wont be able to. When you give a Carraige Return (Press Enter in other words), the text on that line becomes static. If you want to delete it, heres how.

Press your Esc button on the top left of the keyboard, and the "-- INSERT --" at the base dissapears. Vi is ready to accept a new command. The only thing here is that vi will only delete what is in front of the cursor, let me show you. Bring the cursor to the start of Hello, and press the x key once. Now only 'ello' remains. This doesnt really make any sense, so we'll delete the line altogether. Press 'd' twice in quick sucession. Now we have the word 'World' on its own. Press 'i' and insert "Hello" again. This time, leave the text in insert mode. Press the "Insert" key on your keyboard, its above the cursor keys. The "-- INSERT --" at the base of the screen has been replaced with "-- REPLACE --". Now type the word "Nurse" over the existing "World". Press Esc again to return to the ready mode.

Vi has a search tool too, which I find indispensable. From the ready mode, type in a slash, /, and input the text you want to look for. Remember, since Linux is case sensitive, looking for "money" is not the same as looking for "Money".

Well, what about saving the file, or quitting without saving? What about just quitting? Its easy. From the ready mode, type in ":w" to write the file ... you already supplied the file name when you typed 'vi blah' earlier. The editor will return this on the status line : "blah" [New File] 1 line, 12 characters written. Lets quit out of it altogether. Type in ":q". You have been returned to the command prompt. There was an easier way of doing that ... from the ready mode in the editor, you could have just typed in, in capitals, "ZZ" ... this saves the file and quits. If you had wanted to quit without saving the changes, you could have just typed in ":q!", and nothing would have saved ... blah wouldn't have existed.

Thats the basics of vi, but you could always 'man vi' from the command prompt ...



--

No comments:

Other Articles

Enter your email address: