=============================================================================== = VIM Activity for CS400 @ UW Madison (Spring 2023) = = - based on v1.7 of the VimTutor - = =============================================================================== This activity is designed to walk you through some of the commands you will need to use Vim as an editor for the activities in this class and for editing Java code. The activity is split up into 4 parts. You can directly edit this file for each of the parts. Before you start, make sure that your Caps-Lock key is NOT active and press the j key enough times to move the cursor so that part 1.1 completely fills the screen. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 1.1: MOVING THE CURSOR ** To move the cursor, press the h,j,k,l keys as indicated. ** ^ k Hint: The h key is at the left and moves left. < h l > The l key is at the right and moves right. j The j key looks like a down arrow. v 1. Move the cursor around the screen until you are comfortable. 2. Hold down the down key (j) until the screen scrolls down. Now you know how to move to the next part. 3. Using the down key, move to part 1.2. NOTE: If you are ever unsure about something you typed, press to place you in Normal mode. Then retype the command you wanted. NOTE: The cursor keys also work for moving around. Often, Vim users prefer the hjkl because they make moving the cursor faster once you get used to them. For this class you can use the ones you prefer. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 1.2: EXITING VIM NOTE: Before executing any of the steps below, read this entire part. 1. Press the key (to make sure you are in Normal mode). 2. Type: :q! . This exits the editor, DISCARDING any changes you have made. 3. Get back here by executing the command that got you into this tutor. For the CS400 activity this is: vim vimTutorial.txt 4. If you have these steps memorized and are confident, execute steps 1 through 3 to exit and re-enter the editor. NOTE: :q! discards any changes you made. You will learn how to save changes to a file later in this tutorial. 5. Move the cursor down to part 1.3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 1.3: TEXT EDITING - DELETION ** Press x to delete the character under the cursor. ** 1. Move the cursor to the line below marked --->. 2. To fix the errors and make the first line the same as the second, move the cursor until it is on top of the character to be deleted. 3. Press the x key to delete the unwanted character. 4. Repeat steps 2 through 4 until the sentence is correct. ---> The ccow jumpedd ovverr thhe mooon. ---> The cow jumped over the moon. 5. Now that the line is correct, go on to part 1.4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 1.4: TEXT EDITING - INSERTION ** Press i to insert text. ** 1. Move the cursor to the first line below marked --->. 2. To make the first line the same as the second, move the cursor on top of the character BEFORE which the text is to be inserted. 3. Press i and type in the necessary additions. 4. As each error is fixed press to return to Normal mode. Repeat steps 2 through 4 to correct the sentence. ---> There is text misng this . ---> There is some text missing from this line. 5. When you are comfortable inserting text move to part 1.5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 1.5: EDITING A FILE ** Use :wq to save a file and exit. ** NOTE: Before executing any of the steps below, read this entire part. 1. Press the key (to make sure you are in Normal mode). 2. Type: :wq! . This exits the editor, STORING any changes you have made in the open file (vimTutorial.txt). 3. Get back here by executing the command that got you into this tutor. For the CS400 activity this is: vim vimTutorial.txt 4. If you have these steps memorized and are confident, execute steps 1 through 3 to exit and re-enter the editor. 5. Move back up in the file and check the changes that you have made previously. All changes should still be there. 6. After reading the above steps and understanding them: do it. 7. Now try typing :w this will only save the file, without exiting the editor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 1 SUMMARY 1. The cursor is moved using either the arrow keys or the hjkl keys. h (left) j (down) k (up) l (right) 2. To start Vim from the shell prompt type: vim FILENAME 3. To exit Vim type: :q! to trash all changes. OR type: :wq! to save the changes. 4. To save all changes in a file, type: :w 5. To delete the character at the cursor type: x 6. To insert before the cursor type: i type inserted text NOTE: Pressing will place you in Normal mode or will cancel an unwanted and partially completed command. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 2.1: DELETION COMMANDS ** Type dw to delete a word. ** 1. Press to make sure you are in Normal mode. 2. Move the cursor to the line below marked --->. 3. Move the cursor to the beginning of a word that needs to be deleted. 4. Type dw to make the word disappear. NOTE: The letter d will appear on the last line of the screen as you type it. Vim is waiting for you to type w . If you typed something wrong you can press and start over. ---> There are a some words fun that don't belong paper in this sentence. 5. Repeat steps 3 and 4 until the sentence is correct and go to part 2.2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 2.2: DELETE LINES ** Type dd to delete a whole line. ** 1. Move the cursor to the second line in the phrase below. 2. Type dd to delete the line. 3. Now move to the fourth line. 4. Type dd to delete the line. ---> 1) public static void main(String[] args) { ---> 2) This is not Java code. ---> 3) System.out.println("Hallo CS 400!"); ---> 4) This is not Java code either. ---> 5) } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 2.3: THE UNDO COMMAND ** Press u to undo the last commands, U to fix a whole line. ** 1. Move the cursor to the line below marked ---> and place it on the first error. 2. Type x to delete the first unwanted character. 3. Now type u to undo the last command executed. 4. This time fix all the errors on the line using the x command. 5. Now type a capital U to return the line to its original state. 6. Now type u to undo the U command. ---> Fiix the errors oon thhis line and reeplace them witth undo. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 2.4: THE PUT COMMAND ** Type p to put previously deleted text after the cursor. ** 1. Move the cursor to the first line below marked --->. 2. Type dd to delete the line and store it in a Vim register. 3. Move the cursor to the 1) line, ABOVE where the deleted line should go. 4. Type p to put the line below the cursor. ---> 2) System.out.println("Hallo CS 400!"); ---> 1) public static void main(String[] args) { ---> 3) } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 2.5: COPY AND PASTE TEXT ** Use the y operator to copy text and p to paste it ** 1. Move to the line below marked ---> and place the cursor after "a)". 2. Start Visual mode with v and move the cursor to just before "first". 3. Type y to yank (copy) the highlighted text. 4. Move the cursor to the end of the next line. 5. Type p to put (paste) the text. Then switch to insert mode and then type: second. 6. Use Visual mode to select " item.", yank it with y , move to the end of the next line with j$ and put the text there with p . ---> a) this is the first item. b) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 2 SUMMARY 1. To delete from the cursor up to the next word type: dw 2. To delete a whole line type: dd 3. To undo previous actions, type: u (lowercase u) To undo all the changes on a line, type: U (capital U) 4. To put back text that has just been deleted, type p . This puts the deleted text AFTER the cursor (if a line was deleted it will go on the line below the cursor). 5. You can select text with v (Visual mode) and copy (yank) selected text with y. To paste yanked text back in, you can also use p . ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 3.1: CURSOR LOCATION AND FILE STATUS ** Type CTRL-G to show your location in the file and the file status. Type G to move to a line in the file. ** NOTE: Read this entire part before executing any of the steps. 1. Hold down the Ctrl key and press g . We call this CTRL-G. A message will appear at the bottom of the page with the filename and the position in the file. You can see the line number on the right side of the screen in the bottom line (the first number before the comma). Remember the line number for step 3. 2. Press G (SHIFT-G) to move you to the bottom of the file. Type gg to move you to the start of the file. 3. Type the number of the line you were on and then G (SHIFT-G). This will return you to the line you were on when you first pressed CTRL-G. 4. If you feel confident to do this, execute steps 1 through 3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 3.2: THE SEARCH COMMAND ** Type / followed by a phrase to search for the phrase. ** 1. In Normal mode type the / character. Notice that it and the cursor appear at the bottom of the screen as with the : command. 2. Now type 'errroor' . This is the word you want to search for. 3. To search for the same phrase again, simply type n . To search for the same phrase in the opposite direction, type N . ---> "errroor" is not the way to spell error; errroor is an error. NOTE: When the search reaches the end of the file it will continue at the start. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 3.3: MATCHING PARENTHESES SEARCH ** Type % to find a matching ),], or } . ** 1. Place the cursor on any (, [, or { in the line below marked --->. 2. Now type the % character. 3. The cursor will move to the matching parenthesis or bracket. 4. Type % to move the cursor to the other matching bracket. 5. Move the cursor to another (,),[,],{ or } and see what % does. ---> This ( is a test line with ('s, ['s ] and {'s } in it. )) NOTE: This is very useful in debugging a program with unmatched parentheses! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 3.4: THE SUBSTITUTE COMMAND ** Type :s/old/new/g to substitute 'new' for 'old'. ** 1. Move the cursor to the line below marked --->. 2. Type :s/thee/the . Note that this command only changes the first occurrence of "thee" in the line. 3. Now type :s/thee/the/g . Adding the g flag means to substitute globally in the line, change all occurrences of "thee" in the line. ---> thee best time to see thee flowers is in thee spring. 4. To change every occurrence in the whole file type: :%s/old/new/g ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 3 SUMMARY 1. CTRL-G displays your location in the file and the file status. G moves to the end of the file. number G moves to that line number. gg moves to the first line. 2. Typing / followed by a phrase searches FORWARD for the phrase. After a search type n to find the next occurrence in the same direction or N to search in the opposite direction. 3. Typing % while the cursor is on a (,),[,],{, or } goes to its match. 4. To substitute new for the first old in a line type :s/old/new To substitute new for all 'old's on a line type :s/old/new/g To substitute all occurrences in the file type :%s/old/new/g ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Part 4: THE HELP SYSTEM Vim has a comprehensive on-line help system. To get started, type: :help Read the text in the help window to find out how the help works. Type CTRL-W CTRL-W to jump from one window to another. Be careful when using the combination CTRL-W. On some systems, your browser will interpret this as a command to close your window (and will close the window with your VM). A way around this is the small keyboard symbol on the top right of your VM window. Click on it and select CTRL-W as a key combination to Vim running in the VM without closing your browser window. Type :q to close the help window. You can find help on just about any subject, by giving an argument to the ":help" command. Try these (don't forget pressing ): :help dd :help y ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~