Vi For Smaties Linux Editor
Vi For Smaties Linux Editor
Basic Concepts
1. Start by typing vi followed by the file(s) you want to edit. Example: vi one.txt two.txt etc.txt 2. There are two modes: command mode and insert mode. You will begin in command mode. (Unless you have a config file which changes this. Rare.) 3. Hitting the Esc key always puts you in command mode. It never takes you out of command mode. 4. Commands only work in command mode. Get used to hitting Esc a lot. 5. Esc is your friend. If you begin to issue a command and then change your mind, always hit Esc a few times to cancel. 6. Remember: ALL COMMANDS ARE CASE-SENSITIVE!!!
Lesson Two
2. Lettered buffers are accessed with the " double quote. Example: "a3yy yanks three lines into the a buffer starting with the current one. Example: "bp pastes the contents of buffer b to the line under the current one.
Lesson Three
2. Use :set ts=4 to set the number of spaces used to display the tab character to 4. The 4 can be some other value if you wish. 3. Use :set ic to make string searches with / ignore cases. Example: After :set ic using /foobar will find foobar as well as FooBaR.
Lesson Four
Useful Tricks
1. G by itself goes to the last line of the file. 2. ~ (tilde) toggles the case of the character under the cursor. 3. When deleting blocks of text, move to the first line of the block, type ma to mark it, then move to the last line of the block and type d'a to delete the block. 4. Use the above trick with y'a to yank blocks of text.
Useful Symbols
1. Outside a colon command, $ stands for the end of the current line. Inside a colon command, $ stands for the last line in the file. Example: $ goes to the end of the current line. Example: d$ deletes from the cursor to the end of the current line.
Example: :$ goes to the last line in the file. Example: :24,$ d deletes everything from line 24 through the last line in the file, inclusive. 2. Outside a colon command, 0 (zero) stands for the beginning of the current line. Inside a colon command, 0 (zero) stands for line 0, or the line that is going to be created above the first line in the file. Example: 0 goes to the beginning of the current line. Example: :10,20 m 0 moves lines 10 through 20, inclusive, above the first line of the file.
Shell Operations
1. Use :!command to execute command without leaving Vi. Example: :!date will execute the shell command date and display its output. 2. Use :sh to spawn a new command shell inside Vi. Use exit to leave that shell when done.
Quiz One
1. Your friend comes to you with an odd request. All she wants you to do is make a simple text file containing the word hello. She asks that you name the file hello.txt. How would you do this using Vi? (Remember to save and quit.) 2. Bad move. Now your friend knows you can use Vi, and she wants you to make a longer text file named dummy.txt which contains the sentence "Vi is for dummies." once per line for 23 lines. What is the most efficient way to do this with Vi? 3. Your annoying friend is back. She has seen how efficient Vi is, and now wants you to replace all occurrences of the word dummies with the word Smarties in the file dummy.txt. What is the quickest way to do this in Vi? 4. She's sulking because her own editor seems useless now. Unfortunately, you've wasted so much time showing off that you've forgotten you have a report to finish. You've also forgotten to wear your watch today, and you need to make this very important deadline. Quickly, how do you access the system's correct time via the Unix date command without leaving Vi?
Vi is for dummies.
followed by the Enter key. Then hit Esc to enter command mode and type: 22
The dot command repeats the last buffer change the number of times specified, in this case 22 times. You only needed to manually enter the sentence once on the first line. Don't forget to use ZZ to save and quit. Note that the above answer does not work with Solaris vi (and probably most non-clone implementations), so the "ugly" answer is also acceptable. You will know what the "ugly" answer looks like when you see it, and it will still contain the same number of occurrences of the desired text. Update 2007-11-18: Bryan Deter pointed out that a more efficient solution would be:
23iVi is for dummies.
followed by the Enter key and Esc. Note that this also suffers from the same problem with Solaris vi.
Then hit ZZ to quit. The : (colon) makes this a "colon" or "ex" command. Do not forget the colon. The s stands for substitute. The g at the end stands for global, meaning the action should be performed for all instances of a string found on any given line. The % sign means " Do this for all lines in this buffer." Thus, the command can be read from left to right as: For all lines of text in this buffer, substitute the string "dummies" with the string "Smarties" for every occurrence of the string "dummies" found on any given line.
Lesson Five
Dangerous Pitfalls
1. When you abort a command, ALWAYS hit Esc before continuing. It's easy to lose a lot of work if you forget this! Example: If you hit d to begin deleting a chunk of text, then change your mind and decide to go look at the last line of your file by hitting G, you'll lose the rest of your file starting from your current cursor position. 2. Use :w to save your file regularly. 3. Start vi with vi -r filename to recover the swap file of filename if you lost your editing session prematurely. The swap file may or may not be complete, but it's better than nothing.
Display Control
1. If you hit Q you will enter the line editor, ex. Type vi followed by the enter key to return to vi mode. 2. Use z. (remember the dot) to redraw the screen. Rarely necessary.
Lesson Six
Lesson Seven
1. Use :map followed by a space, a key, another space, and a macro definition to map a complex command or series of commands to that key. Not all keys can be mapped with this command. It is best to choose a key which you rarely or never use in your editing. If the command includes any special characters, such as Esc, Enter, Tab, or any Ctrl characters, use a <Ctrl-v> (hold down the Ctrl key and press v) before each special character. Example: :map @ I/* <Ctrl-v><Esc>A */<Ctrl-v><Esc>0 followed by hitting Enter will make it so that hitting @ in command mode will insert C style "/* */" comments (without the quotes) around the line which the cursor is on. Vi will go to the beginning of the line, insert a "/* " (without the quotes), hit Esc for you, go the end of the line, append a " */" (without the quotes), hitEsc for you again, then place the cursor back on the beginning of the line and leave you in command mode. 2. BE VERY CAREFUL! Sometimes macros can become recursive. If you are not an advanced macro user, do NOT attempt to put the key being mapped anywhere inside the actual macro definition itself. Example: In the C commenting macro above, you would not want to put the @ symbol anywhere in the rest of the macro.
Customizing Vi (.exrc)
1. Any of the colon (ed/ex) commands may be put into a setup file in your home directory called .exrc so that you may customize Vi to act however you want without having to type in each setting by hand every time you start the editor. Your .exrc should contain one complete command per line. When entering commands into the file, omit the beginning colon. Thus, a line that would normally begin with :map would instead begin with map when it is in your .exrc file. 2. Users of the various Vi clones, such as Vim, Nvi, and Elvis, should consult their documentation to see if there is a more appropriate name for the setup file. Vim, for example, uses .vimrcinstead.
Quiz Two
1. Your friend is making it hard for you to concentrate. At one point, you decide to move lines 20 through 80 inclusive to the point between line 1 and line 2, so you cut them out of the text by moving to line 20 and typing 61dd. Before you can paste them to the desired location, however, your friend points out that you have an extra "the" in one of your lines, so you delete it with dw to shut her up. Oops. Now how do you recover those 61 lines you had previously cut and then paste them to the desired location at the top of your file? ANSWER 2. Your friend finally found something more fun than pestering you. Candy. While she is out looking for a vending machine, you notice that you have been repeatedly typing the phrase, "deoxyribonucleic acid strands" (without the quotes) because your mean instructor will not allow students to abbreviate. How would you arrange it so that whenever you type the word "dnas" (without the quotes) in insert mode, it immediately expands to the correct phrase? ANSWER 3. She's still out looking for candy. You are feeling better now that you don't have to keep typing that long phrase over and over again. But now you want the text to "wrap" for you whenever it goes beyond column 72, and you don't want to have to see it coming each time and manually hit Enter yourself. You also need to use the restroom, and you have to log out each time you leave this room. Quickly, how do you set vi to wrap the text for you and then make both this setting and your "dnas" macro from the previous question permanent? ANSWER 4. You knew this would happen. You return from the restroom to find that your friend has purchased her candy and is now occupying your seat. There are no other available terminals, and you need to get your work done. Your friend offers to forfeit her game of Tetris and let you have the terminal if you help her edit a quick reply to one of her email messages. She is too lazy to keep hittingEnter three times, going up two lines, and hitting Space five times whenever she wants to insert her reply in the middle of part of the original sender's message. How would you map the @ to do this in Vi? ANSWER
Lesson Eight
one specified. Example: :25r foo.txt will read in the contents of the file named foo.txt starting at the line under line 25. Thus, the first line of foo.txt will become line 26 in the current file. 2. Use :r !command to read in the output of command starting at the line under the current one. Put a line number in front of the r to read in the output of command starting at the line under the one specified. Example: :25r !date will read in the current date and time onto line 26. 3. Use :w outfile to write the contents of the current file to a file named outfile. Put a line number range in front of the w to write only those lines to outfile. Example: :1,10 w outfile writes lines 1 through 10 inclusive from the current file into outfile. 4. Use :1,10 w >> outfile to append lines 1 through 10 inclusive from the current file to outfile nondestructively.
Advanced Vi Invocation
1. Use vi +/foo when invoking Vi from the command prompt to have it automatically move the cursor to the first occurrence of the string foo in the first file being edited. 2. Use vi +24 myfile.txt when invoking Vi from the command prompt to have it start with the cursor on line 24 of myfile.txt. Replace the line number and filename with whatever is appropriate. 3. Use view myfile.txt to start Vi in read-only mode on the file myfile.txt. On systems which do not support symbolic links, such as MS-DOS, try vi -R myfile.txt instead. 4. NOTE: It is sometimes necessary to invoke clones of Vi by their appropriate program name, such as vim under MS-DOS.