Intro to Operating Systems

Presented by Instructor: Greg Scott Phone: (310)660-3203 email: [email protected] Class Topics

Advanced editor 1. Copying and Moving text 2. The Numbered Buffers 3. Editing two files once 4. Transferring text with Letter Buffers 5. Searching and Replacing with Regular Expressions Advanced vi Commands Copying and moving text-Commands can be combined to achieve the , copy, and paste features of word processors: • To cut text, use a single x or d to delete the characters, words, or lines you want cut. These commands remove the text you have indicated and place it in a temporary buffer. • To copy text into this same buffer, use the y (yank) command, is followed by a single cursor movement command. It copies to the buffer from the current cursor position to wherever the cursor movement command indicates. If the cursor movement command is omitted and y is simply repeated, it copies the entire line to the buffer. Advanced vi Commands Examples of yanks: – yw copies a word to the buffer; 5yw or y5w copies five words – yy or Y copies an entire line to the buffer; 7yy or y7y copies seven lines – y$ copies to the buffer from the cursor to the end of the line – y0 or y^ copies to the buffer from the cursor to the beginning of the line To paste text (i.e., copy from the buffer), use the p (put) command: – p (or P) pastes the text from the temporary buffer below(above) the current line or before(or after) the cursor, depending on whether the buffer contains a partial line or not. Advanced vi Commands These commands undo previous edits: – u cancels your most recent edit – U restores the current line to its previous state Exercise: Copy the oldlady.txt. Use yanks and puts to copy and reorder the four lines so that a familiar poem is created below the original lines. Save the result as oldlady2.txt. The Numbered Buffers-The contents of vi’s cut/paste buffer lasts only until the next editing command’s text replaces it. However, there are 9 numbered buffers (1-9) which keep track of the chronological sequence of edited text, with 1 holding the most recent edit, 2 holding the second most recent, etc. Advanced vi Commands

• To paste from the numbered buffers, use a double quote followed by the number and the letter p(or P). Example: “3p or “3P (do not use a closing quote) Exercise: Copy the file oldman.txt. Move to the last line and use the command nine consecutive times to delete the last nine lines in reverse order. Now paste the nine lines using buffers 1-9 under the appropriate line to create a familiar children’s song. Advanced vi Commands

• Editing two files at once-The e command (entered at the last line (:) prompt) allows a new edit session to be opened in the foreground while keeping the previous session open in the background. Example- :e file2 You can switch between the current and previous sessions by using a # with the e command: Example- :e # Note that you must save before using these commands, or use e! in each case. Advanced vi Commands

• Transferring text between files – There are 26 additional cut/paste buffers called the letter buffers. Using the letter buffers (a–z) you can tranfer text across edit sessions. – The commands are similar to using to numbered buffers, requiring a quote and a letter preceding the delete or copy command followed by a quote and letter preceding the paste command. Example: “a5yy (yanks five lines into the buffer a) “ap or “aP (paste five lines from buffer a) – In addition, the letter buffers can append additional text to the current contents by using the capital letters(A-Z) Example: “A5yy (appends five lines to buffer a) Advanced vi Commands

Exercise: Copy and edit the file cliches.txt. Yank all the lines that begin with an “A” into the “a” buffer, then edit a second file named alines.txt. Paste the lines into the file, then save without exiting. Return to cliches.txt. Yank all lines that begin with a “B” into the “b” buffer, then edit a second file named blines.txt. Paste the lines into the file, then save and . Advanced vi Commands • Searching-You can search forward or backward for patterns of characters within your file. A pattern uses regular expressions, and can include spaces. – / pattern searches forward in the file for the specified pattern of characters from the current position. – ? pattern searches backward from the current position. When you the slash (/) or question (?) characters, the cursor moves to the bottom line of your terminal. In each case, the search will wrap around to the opposite end of the file and continue.. – n repeats the last search in the same direction. – N repeats the last search in the opposite direction. Advanced vi Commands • Regular Expressions are patterns that include special characters to its meaning. – [ab] represents a choice of either a or b. For example, [Bb]oy matches Boy or boy. – . represents any character. For example, b.t matches bat, bet, bit, or b t. (that’s b space t) – * represents zero or occurrences of the preceding character. For example, micha*el matches michael or michel. – ^ represents the beginning of a line. For example, ^Hello only matches Hello at the beginning of the line. – $ represents the end of a line. For example, goodbye$ only matches goodbye at the end of the line. Advanced vi Commands

– \< represents the beginning of a word. For example, \ represents the end of a word. For example, ich\> matches stich and which, but not riches – \ represents text as a complete word. Exercise: Edit the file cliches.txt. Use searches to all ocurrences of the word “the” and the word “you”. Allow matches only for whole words and for any combinations of case of letters. You should 11 of the first and 5 of the second. Also, search for “today.” at the end of a line, and the word “Be” at the beginning of a line, finding 1 of each of these. Advanced vi Commands

• Search and Replace – From the colon (:) prompt, the substitute (s) command (see command, class 03) can be issued to search for a pattern and replace it. The action affects only the current line, unless a line range is specified. The form of this command is: :f,l s/pattern/newtext/gc where f=first line, l=last line g=global for each line c=confirmation required Other special characters: $ = file’s last line, %=all lines Examples: :s/John/James/ (replace 1st John with James on the line) :6,$s/John/James/ (replace 1st John with James starting on line 6 to the end of the file)