This tutorial picks up from where the 'using vi' page in the Beginners' Linux Guide finished where you learnt how to use a .22 calibre semi automatic rifle with sights calibrated up to 150 metres as a peashooter (metaphorically of course).
There is so much more to vi than knowing how to:
- go into insert mode (by either pressing the letter 'i' or the insert key)
- delete text to the right of the cursor one character at a time with 'x'
- delete text one line at a time by pressing 'd' twice in quick succession
- search for text with '/'
- save text to a file (:w) and exit vi (:q or :q! if you don't mind losing changes).
Say that again.
You can have a command repeated a number of times by first typing in the amount of times that you want the command to occur so to delete a seven lettered word just type '7x'.
You might wonder about the above line 'delete text to the right of the cursor' - is this excess pedantry or is it leading on to something useful?
The latter applies; pressing 'X' deletes the character immediately to the left of the cursor and typing 7X, for example, deletes any seven characters that are immediately to the left of the cursor.
So what happens if you are at the end of a 21 character line and you type '30X' - do nine charaters of the preceding line get deleted?
No, the above line will remain intact but any the text to the left of the cursor will indeed get deleted.
You can, if you want to, repeat a command just once; by pressing '.', this I suppose could be called the 'redo' button; it has a counterpart ('u') which will undo the last change.
There's more than one way...to delete a word.
Now, just to get back to deleting words. I mentioned that you could use '7x' to delete a seven lettered word. That's all well and good if you don't mind counting off the number of letters in whatever word it is that you want to delete. There's a better way to do this (there's always a better way).
You can delete words with 'dw' and 'db'; use 'dw' for deleting words to the right of the cursor and 'db' for deleting words to the left - and if you want to delete seven words then use '7dw'.
Moving the cursor.
This moves on (hur, hur) rather nicely to the next thing that I want to get to - moving the cursor around. Obviously you can use the cursor keys but there are other ways of doing it.
For example, if you want to move to the start of the previous sentence use '(', if you want to move on to the next one then press ')'. You can go squiggly if you want to move around one paragraph at a time and use '{' or '}' respectively. If you only want move around by a few words
then use 'W' and 'B'.
The great thing about all of this is precision.
And I haven't even gotten to the good stuff yet.
Cut & Paste.
Do you remember about 'dd', and that it deletes lines one at a time?
Well here's something nifty that you can do with it and just one other command.
Let's say that you have a four line block of text that you want to move (be it source code or whatever). Move the cursor to the first line of that block of text and type '4d', then move the cursor to where you want to put the text and type 'p'.
This will put the text you previously deleted back in, after the cursor. Using 'P' will insert the deleted text before the cursor.
Copy & Paste.
So now you know how to cut and paste text in vi.
The next thing to learn is how to copy and paste text and there really isn't anything to it at all; instead of putting text into the memory buffer by deleting it text needs to be put there without deleting it. This is done by yanking the text.
If you want to yank four lines of text into the buffer, move the cursor to the
first line of that block of text and type '4Y'. You can then use 'p' or 'P' to place a copy of that text elsewhere.
Other ways to change text.
There are a few other ways that you can use to edit text. If, for example, you have typed in 'ilug' where you meant 'ILUG' you can either press 'R' with the cursor at the start of 'ilug', type the abbreviation in with caps lock on and press ESC to get out of replace mode. Or you can do it quickly by positioning the cursor and then typing '4~'.
And if you want to delete to the end of a line 'D' will work just fine.
Windows.
Sometimes it can be quite handy to have more than one file open in vi at the same time - specifications in one 'window' and the source code in another for example.
To do this you need to have one file open first and then press Ctrl & w (the control and 'w' keys at the same time) followed by 'n'. This will result in a new window created at the top of the screen, with the screen divided equally between the two windows.
To edit a file in this new window you need to type ':e' followed by the name of the file. You can keep on doing this until you have as many files open as is needed.
Moving between the windows is fairly simple. Press Ctrl & w first to let vi know that you want to do something about the windows and then press:
- t if you want to move to the top one
- b if you want to move to the bottom window
- Up arrow or down arrow to move to a window in the middle, you can also use 'j' or 'k' if you don't have any arrow keys for some reason.
Typing ':q' will close the window that the cursor is in, if you want to close all other windows then press 'Ctrl & w' followed by 'o'.
That's all, folks.
I hope this tutorial hasn't left out anything that you really want to know about using vi - if it has please email me and let me know so I can cover it at some later stage.
Time for me to find out what's happening in the big black room.
The Declan clause.
This tutorial was written by Ken Guest, http://technobrat.net.
Copyright © Ken Guest 1999.
Publication of this work in any printed or electronic form in part or in whole for non private use without (a) the inclusion of the above copyright notice (b) written permission of the author is an infringement of copyright and thus prohibited by law and international convention.
Also by Ken Guest
- Samba Tutorial; using Samba for filesharing and sending messages between Linux and Windows.
- vi Tutorial #2, covering Search & Replace, filters and a few more tricks.
Please tell us what you thought of this tutorial:
|