vi-muman inter-active micro-manual for new vi users ................................................................................ In order to learn vi HERE and NOW, you are strongly encouraged to open this file with vi (lowercase), typing EXACTLY, at command prompt: vi -Readonly vi-muman.uk where: -Readonly (R uppercase) is the read-only option that will prevent you to modify this file; vi-muman.uk is only the name of this file. ^^^^^^^^^^^^^^^^^^^^ MEMORIZE OR WRITE SOMEWHERE! ^^^^^^^^^^^^^^^^^^^^ TO QUIT vi press step by step the keys [ESC] :q [ENTER] where: the [ESC] key is located on the upper left corner (of your keyboard!) and [ENTER] is the ENTER key. After pressing such keys, remember to lift your finger from them. Before you press [ENTER] you will see approximately this :q This command (quit) will appear in the last row at the bottom. If, due to some mysterious matter, strange phenomena occur, then press [ESC] :q! [ENTER] You will exit (quit!) from the file without damaging it. Then you will be able to re-open it. And now be careful not to press keys at random on the keyboard. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To go 1 page FORWARD: [CTRL] f (keep pressed [CTRL] and press f) To go 1 page BACKWARD: [CTRL] b (f stands for Forward, b for Backward) (continue) Well done! Exercises: a - go back to previous pages and then come back here. b - quit this manual (do you remember how to do it? If not: go back and study!) and re-open this file with vi, not forgetting the read-only option: if you don't remember how to do it go back to the initial page and memorize it (better in your head than on some sheet) c - keep doing exercises a, b till you have practiced enough. WARNING 1: in this manual there are many exercises: to avoid damages to the file it's a good idea to use the vi readonly option. WARNING 2: the idea not to do the exercises instead is much worst. ________________________________________________________________________________ CONTENTS ________________________________________________________________________________ 0 INTRODUCTION 0.0 WHAT is vi? 0.1 WHY to learn vi? 0.2 HOW to learn vi? 1 TO BEGIN 1.0 First step: COMMAND MODE and INSERT MODE 1.1 Basic commands 1.2 Moving 1.3 And now to work! 1.4 Searching strings 1.5 Using set 2 ADDITIONAL INFORMATIONS 2.0 Other commands 2.1 Substituting strings 2.2 Writing files 2.3 Reading other files 2.4 Opening different files at the same time 2.5 Opening a file in read-only mode 2.6 Shell's commands 2.7 Give me a shell! 2.8 Macros ________________________________________________________________________________ *** 0 INTRODUCTION -- 0.0 WHAT is vi? vi (lowercase) is a text editor, i.e. software that allows you to write text files. -- 0.1 WHY to learn vi? Why should I work hard to learn a mysterious and enigmatic text editor such as vi when there are so many others easy and user-friendly? Because of some very good reasons: a - vi is the only text editor you are sure to find on ALL UNIX/Linux machines; b - vi allows you to manipulate huge files; c - vi is exceptionally powerful; d - vi allows you to make complex operations in a very quick and efficient way. -- 0.2 HOW to learn vi? Gradually. It's a mistake to try to learn everything at once: you can do many things with vi, but in reality you can begin to work with a very limited number of commands. Very often you can remember better an apparently mysterious one-letter command if you notice that it is the first letter of a more meaningful english word. *** 1 TO BEGIN -- 1.0 First step: COMMAND MODE and INSERT MODE It's essential to realize that vi works with two modes: COMMAND MODE and INSERT MODE: * in Command Mode you can digit commands to tell vi what you are going to do; * in Insert Mode you can insert text in the file you are editing. Q- "In the first exercises I've done (exiting from vi, going forward and backward in the file) did I give commands?" A- Yes. Q- "But why all this confusion, Command Mode, Insert Mode, uh?" A- Because, despite the appearance, all this will facilitate your job, especially if you are working in an 'only-characters' environment. Q- "How can I switch from Command Mode to Insert Mode and vice-versa?" A- To switch from Insert Mode to Command Mode you have simply to press the [ESC] key. To learn to switch back read chapter 1.1 -- 1.1 Basic commands NOTE 0: generally speaking, in order to digit COMMANDS you must be in COMMAND MODE (hence the name). In doubt press the [ESC] key. The following commands will switch you from Command Mode to Insert Mode (so you will be able to insert text). iText (insert) insert the string "Text" beginning from the position of the cursor aText (append) append the string "Text" oText (open) open a new line downward and write Text OText (open) open a new line upward and write Text NOTE 1: vi distinguishes between lowercase and uppercase letters. NOTE 2: when you have finished writing your Text, you have to communicate it to vi, otherwise you will remain 'in aeterno' (forever) in insert mode. Q- "And how do I quit insert mode?" A- Press the key [ESC] Q- "This will switch me to command mode?" A- Exactly. There are no other modes: either command, or insert mode. NOTE 3: The commands preceeded by ":" will be written in the last line in the bottom and require you to press the [ENTER] key. The other commands won't be written. The following commands are frequently used, too (you will remain in command mode): J (join) join the current line with the next one x delete the character upon which is the cursor u (undo) undo the last modification... . ...do it again :w (write) save the current file (the one you are editing) :w anotherfile save the file to a new one named "anotherfile" :e anotherfile edit file "anotherfile" :q (quit) quit and enough; if you didn't save any modification it will ask you if you want to do it :q! (quit!) quit without saving the last modifications :wq (write quit) save and quit :7 move the cursor to the beginning of 7th line G move the cursor to the end of the file dd (delete) delete the current line Exercises: a - go to line 99, then to line 199 b - go to the end of this file using the G command (do you see odd things?) then go back to line 199 c - (you can find other exercises at the end of chapter 1.3, but it's better reading 1.2 first) -- 1.2 Moving Q- "How can I write text where I want instead of where vi wants?" A- Moving the cursor inside the body of the file. The keys you must use, in COMMAND MODE, are: h move the cursor toward the left... j ...the bottom... k ...the high... l ...the right You are invited to experiment by yourself. Of course if you use these commands in insert mode you will see your file filling with letters hhjjjjjkkkllll... Q- "But I will never remember all these commands!" A- Relax. Because I feel you will be tempted to use the arrow keys on your keyboard (maybe they work, goodness knows!) I want to tell you why this is not a good idea: the arrows keys not always work, and this can be puzzling. Q- "Well, then I will write them..." A- Wait a moment. Observe that hjkl keys are aligned on the keyboard. Furthermore, "j" has undoubtedly a ramification DOWNWARD, that can help you to remember its function; "k" stretches itself UPWARD; finally "h" is the most LEFT letter of the group (look at the keyboard, not at me) and "l" is the most RIGHT one. To move the cursor quickly there are other commands, too: w (word) move the cursor to the beginning of the next word b (backward) move the cursor to the beginning of the previous word e (end) move the cursor to the end of the (next) word Exercises: a - test all of the following keys: w, b, e, h, j, k, l -- 1.3 And now to work! Now, begin to play with vi! Always keep in mind in which mode you are: in doubt, the [ESC] key will switch you to command mode. Exercises: a - save the content of this file in the new one junk.txt b - save the content of this file also in the new one garbage.txt c - edit the file junk.txt quitting this file first: at command prompt use vi junk.txt now (not having used the -Readonly option) you have full power upon the file junk.txt: make havoc of it with the following exercises d - try to create new lines (upward/downward with respect to the cursor) e - add text using commands "a" and "i": what is the difference? f - what happens if, in INSERT mode, you press the [ENTER] key? g - save the file junk.txt h - move the cursor where you want using the h, j, k, l keys: do they work? (did you forget you have to use them in command mode only? In case, press [ESC]) i - edit the file garbage.txt WITHOUT first quitting junk.txt: use the command :e garbage.txt and spoil it with the following experiments j - join two lines (with which command?) k - let's assume you joined the wrong lines: undo the modification l - what happens if you undo the undoing of a modification? m - delete one character or more n - add two new lines of text o - try to make vi do exercise "n", using command "." p - try to delete lines using command "dd" q - continue to play (with one of the files junk.txt or garbage.txt) till you can comfortably perform all the above tasks. Good, what about creating a new file? Get a command prompt in another window (if you can, otherwise quit first this file) and digit: vi filename (Albeit it's possible, it's a bad idea editing the same file in two different places: so filename should be different from the name of THIS file) Notice that you DID NOT open the file with the "-Readonly" option. If the file already existed (and you don't care for it so much) you can destroy it with your experiments. If the file is completely new (recommended choice) you will note... Q- "What about all these signs at the beginning of each line, I've already seen somewhere?" A- ...exactly, they are 'tildes', and show the end of your file. You saw them at the end of this file, too. If your file is empty you will see only tildes, approximately in such a way: ~ ~ ~ ~ Exercises: a - repeat, at your pleasure, some of the exercises a-q above, in particular: b - insert the text you want in the place you want c - by the way: do the h, j, k, l commands work where there are the "~", too? (I don't mean the 4 "~" you see above, but those at the end of the file) -- 1.4 Searching strings To search (downward) the string "per exemplum" digit (now!) the command /per exemplum This command appears in the last line at the bottom. If not, you are NOT in command mode (so press [ESC] to switch to it). to find the next occurrence digit the command n (next) for the previous digit N The searches upward are identical but you have to use ?per exemplum and the meaning of n and N are inverted. Exercises: a - find all the string 'per exemplum' of this file using the command '/' and pressing repeatedly 'n' and 'N' b - repeat the exercise "a" using the commands '?', 'n' and 'N' You will notice soon that digiting /PER EXEMPLUM or /per eXemplum is not the same thing as /per exemplum This characteristic of vi, descending from the fact that UNIX systems distinguish between uppercase and lowercase characters, at a first glance can appear irritating, but actually it can be very useful. Try now digiting /per e.emplum Observe that will be found either "per exemplum" either "per eXemplum" (and to be honest even "per eZemplum"), but not "PER EXEMPLUM". The dot so means (in searching) an unidentified character Q- "And if I want to search exactly the string 'per e.emplum'?" A- Try with /per e\.emplum Generally speaking, there are some special characters vi interprets in a particular way. To make vi interpret them as standard characters it's enough you put a \ before them. For example: the asterisk (*) is also a special character; to search the beginning of each chapter, marked with '***', you will digit /\*\*\* Q- "What happens if, due to my lazyness, I digit the following command only?" /\*\* A- Try it. As you can see lazyness is punished. But being lazy myself I suggest that you test the following: /\*\* Q- "But it's the same!" A- No Sir, a blank has been added after the last asterisk. Try it (to come back here search the string 'No Sir'). With vi, you can see hidden characters: if the argument excites your interest now, search the string 'Using set'. Q- "Also '\' is a special character?" A- Yes. Try searching the string 'per e\.emplum'. Before going mad digit /per e\\\.emplum '/' is a special character, too: in order to search a '/' you have to digit /\/ Exercises: a - search the strings '\*\*\*' and 'per e\\\.emplum') b - find the beginning of each paragraph, marked with the string '--', but WITHOUT matching this exercise. Is '-' a special character?) c - search the string "--" of the previous exercise WITHOUT matching neither any paragraph nor this exercise. Is the apex (') a special character? And the quotation marks (")? d - What happens if I perform the following searches? (Try it!) /... .X...... /... .X /.....X the last search is really different from the last but one? e - search the string '../images' without matching './somedir/images' -- 1.5 Using set To see the hidden characters try (in command mode!) the command :set list The just appeared $ show you the end of each line and reveal the eventual blank (as for example at the end of this line). To get rid of all these $ digit :set nolist To see/[not to see] numbered lines :set number :set nonumber To see/[not to see] in which mode you are (written at the bottom on the right) :set showmode :set noshowmode But since vi is not famous for the prolixity of its commands, there are more synthetic alternatives (same meaning) :set li :set noli :set nu :set nonu :set smd :set nosmd Exercises: a - Try to see what happens digiting: :set nu li smd :set nu noli smd *** 2 ADDITIONAL INFORMATIONS -- 2.0 Other commands The following commands are not really vital: you will learn them step by step, but you should know they exist. IText (insert) insert the string "Text" at the beginning of the current line AText (append) add the string "Text" at the end of the current line dw (delete word) delete the current word, starting from the current position of the cursor (you remain in command mode) cw (change word) change the current word, starting from the current position of the cursor (you switch to insert mode) 5dw delete 5 words 5cw change 5 words s (substitute) substitute the character where the cursor is 5s substitute 5 characters beginning from the cursor x delete the character where the cursor is 5x delete 5 characters beginning from the cursor dd (delete) delete the current line 5dd delete 5 lines, beginning from the current one :18,22d delete the 5 lines from 18th to 22nd (included) D delete the remaining part of the current line beginning from the present position of the cursor cc (change) change all the current line 5cc change 5 lines, starting with the current one :18,22c change the lines from 18 to 22 (included) C change the remaining part of the current line ZZ save (if there is any modification to be saved) and quit. yw (yank) memorize in a buffer the current word... yy ...all the current line... 5yy ...or 5 lines starting from the current one NOTE: when you are copying (yank) or deleting anything, this ends in a buffer, for possible usage like the following (copy and paste): p (print) write the lines or the words contained in the buffer 0 (zero) move the cursor to the beginning of the line $ move the cursor to the end of the line :$ move the cursor to the end of the file (same as G) ^ move the cursor to the first non blank character of the current line 7| move the cursor to the seventh character of the current line H (high) move the cursor to the first line of the current page L (low) move the cursor to the last line of the current page M (middle) move the cursor to the middle of the current page [CTRL] u (up) go half-page upward [CTRL] d (down) go half-page downward [CTRL] y go one line upward keeping the cursor where it is [CTRL] e go one line downward keeping the cursor where it is [CTRL] g give informations about the current file [CTRL] l redraw the current page (refresh) [CTRL] h move the cursor backward in case of some modification (you can use it in insert mode, too) fx (find) find the next character "x" in the current line ; find the next character "x" in the current line , find the previous character "x" in the current line Fx (Find) find the previous character "x" in the current line ; find the previous character "x" in the current line , find the next character "x" in the current line Exercises: a - verify if the following commands work (to go back here search the string "Exercises") 0, $, :$, ^, 3|, H, L, M b - test the commands [CTRL] u, [CTRL] d, [CTRL] y, [CTRL] e, [CTRL] g, [CTRL] l, [CTRL] h c - put the cursor in the previous line and find all the "T" using f and F commands d - [CTRL] h is a quite particular command: d1) when you are in insert mode you can use it to correct a string while STILL REMAINING in insert mode; d2) when you are in command mode you can use it to correct a command you digited incorrectly. You are invited to perform some experiments: verify d1) Insert text and correct it BEFORE going back to command mode: for example you can digit WrongText and correct it in RightText WITHOUT going back to command mode. verify d2) Correct a command such as :set li (you wanted to digit :set nu). Correct the command /SearchWrongString in /SearchRightString without digiting the command from the beginning. e - commands yy, dd, used with p, are very quick and frequently used: try to copy or move one or more lines of text somewhere: put the cursor in the first line you want to copy, digit 3yy (for example), move the cursor where you want and digit p: where are the new lines written? (To move the lines use 3dd, p) f - you are encouraged, if not to learn them now, at least to verify that the commands allowing file modifications work (for these experiments use another file, type junk.txt) -- 2.1 Substituting strings To substitute an "old string" with a "new string" you can use various commands: :s/old string/new string if there are two strings "old string" in the current line it will substitute the first only :s/old string/new string/g substitute all the strings "old string" in the current line NOTE: In the following commands /g is optional: if you skip it only the first strings "old string" of each line will be substituted. :1,9s/old string/new string/g substitute all the strings "old string" in the lines from 1 to 9 :7,$s/old string/new string/g substitute all the strings "old string" from line 7 to the end of the file :%s/old string/new string/g substitute all the strings "old string" in the file -- 2.2 Writing files You already know that if you are editing the file "firstfile" with vi, :w save eventual modifications in the current file "firstfile" :w otherfile write the content of "firstfile" in the file "otherfile" You can do other, too: :3,9w otherfile write lines from 3 to 9 (included) of the current file in the file "otherfile" If "otherfile" already exists :w! otherfile overwrite the file "otherfile" :w>>otherfile add the current file at the end of "otherfile" :3,9w>>otherfile add the lines from 3 to 9 (included) of the current file at the end of "otherfile" :e otherfile edit the file "otherfile" Exercises: a - using the previous commands, starting from this file create a new one containing a list of vi commands that you think you will use more frequently. Hints: start numbering the lines of this file and refer to paragraph 2.2 -- 2.3 Reading other files If you need to append a file to another one at a well defined point you can use the command :r acertainfile append the file "acertainfile" to the current one, starting from the next line with respect to the cursor (the file "acertainfile" will still exist) Exercises: a - test if it's true: catch a pair of files of the kind garbage.txt and test it -- 2.4 Opening different files at the same time With vi you can edit different files at the same time, with vi firstfile secondfile thirdfile [...] vi will make you edit the files one after the other: you will first see "firstfile", then, with the command :n (next) you will switch to the second file and in the same way to the third. To switch to the preceeding file instead than to the next, digit :N NOTE: it's easy to remember! It's very similar to string search, but with the ":" Q- "Why should I be interested in editing different files?" A- Because you may have to do the same operation in many different files (searches, modifications, string substitutions...). Using this vi capability you can do all of this very quickly. -- 2.5 Opening a file in read-only mode You already know you can use the -Readonly option. That is vi -Readonly filename This is the long version. You can use the short one, too: vi -R filename Also with the option -R (the same as -Readonly) you can open various files, with vi -R firstfile secondfile thirdfile [...] -- 2.6 Shell's commands Q- "But what is a shell?" A - It's the interpreter of the commands. Or the spokesman of your needs to the operating system. In order to give whatever command to your operating system, you have to make it preceed by ":!". For example, if you are using a UNIX system and you want to digit "ls" WITHOUT quitting vi, digit (in command mode) :!ls (Of course if you are not using a UNIX system you should use YOUR operating system commands!) Exercises: a - test any command you want (but do not delete this file!) WITHOUT quitting vi b - test what happens concatenating commands (with "|"), for example (in UNIX systems) :!ps -ef | grep vi c - if your system accepts different commands at once try giving them within vi, for example digiting (in UNIX systems) :!pwd; ls -l; ps -ef | grep vi d - you were wrong! Unfortunately you drop a finger of yours on the [ENTER] key and the output of the previous command disappeared! Do it again! Hint: use :!! Q- "Can I do within vi anything I can do at command prompt?" A- Yes -- 2.7 Give me a shell! To open a shell while you are editing a file with vi, (in command mode) digit the command :sh To go back to vi close the shell (exit or CTRL+D in UNIX). Like magic the file you were editing will reappear! Don't be afraid: try it! Q- "What can I do with a shell?" A- You can give all the commands of your operating system. For example you can edit the new file "anotherfile" with: vi anotherfile Of course now you can open another shell in which you will be able to edit a further file "thirdfile" with vi thirdfile in which you will be able to open another shell, etc. etc.. But maybe it's better not to go too far, for the time being. -- 2.8 Macros Have you ever tried, with your usual editor, to delete the last character of each line? (maybe in a 4000 code lines program...) vi allows all this (and much more!) to be done quickly using macros. Unfortunately using macros is much beyond the purpose of this manual: if you feel the irrepressible desire to learn more, then I have definitively converted you to vi and you can deepen your knowledge by yourself. ________________________________________________________________________________ Copyright (C) 1999 - Massimo Pilolli, Modena - ITALY maxpilolli@yahoo.com Stefania Perrucci, Modena - ITALY To download: http://www.navigare.org/vi-muman.html This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; see the GNU General Public License for more details.