Question

In: Computer Science

The vi text editor can be a bit challenging to use at first, but once you...

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.   

Solutions

Expert Solution

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


Related Solutions

Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
you can use either vim or nano as text editor Implement the following code in ARM...
you can use either vim or nano as text editor Implement the following code in ARM on Raspberry Pi, compile and run. g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note: answer should be calculated not hardcoded
Unix / Linux 6. vi editor: In vi command mode, what are the keystrokes you would...
Unix / Linux 6. vi editor: In vi command mode, what are the keystrokes you would type to... Quit the vi editor and save any changes? 7. C language source files, by convention, have the suffix ".c". The associated "header" files have the suffix ".h". What is the command to perform the following task: List all the C source and C header files in the current directory. [Hidden files may remain hidden.] 8. C language source files, by convention, have...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Use your favorite text editor or IDE to search for occurrences of setState. Where you found...
Use your favorite text editor or IDE to search for occurrences of setState. Where you found uses of setState, refactor the code to use JavaScript functions instead of classes and the useState hook instead of setState. import React from 'react' import ColorSlider from './ColorSlider' import ColorOutput from './ColorOutput' import styles from './ColorBrowser.module.css' class ColorBrowser extends React.Component { constructor(props) { super(props) this.state = { red: Math.floor(Math.random() * 256), green: Math.floor(Math.random() * 256), blue: Math.floor(Math.random() * 256) } } updateColor(e) { this.setState({...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt)...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Use the Table button in the Rich-Text Editor to provide an adjacency matrix for a simple...
Use the Table button in the Rich-Text Editor to provide an adjacency matrix for a simple graph that meets the following requirements: 1) has 5 vertices 2) is maximal planar
Please do the following in JAVA. Deposit and Withdrawal Files Use Notepad or another text editor...
Please do the following in JAVA. Deposit and Withdrawal Files Use Notepad or another text editor to create a text file named Deposits.txt. The file should contain the following numbers, one per line: 100.00 124.00 78.92 37.55 Next, create a text file named Withdrawals.txt. The file should contain the following numbers, one per line: 29.88 110.00 27.52 50.00 12.90 The numbers in the Deposits.txt file are the amounts of deposits that were made to a savings account during the month,...
The application of lexical analysis techniques in text editor You should cover: 1) What is the...
The application of lexical analysis techniques in text editor You should cover: 1) What is the problem? 2) What is the compiler construction techniques used to solve the problem 3) How to solve the problem using the compiling techniques.
Respond in a single Word doc (or comparable text editor). Henrietta’s Pine Bakery Background You are...
Respond in a single Word doc (or comparable text editor). Henrietta’s Pine Bakery Background You are an Analyst for the professional service firm, FINACC LLP. Your firm specializes in providing a wide variety of internal business solutions for different clients. Given the outstanding feedback you received on your first engagement working for Big Spenders Inc., a Senior Manager in the Financial Advisory group requested your support on a compilation engagement. Additional Information Henrietta’s was established in 1963 when it first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT