In: Computer Science
The vi text editor can be a bit challenging to use at first, but once you have familiarized yourself with how it works you will find it an efficient way to create and edit text files in Linux, and UNIX as well. The key thing to remember is that vi has two operational modes: insert mode and command mode. In insert mode, everything you type is entered into your file. You can use the backspace, delete and the arrow keys. You must press Enter at the end of each line. In command mode, everything you type is processed as a command. Most commands are single-letter commands, and some will accept a number before the command to indicate how many times you wish the command to be performed. You can use also specialized cursor movement commands (forward a page, back a page, go to a specific line, etc.) while in command mode. Creating and Saving a File 1. In this part of the lab, you will copy out one of William Shakespeare’s sonnets (fourteen-line poems). To find which one of his poems you are to type in, go to https://www.opensourceshakespeare.org/views/sonnets/sonnets.php Take the last two digits of your student number, and click on the sonnet number that matches that part of your student number. For example, if your student number is 100012345, choose sonnet 45. 2. Log into . 3. To create a new file in Linux, you type vi followed by the new file’s name. Type vi sonnet 4. You will see an empty text file. The ~ characters on the left indicate empty (non-existent) lines of text. When you start vi you are in command mode, so you cannot enter text yet. To get into regular insert mode, press the i key and type in your sonnet (do not include the title or the line numbers at the ends of lines 5, 10 and 14, but do include the spaces at the start of lines 13 and 14). Remember to press Enter at the end of each line. 5. To save the file, you must enter command mode. Press Esc to do so. You can always press Esc at any time to enter command mode. To save the file and exit vi, press ZZ (that is, two capital Z’s). 6. You should now be back at the Linux prompt. Use cat sonnet to verify that you have saved your file and that it contains the correct text. Obtain a screenshot. Editing a Text File 7. Use vi sonnet to open your text file again. Notice that you again start off in command mode, which you can check by using cursor movement keys. Write down a brief description of what each of the following nine keys does. You may have to move the cursor into the centre of your poem before using the keys, to make the purpose of each character more obvious. a. Each of the letters h, j, k, l. b. The letters b, w. c. The symbols $, ^. d. The control code ^g, and a number followed immediately by G. 8. You will now add a line to the end of the document. Remember that you are still in command mode. You can enter insert mode in a number of ways. When you want to add text below an existing line of text, use the o command (think “o” for “open”). Move the cursor down to the last line of the document, then press o. Then type the following text: This is the end of the file. Press Esc to get back into command mode. 9. You will now add a line to the top of the document. When you want to add text above an existing line of text, use the O (capital O, not zero) command. Move the cursor to the top of the document and press O. Type the following: My own sonnet and press Enter. Press Esc to get back into command mode. 10. You will now insert lines in the middle of the document. To do so, move the cursor to the position in your document where you want to insert a line. Press o or O depending on whether you want to add the line below or above your cursor position. Use this information to add a row of about ten dashes (the exact number isn’t important) just above the line reading This is the end of the file. Get back into command mode. 11. Copying and pasting a line of text is done using the “yank” and “put” commands in vi. What you will do next is to copy the row of dashes that you just added, and paste it underneath the title line at the top of the document. In command mode, move the cursor to the line of dashes. Press yy to copy the entire line to the buffer. Now, move the cursor to the top line of your file, which should be the title line. There are two ways to paste the contents of the buffer: p pastes the contents below the current cursor position, and P pastes the contents above the current position. Since we would like the dashes to go below the title line, press lowercase p. Obtain a screenshot. 12. At this point, you can save the file and exit vi as you did earlier (by pressing ZZ), but you can also simply save the file and carry on editing. To save the file, make sure you’re in command mode (using Esc), and type :w (colon and w) to write the file to disk. 13. As well as knowing how to enter new lines of text, you will also need to know how to insert text at the start, in the middle of a line, or at the end of a line. a. To add text to the start of a line, move the cursor to the beginning of the line, press i to enter insert mode, type the text, and press Esc to exit insert mode. Use this technique to add the text Lab 3: to the beginning of the first line of the file. b. To add text in the middle of a line, move the cursor to the spot where you want your new text to start, press i, type the text, and then exit insert mode. Use this technique to insert ***** (five asterisks) in the middle of the fifth line of the sonnet. c. To add text at the end of a line, move the cursor to any position in the line and press A (think “after”). Type the text, then press Esc. Use this technique to add The end! at the end of the last line of the very last line of the file. Obtain a screenshot. 14. There are several ways to delete text while in command mode. a. Use x to delete the character under the cursor. b. Use dw to delete from the cursor position to the end of the current word. c. Use dd to delete the current line. Note that methods (b) and (c) also copy the deleted text to the buffer, so you can paste what you have deleted to another part of your file. d. Using the above information plus what you learned earlier, delete the lines of dashes and each line with a blank line instead. Delete the word own from the first line. Replace the word file with poem. e. Sometimes it is useful to be able to delete the entire last part of a line of text, or to replace the last part of a line with new text. You would use D to delete everything from the cursor position to the end of the line; you would use R to enter replace mode, where everything you type overwrites the current text on the line. Make sure you’re in command mode, and move the cursor to the T in The end! Press R, and type the text Wasn't it great? to replace the previous text. Then press Esc to enter command mode. 15. As you can see, files in vi consist of individual lines. Unlike in the Windows programs Word or Notepad, you must press Enter at the end of each line rather than just continue typing. Sometimes you may find that after you created two separate lines of text, that you would now like to join them together as one line of text. a. Go to the last line of your document. Press o to add text to the file. b. Type the following four words, making sure to put each on a separate line: These are four words. c. In command mode, move the cursor to the line reading These. Press J (for “join”), and notice that the word are moves up and becomes part of the current line. Press J twice more to complete the sentence. 16. To search for text, you use the / command. In command mode, type /a to find the next occurrence of the letter a in the file. Once you have found the first occurrence, you can press n or N to find the next or previous occurrence of the same text. Press n repeatedly to cycle through all the occurrences of a in the file. 17. Searching and replacing is a bit more complicated. a. To search for text and replace it with new text, use :s/old/new/ while in command mode. For example (but don’t do it yet), to replace the next occurrence of the with THE, you would type :s/the/THE/ while in command mode. b. If you wish to replace all occurrences of a text string throughout the entire file, you use :%s/old/new/ in command mode. You would do the following to capitalize occurrences of a in the file: :%s/a/A/ Here, only the first a in each line is capitalized. If you wanted to change every a on each line, you would have to put the code g (for “global”) after the last / in the command, as in: :%s/a/A/g c. If you ever do an edit which you don’t like you can undo the change. Simply press u to undo the last command. If you press u repeatedly now, you will undo the last number of commands. To redo the commands, press Ctrl-r. d. Obtain a screenshot showing the file with all a’s changed to capital A’s. e. Save the file and exit vi. Create and Modify a Text File Now you will get to enter a text file and make changes as directed. Follow the steps, using what you have learned in the earlier part of this lab. 1. Using vi, create a new file called lab3. Enter the text that you see below, keeping the line breaks as shown. This is a test to see what you have learned. Here are some lines of text to enter. When you have entered them all, you will do a few editing commands. I hope that you have learned well. 2. Obtain a screenshot, then save the file. 3. Change the words a test to read an exercise. 4. Add the title My Awesome File by as the top line of the document. Put your name at the end of the line. 5. Edit the document so that each full sentence (ending with a “.” period) is on its own individual line of text. 6. Delete the word editing. 7. Put a blank line in between the title and the first sentence. 8. Change all the lowercase e’s in the document to capital E’s. 9. Use cut and paste to change the order of the sentences in the document, so that they appear in alphabetical order “Here are…”, “I hope…”, “This is…”, and “When you…”. 10. At the end of the document, add a line of dashes and a line that reads AAAKKK and your section number. 11. Obtain a screenshot, then save the file and exit vi.
1st Screenshot
a)
l: Move cursor to left
h: Move cursor to tright
j: Move cursor downwards
k: Move cursor upwards
--------------------------------------
b)
w: Move cursor to next word
b: Move cursor to previous word
--------------------------------------
c)
$: Move cursor to end of current line
^: Move cursor to beginning of line
--------------------------------------
d)
Shifg + g: Move cursor to the beginning of last line in file
Shifg + g + number: Move cursor to the beginning of line ‘n’ from end of line(‘n’ is the number entered)
--------------------------------------
2nd Screenshot
--------------------------------------------------------
3rd Screenshot
-------------------------------
4th Screenshot