**Vi Editing Exercises** Goals ==== * Practicing using vi editor ## Notes: * Remember The vi editor uses "modes" * The easiest thing to do if you get confused in vi is to press the ESCape key a couple of times and start over. With what you were doing. Exercises ===== Log in as the ubuntu user using ssh to your machine. ~~~ username: ubuntu password: {given in class} ~~~ ~~~ ssh ubuntu@100.64.0.N ~~~ ## Create a file using vi editor and editing At the command prompt type the following command, NOTE: "cd" command without a parameter returns you to your home directory. ~~~ $ cd $ vi temp.txt ~~~ vi will create the file “temp.txt” for you. ~~~ Press the "i" key to switch to input mode. Type something like, "VI is great! I think I'll be using vi from now on instead of Word” Press to add lines. Type some more text ~~~ Save the file that you are in. To do this do: ~~~ Press the ESCape key for command mode Type “:wq” to save and quit the file (notice the “:” before the “wq”!). ~~~ Copy a large file to your home directory so that you can play around with some more vi commands. We'll copy over your /etc/defaults/rc.conf file for this exercise. To do this do: ~~~ $ cd $ cp /etc/rsyslog.conf rsyslog.conf.bak ~~~ ## Navigation, copy/paste and editing in vi Edit the file, but let's start at the bottom of the file: NOTE: Opening a file with "+" open the file and places the cusor at the end of the file. Handy for long config files. ~~~ $ vi + rsyslog.conf.bak ~~~ Go to the first line of the file. Notice the colon (“:”) before the “1”. ~~~ :1 ~~~ Go to line 10, add a new line, and add in some text: ~~~ :10 Press the “o” key ~~~ Add the following text: ~~~ ## ## A sample comment ## ~~~ Delete the three lines you just created: ~~~ Move to the first line of new text Press the ESCape key Press “dd” to delete a line, repeat until the text is gone ~~~ Save the file, but don’t exit. ~~~ :w press ~~~ Practice copying and pasting text. Go to line 12, copy 3 lines of text, go to the bottom of the file, place the text there: ~~~ ESC (go to command mode) :12 (go to line 12 of the file) 3yy (“yank” 3 lines of text and place in copy buffer) G (go to the end of the file) p (place the contents of the copy buffer here) ~~~ If want to undo this you would type (in command mode): ~~~ u ~~~ Go to the top of the file, replace all occurrences of “514” with “1024”, but prompt for each change: ~~~ ESC :1 :%s/514/1024/gc ~~~ Say “yes” or “no” to a few prompts then escape from this mode by pressing ctrl-c and {ENTER}. Go to line 1, search for “kernel”, move to the end of the line, add some text: ~~~ ESC :1 /kernel SHIFT-A “text here” ESC ~~~ Now let’s exit from the file and not save the few changes we’ve made. ~~~ :q! ~~~ Why did we add "!"? ## PRACTICE, PRACTICE, PRACTICE As you should be able to see vi is _extremely_ powerful as an editor, but not necessarily intuitive. The best way to get good at using vi is to practice. Make sure you are logged in as _ubuntu_, then do the following: ~~~ $ cd $ vi rsysctl.conf.bak ~~~ In this file practice some of the following items: ~~~ Moving around: By word (w or b) End of line (A) Start of line Top of file (1G) Bottom of file (G) To an absolute line number (:n) Copying and pasting multiple lines (use vi commands, nyy) Copying and pasting single lines (use vi commands, yy & P or p) Copying and pasting multiple lines (use your mouse buffer) Copying and pasting single lines (use your mouse buffer) Search for items backwards and forwards (?string or /string) Replacing text (:s/pattern /string /flags) ~~~ And, anything else you wish to practice. ## REPEAT the above exercises USING another editor. Such as nano, joe, ee or any of your vaforite editor. NOTE: if you dont have the editor installed, you can install by: ~~~ $ sudo apt-get update $ sudo apt-get install {package-name} ~~~