/**********************************************************************/ /* Note : Quick intro on "vi", the default unix editor. */ /* By : Albert */ /* Version: 1.0 */ /* Date : 06/06/2005 */ /**********************************************************************/ This (very) short note, introduces "vi", which is the standard editor on unix systems. This is not a tutorial ofcourse (it's just a note) but it should contain enough info to get you started with vi, in, say, 30 to 60 minutes or so. This note might be "ok" for true starters. But for folks with experience on Unix or Linux, this note is likely to be too simple. 1. Starting and ending a vi session: ==================================== With "vi" you can create and edit textfiles, which are 'flat' files (or also called 'ascii' files). It means that such a file, contains only plain text, with no make-up (in the sense of bold or italic characters or different fontsizes). Hopefully, you have such a text file lying around somewhere. It would be nice if it would contain a few pages of text, on which you can do some exercises. -- Starting vi Let's see how we can start vi. Suppose you want to edit an existing document (existing_filename), then just enter from the prompt ($) $ vi existing_filename And, if you want to create a new document (new_filename), you just issue this command: $ vi new_filename # (that is, if you already decided on the name of the file) When you have opened an existing document, don't start typing text yet! It's quite easy to mess up things in vi, if you are a starter. First we are going to try some navigation, that is, we try to move around in that file. Please press the "Esc" key. I will explain later why. Now, try the usual "arrow keys" (or "cursor keys") to go lines up, or go lines down, or to move your cursor over the characters, to the left or to the right. (Ofcourse, if you don't have an existing file with some substantial amount of text, you can not do that exercise. That's why we encourage you to find one, even if it was only some "readme" file from some application). Try to experiment with the "Ctrl-D" keys, to go down half a screen, and try "Ctrl-U" to move up half a screen. Indeed, those two commands are quite handy to move quickly in your document. Also, try "Ctrl-B" and "Ctrl-F". Those commands will move your cursor even a whole page at every command. Now that we have some idea about navigating through the text, we need to know how to leave vi, with or without saving changes. -- Quiting vi When you press Esc, and then press the ":" symbol, I hope you will see the ":" character appear, on the left bottom of the screen. At that point, you can give a whole bunch of commands to vi. In this case, we just want to exit vi, and we can do that simply with the following: Enter: q! to quit vi, and not saving any changes (q from "quit") Enter: wq! to quit vi, but now saving your changes to the document (q from "quit" and w from "write") (Note: You may look at it also this way: press Esc, then enter ":q!" or ":wq!") So, just quiting vi and not saving anything (by using "q!", is ofcourse very handy, if you have messed up the document. In section 3, we will see how to enter en edit text, but first you need to know a few things about the "modes" vi can run in. 2. Modes of vi: =============== Vi has (at least) two modes, named the "command mode" and the "insert mode". - The "insert mode", is the mode in which you would use vi as an editor, that is, for entering text. There are multiple ways to enter the insert mode. In this note, we will only use the "i" and "a" keys. It's true, if you just press "a", or "i", you can enter text from the position where your cursor was at that time. But there is also a little difference between using a or i. - The command mode, is the mode you can goto, if you press the "Esc" key. For example, it's that mode where you can press ":" and the ":" symbol appears on the leftbottom side of your screen. Then, for example, you can enter "q", to exit vi. There are many commands you can enter from the command mode, as for example commands to format text, delete lines, delete complete blocks of text etc.. Remember, you can reach the command mode just by pressing "Esc", and it's quite easy to navigate in this mode, through your text. 3. A few words on navigation: ============================= If you are not sure in which mode you are right now, just press Esc. This makes sure you get into command mode. If you were in insert mode, you will be returned to command mode. If you were already in command mode, you will simply stay in command mode As we already have seen above: - Use the arrow keys (or cursor keys) to move your cursor up, down, left, right. - Use Ctrl-U and Ctrl-D to move half screens up or down. But the following key strokes are handy too: - If you want to move up one page in your text, use Ctrl-B (from Ctrl-Backward). - If you want to move down one page in your text, use Ctrl-F (from Ctrl-Forward). - Jumping to a linenumber: Suppose you have an idea about the linenumber, of the text (where you are interested in) may be located, then you may jump directly to that linenumber, by using: . press Esc (go to command mode) . press ":" . enter the linenumber (like for example :123) - showing linenumbers: If you like to see the linenumbers in front of the text, do the following: . goto command mode (press Esc) . press ":" . enter "set number" (do not type the quotes) if you want to get rid of the numbering, just use "set nonumber". - if you want to go quickly to the last line of your text: Go into command mode, then just enter "G" (that is capital g, using Shift-g) Note: If, for some reason, the cursor/arrow keys do not seem to work, you can still use this: h move the cursor to the left one character position. j move the cursor down one line. k move the cursor up one line. l move the cursor to the right one character For this type of compact note, this is enough for navigation capabilities. Let's move on to the subject of entering and editing text. 4. A few words on entering and editing text: ============================================ -- Entering text, deleting characters: In this section, we will start with a empty textfile, so start vi in the following way: $ vi A blank screen will appear. Press "i" to get into the insert mode, from where you can enter text. I a moment, I invite you to enter a line of text. At the end of that text, press the Return (or Enter) key, in order to move the cursor to the next line. (never mind typing errors, just type, we will correct the errors later) Type this text: I will drife to Switzerland today. Hope the customs will not find my block money. - If all went ok, you pressed the Enter key after the word "money". Now, your cursor is positioned under the "I". You discover an typo error, because you have a lot of black money hidden in your car, instead of block money. So, you want to change the "o" into an "a". There are several ways to do that. Let's try this: - First press Esc (to go to command mode) - Move the cursor, so that its exactly positioned at the "o". - Now press "x" (one time only!). Hopefully, the "o" gets deleted. Now you want to insert the "a": - Press i - type the "a". Again press Esc. What we have done, is illustrating the use of "x" in command mode. You can delete one or more characters with that. Ofcourse, if you were in "insert mode", and you pressed the x, vi would just have put that letter into your text. Let's first save what we have, to a file. Since you are in command mode, press ":" then enter "w mytext.txt" (for write mytext.txt). So, on the bottom of your screen, it looks like :w mytext.txt If you have entered that, vi saves your text to that file. As said before, if you are in command mode, you can enter text into the file, after pressing "i" (we have just seen that) or after pressing "a". There is a slight difference though. It's this: - If you press i, the cursor will not move and what you then type, will start from that exact location. - If you press a, the cursor will first go one position to the right, and what you then type, will start from that new position. There is no other way "to get a good feeling about the difference between i and", than to exercise a bit. -- Entering text, deleting lines: We are gonna start a new vi session. From the prompt type: $ vi myprg1.sh You will enter vi, press the "i" key, and try to type in the following text: for i in run jump play do echo See Albert $i echo Wow! done Actually, you have ceated a small script here. If you would make it "executable", and run it, you would see: See Albert run Wow! See Albert jump Wow! See Albert play Wow! ----------------------------------------------------- Note: if you indeed want to run it, do the following: from the promt, make the script executable: $ chmod u+x myprg1.sh You can (probably) run it now, with the command: $ ./myprg1.sh ----------------------------------------------------- Now suppose, you are not satisfied with the "echo Wow!" line in your script. You want a simple way to remove the whole line in one command. This is how you can do that. - Goto to command mode (press Esc) - Place the cursor at any place at that line (it does not matter where the cursor is, as long as it is somewhere at that line) - Now press "dd". That is, press the "d", and "d" again. The whole line will be removed. -- Search, and search and replace: Search text: If you want to search for a word throughout the text, this is a way to do that: - Make sure you are in command mode: - Press the "/" key. Hopefully, you see the "/" symbol appear, positioned leftbottom of your screen. Right behind the "/", type the word you want to search for, like for example "money". Press Enter. - If the word is found, vi will place the cursor at the first occurrence of that word. - If you want to repeat the search (search for other occurrences of that word), then press the "n" key. -- Replace text: If you want to replace a word in your text, with another word, this is how you can do that: - Goto to command mode (press Esc) - Press the ":" key. Hopefully, you see the ":" appear leftbottom on your screen. - Type behind the ":" %s/OLD/NEW/g where OLD is the word you want to replace, with the word NEW. Observe that you replace all occurrences of that word, with the new word, throughout all of your text. Typically, thats often what you want to do. Below you can see how other ways of replacement can be realized. First occurrence on current line: :s/OLD/NEW Globally (all) on current line: :s/OLD/NEW/g Between two lines #,#: :#,#s/OLD/NEW/g Every occurrence in file: :%s/OLD/NEW/g -- yanking, copy and paste: It's funny how you can sometimes hear experienced vi users yell "yank yank". It all has to do with copying and pasting lines of text. I like you to expriment with that in some text. In command mode, goto a certain line, and press "Y". Now goto to somewhere else in your text. Try "p", and now again go to another place in your text. Try "P". What did you see? Note: I you run a terminal on your Windows PC, that connects you to some host, it's easy to paste lines from Windows software, like text in notepad, into vi running on your terminal. Suppose you have a lot of text in notepad. Just highlight it, and copy it. Now goto your vi session. Most popuar terminal software, like putty, allows you to paste text, using the right button of the mouse.