#Written by Jen Chen #Copyrighted by Jen Chen #CS 122: #Lesson 1b #Spring 2005 (Updated on Thursday 05/12/05) VI manipulation: - Note: while we are in VI, we can navigate around by using the arrow-keys in the accountant key pad to move left, right, up or down. - We can delete a character by positioning the cursor on top of that character then press the Delete-key (to the right of the Backspace-key). - To avoid typing long sql-query, we issue the "edit" command at the mysql prompt to bring up the VI-editor. We then edit the previous query (or any saved query) and modify its contents to fit our new query. This will save us quite a bit of times and we can also save our new query into the desired directory. Think of this as a way to save all of the important queries that you type at the mysql prompt. Also think of this as a way to save your lecture notes. Note: To put a comment(s) in your query while you are in VI, precede the comments with a pound (#) sign. If your comments span few lines long, then precede each line with a pound (#) sign. Ex: #This is a select statement. select * from temp - While you are in the VI-editor, then to save your current contents, you press the Esc-key, then press the :-key (COLON key), follow by the W-key. To save the contents of the current Vi editor to a new file (let's say called "tmp.sql"), first press the Esc-key, then a colon (:), follow by pressing the W-key, then a space, then the name of the file that you would like to save to. If you do not enter the path of the file, then your file is saved to the current directory. To edit any saved file in VI (let's say tmp.sql) while in VI, first remove all contents in the current VI, then press the Esc-key, follow by a colon (:), then the r-key, then a space, and finally type the name of the file that you want to edit, then press ENTER. If the file that you want to edit exists then you will see the contents of the file in your VI editor. Edit the file as you like. - To create a blank line BELOW the current line (wherever your cursor is) in VI, press the o-key (the lower case letter o, which stands for OPEN BELOW). - To create a blank line ABOVE the current line (wherever your cursor is) in VI, press the O-key (the upper case letter O, which stands for OPEN ABOVE). - To delete a line in VI, press the D-key (for delete) TWICE. - To delete 2 (or more) lines, first place the cursor to the beginning of the line that you want to delete then press the 2-key, follow by pressing the D-key (for delete) TWICE. OR: press the Esc-key, then type the beginning line number that you want to delete (let's say you want to delete lines number 6 up to line number 9), it is 6 in this case, then a comma, then the number of the line that you want to delete up to, in this case it is 9, follow by the letter d, then press ENTER. (at the bottom of the VI editor you should see :6,9d ) - To undo any mistake in VI, press the Esc-key then the U-key (for UNDO). - To copy a line in VI, first position your cursor anywhere in the line that you want to copy then press yy (that is press the y-key TWICE, by the way the letter y stands for 'yank". We yank the contents of the current line to the buffer), then place the cursor ABOVE the line that you want to copy to, then press the letter p (for paste). The contents of the line to be copied will appear BELOW the cursor. (To paste the contents of the line ABOVE the current line press the upper letter P-key: ie press SHIFT+P) Similarly to copy 2 (or more lines) in VI, first position your cursor anywhere in the line that you want to copy then press 2yy (that is press the 2-key, then press the y-key TWICE), then place the cursor ABOVE the line that you want to copy to, then press the letter p (for paste). - To delete a character in VI, first move the cursor to where you want to delete, then press the DELETE key; or press the X-key. - To insert a character(s) in VI, first move the cursor to where you want to insert, then press the Esc-key, follow by the character(s) that you want to insert. Press the Esc-key ONCE after you are done with your typing, follow a pressing the W-key to save the contents of your VI buffer. - To quit your VI, press the Esc-key, follow by the COLON (:), then press the Q-key. If your VI does not quit, then repeat the same process but add the exclamation mark AFTER you press the Q-key (ie: press !, then press the Q-key, follow by the !-key) ==============================================================================================