Lab 1 - Unix Lab#1 - vi editor [CE Page 1 of 12 [m LAB #1...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unix Lab#1 - vi editor [CE Page 1 of 12 [m LAB #1 CS 2351 UNIX Operating System Name 1’ Section 4! 0 Z The purpose of this lesson is to learn how to use the Vi editor. The emphasis will be on learning to use many features of the command and insert modes. (NOTE: There are a few summary pages that contain the vi commands you need that you can print off to assist you after you have done this lab. They are located at: http;//www.cs.okstate,edu/newuser/Vi guidehtml. At this time, the Computer Science Department is using a Sun Microsystems SunFire V880 computer. It uses Solaris as its UNIX operating system. The Solaris 9 system is based on the AT&T UNIX System V version of UNIX. Some UNIX machines use different versions of UNIX and not all of the commands may work on other machines. This lab exercise and others for the UNIX class will have exercises interspersed with reading material. Generally, the statement "Issue the command." or "Type in:" is used to indicate that you should type in the command to determine the UNIX response. The commands that you should type in are in BOLD. You should write your answer to a question in the underlined spaces. These responses are graded and determine your score for the lab assignment. In order to type in a file in UNIX, you must learn how to use an editor. You will learn how to use the vi editor in this lab. You will create a file, insert some text, make corrections and save the file. The vi editor is not as easy as a word processor and takes some time to learn. When you first logon on to your account, you will see a $ (dollar prompt) on the left hand side of your screen. Some problems you may encounter when you first access your Computer Science account. The backspace key will not work on your account. However, the delete key will work at this time. To fix this, at the $ prompt type in: stty erase ‘h Then press the ENTER key. NOTE: You will have to do this each time you login to your account. However, you can fix this by getting a special file into your account that has this information already in it. You will not have to retype in the "stty erase "h" each time you get on the machine. To fix this problem, copy a file to your account. Type in the following: cp /pub/htdocs/newuser/.profile .profile There is a space after the command op. The only other space is between the two .profile names. This is a spemal file that Will help customize your account to your needs. If you are taking the UNIX class, you http://www.cs.okstate.edu/~csZ35 l/unixOl .html 1/13/2011 Unix Lab#l - vi editor Page 2 of 12 will learn more about these commands later in other labs. After you have copied this file to your account, you are ready to start learning the Vi editor. At the $ prompt, issue the command: vi practice What was printed on the screen? WEE? I’Vgefé’. a! J _ x”; A; . r/ in» 1, When you first get into the Vi editor, all you will see is a screen full of tilde marks ( ~) down the left side of your screen . At the left bottom, you will have the name of the file -- in this case, "practice" [New file]. You will be typing in information on the lines that have the tildes (~). The Vi editor has two different functional modes. They are: (1) Insert or append mode. When yOu are in the insert or append mode, you are typing in information into the file. This is where you input information into a file. You will also see the change, replace, and open modes used here. These are other ways to type in information into a file. (2) Command mode. This is where you want to make changes in the file that you have been typing. The vi editor is a full screen editor. You can go over the entire document to make changes. You will learn how to use these two functions in this lab. Before you type in the file practice, issue the following command: : set showmode or you can abbreviate the command by typing: :set smd One problem with the vi editor is that you usually cannot tell if you are in the insert mode or in the command mode. The command, ":set showmode", will put a message at the bottom right hand corner of your screen when you are in either the insert or append mode. Later, you will also see the replace, change, and open modes used. NOTE: This feature does not work on all versions of UNIX. If there is no message at the bottom of the screen, you are in the command mode. At this time, you should not have a message at the bottom of your screen. In order to see what the message looks like, enter the insert mode by typing the Vi command: i You will not see the "i", on your screen, but you should see a message on the right bottom corner of your screen. What is thcynessage that you see on the bottom of your screen? _. ._ 1&5}; M a... V iii All The insert mode allows you to enter text into the file you are creating. In order to make corrections after you have typed in the file, you must get out of the insert mode. You do this by pressing the escape key. You Will see places in the labs where you are to use the <esc> key. This is the abbreviation for the escape key. When you finish with the insert mode, you will then press the <esc> key and you will no http ://www.cs.okstate.edu/~cs23 5 l/uniXOl .html 1/13/2011 Unix Lab#1 — Vi editor Page 3 of 12 longer have the message :"insert mode" at the bottom of your screen. You will then know that you are in the command mode and can make changes to your file. In order to see how this works, press the <esc> key now. What happens’to the message on the bottom of the screen? I ’f a f)! 571 {J} flu"; / J 7 » Issue the following command again: i You should be back in the insert mode again. The message "lnsert Mode" should appear at the bottom of your screen again. The other symbol that you will see frequently is the <cr> symbol. It stands for "carriage return". Any time you see this, you should press the Enter key. DO NOT TYPE IN THE <cr> CHARACTERS. Typingin information using the vieditor You will type in the indented lines below starting with "The The Vi editor..." up to but not including "<esc>". This is text to be entered into the file whose filename is practice. Enter the text exactly as it appears, typos included. Type in the paragraph as you see it. There are errors in the paragraph but these will be fixed later as you learn how to use the Vi editor. Do notpre‘ssfithe Enter key until the Scr? symbol, appears in the text. If you make a mistake and you are on that particular word, you can press the "backspace" key to fix that error as you go along. Don't worry if you make other errors. You can fix them later after you have learned more about the Vi editor. The The vi editor is a fill editor that allows you to move the cursor around the entire screen when you are in <cr> the command mode. You can do this by <cr> (l)typing certain unique key sequences, <cr> (2)or you can move from one line to another or over one or more spaces <cr> ona line by using the h j k 1 keys. <cr> The h key moves the cursor to yhe left, 1 moves the currsor to the left, <cr> j moves the cursor down one line and k moves up two lines.<cr> Delete line l.<cr> Delete line 2. <cr> Delete word word 1. <cr> Delete delete word 2.<cr> You can also move lines to a new location by using the "y" for yank. <cr> It will copy your line or lines to a buffer and you can then place it <cr> somewhere else by using the "p" for the put command. <esc> After you have completed the paragraph, you are to press the <esc> key. The "INSERT MODE" message should now be gone from the bottom of the screen. Mg the Document http://www.cs.okstate.edu/~c52351/unix01.html 1/13/2011 Unix Lab#l — vi editor Page 4 of 12 Before you do anything else, you should save the document. Issue the command: :w What was printed on the screen? “ Patch” ” ML, é “dim Wig/1,5,5 i The "W" means "write". It is the command to save a file. You have saved the file, practice. If the computer went down BEFORE you saved the file, you would not have a copy of it to work with. If you are entering in long files or programs, you should save them often so you do not have to retype everything again if the computer goes down. A good thing to keep in mind when you are working with computers is: If something will go wrong it will, and at the most inconvenient time! Making Changes in the vi editor You are ready to make changes now that you are in the command mode. Note that sometimes you continued onto the next line of the editor before you are asked to use the "<cr>" key. In the vi editor, a line is considered to be one line until the <cr> key is used, even if there is more than one line of typing before you use the <cr> key. If you kept on typing the entire paragraph and did not press the carriage return, you would have a paragraph that is considered one line long! This is significant when you make changes using the keys that move you up one line or below one line. Using the command mode to make changes rinvyour file You will be using various commands in this part of the lesson. UNIX is case sensitive, meaning that if you see a lower case letter, you must use a lower case letter and not an upper case letter as these have different meanings. You are now ready to get some practice using the command mode. NOTE: If you make a mistake, you can "undo" the mistake by immediately issuing the undo command by entering u. If you enter a command other than 11 after the command causing the mistake, the shortcut undo won't work. Moving to the top or bottomiof the document In some cases you will use different methods to make the same changes; thus, you can compare them and Select the one(s) you find easier to use. Move the cursor to the home line (top of document) of the work buffer by issuing the vi editor command: H The H takes you to the top of the document. After t ping the H, What character in What word is highlighted by the cursor? Th “T” A HA, 1' “wk ,/ Move the cursor to the last line of the work buffer by issuing the command: L In the case of the or "L" command, you did not have to press the enter key after you typed them in. What character in what word is highlighted by the cursor? http://www.cs.okstate.edu/~cs235 l/unixOl .html 1/13/2011 Unix Lab#l - vi editor Page 5 of 12 .1 . H II 39"” 55-} ~ \\. i l 3&4. 6 ix 50 Mr miter: i! Note that L moves the cursor to the last line of the file, whereas, 1 (lower case L) moves the cursor right on the current line. Moving around in the documeint You can move around one space or line at a time by using the h, j, k, and 1 keys. You should be at the . bottom of the document now. Next, press the k key three times. What happens when you do this? TL (page; 3055 «Af 3 if“: Next, press the 1 (lower case L) key three or four times. What happens when you do this? Max/45 44% mam +9 #1“ 471/ Try the j key two or three times. What happens when you do this? Mugs" fl dim. mama! (la-w? Next, press the h key three or four times. What happens when you do this? Mo's/«:5 ‘Hd. UAIfm" You can also move one word either forward or backward at a time. In order to do this you use the W for forward and b for backward. Move to the top of the document. How did you do this? if i ' T65vti/37rg_ roMMm-«ci l H” Next press on the w key three or four tiiries; What happened when _ ou did this? TL“ 5’.“ (513 f Net“! in Di: iiiu 'iieiwiti’a wild Try pressing on the b key three or fo times. What happened when you did this? mu Q Vile MOW if} ii 3 i, is“?! oi Fii'fi. View 15‘ You now have learned how you can move to the top or bottom of the file. You also have seen how you can move around in the file one character, one line at a time, and move one word at a time. NOTE: Depending on your computer setup and the terminal emulation, you may be able to use the arrow keys in place of the h, j, k, and 1 keys. The terminal emulation should be set to VTlOO. See instructions at top of this lab. Searchingflforgwords or sequences of letters Move to the top of the document by typing in: H Now you can search for certain words or sequences of letters if you want. You can do a forward search meaning that the computer will search from the top of the document to the bottom. To do a forward search, use the "/" (the slash) followed by the word or letters you are searching for. Find the word fill by using the forward search technique. Type in the following command: /fill <Cr> http://www.cs.okstate.edu/~cs235 l/unixOl .html 1/13/2011 Unix Lab#1 — vi editor Page 6 of 12 Notice that you need to press the Enter key after you type in this command. What character in what word is highlighted by the cursor? m “t” .4» “ Fill" Once again search (forward) for the word fill by issuing the command: / 2' /fi NOTE: For a forward search, the first line of the file follows the last line of the file. Also, since there is only one occurrence of the characters fi in the text, fi can be used as the search pattern. Now, move the cursor to the last line of the work buffer by using the L command. Issue the (backward) search command: ? f i _ “gr! \ u W Jr What character IS the cursor on? Tl-c In Hi NOTE: For a backward search, the last line of the file follows the first line of the file. Move the cursor to the last line of the work buffer. Now, search backward through the text for the characters "and". NOTE: there are several words with "and" in them. To repeat the find command, type in: 11 The n will allow you to repeat the find command so you can find all uses of the characters "and". Go to the top of the document. List in sequence all the words in the text thatcgjntained the characters and. Cor/(mag gig: {QIMMWA§§% Making changes inAwords or adding new words Move to the word fill again. This time change fill to full by placing the cursor over the i in the word fill. Use the "1" (lower case L) key to move to the i. The cursor will now be over the "i" in fill. Next issue the Vi single character "replace" command: ru to make the change. The character beneath the cursor is replaced by the character following r. What happened to the word "fill" when you used the "ru" command? 9. is H y- WI Tat AGE {5! “h; in 5‘ it {53/ http://www.cs.okstate.edu/~c52351/unix01.html 1/13/201 1 Unix Lab#l - vi editor Page 7 of 12 Next, you are to change full editor to full screen editor by using either the insert command or the append command. These are the differences between append and insert. 1. Append means to "add on to the end." If you are at the end of a word and you want to add on a new word, you can use the "append" mode. If you were on the last "1" in the word "full" and you pressed the a key, you would move over one space to the right. Then you would start typing. 2. Insert mode is different. This would usually be used when you wanted to add something before a word that you already have. If you were on the "f' in the word "full" and you pressed the i key, the cursor would stay on the letter "i". You would then type in a letter or word that would go in "front" of the letter "f". You need to get at the end of the word "full" or at the beginning of the word "editor" in order to make this change. You should use the h, j, k, or 1 keys to do this. After you are in the right position, you will then use either a for append or i for insert to add the word, "screen". Type in: screen DON‘T FORGET TO PRESS THE <esc> KEY TO GET OUT OF EITHER OF THESE MODES! What command(s) did you use? append On what character of what word did you place the cursor before issuing the command? '7 “Wm M I! t Try adding more words again by changing k moves up to k moves the cursor up. What command sequence did you issue to accomplish this change? ("9"? Vi “rift of Maurie iii-an. §?>‘C§£A (6M? M/fllll <f$c7> After you have completed the change above, you need to make another change. Actually, the "k" key moves the cursor up only one line. You can change the word "two" to "one" by using the "cw" command. Position the cursor on the "t" in the word "two". Next type in: cw (do not press the enter key at this time) What message do you see in the bottom right hand corner of your screen? \“ - X NSE (351 ~ ~ Also, do you see any other change in the word "two"? _ $1 ($50. We 1N 1% w/x Next, type in the word: one Notice, that you are still in the "CHANGE MODE". You will need to press the <esc> key to get out of that mode. You will then be back in the command mode. Now that the "two" has been changed to "one", you need to change the word "lines" to "line". Move to the "s" in the word "lines" and then use the "x" command. This allows you to delete one character. Place http://www.cs.okstate.edu/~c5235 l/unixOl .html 1/13/2011 Unix Lab#l — Vi editor Page 8 of 12 your cursor on the "s" and then type in: X What happened when you typed in the "X" ? 7L “5 H Jig/zymnj Next, position the cursor so that you can change yhe to the. Use the single character "replace" command. What command sequence did you issue to accomplish this? /’ Z \\ I! Deleitinglvordioriling Place the cursor on the "t" in the word "typing" and type in: dw to delete the word "typing". Now undo the deletion by issuing the undo command: u If you placed the cursor on the t in typing, the entire word was deleted. What would have happened if you placed the cursor on the y 'nstead? W“ a; ) ‘/ TlM w01& :5 "hm My” {neg (fay/33¢} Try it and report the results here. fl,» n15“ (23} AL! wow) +1“ 4.19%“) wag Undo whatever you just did so that the word typing is still in the text. The word The is duplicated on the first line of the work buffer. Use the dw command to delete one of the duplicates. Move the cursor to the number (1). Then issue the command: dd Whathappened? TL; liq 0i "taxi (ligaWaMml Restore the text by issuing the undo command. Next move to the line that begins with the number (2). Issue the command: d <cr> What happened this time? ‘ % TVA? lCNLS { Mjli'vfle-E‘EV 0AA inijingflfwfi ii MT} 43,: er: did L~ From these examples, what are the differences between the two delete commands? WA“ baht“ Dnifl H" (muff? limit uti<5f3ii / g Quilt 49.3, http://www.cs.okstate.edu/~cs235 l/unixOl .html 1/13/2011 Unix Lab#l — vi editor Page 9 of 12 Issue the undo command to restore the text. Four lines of the text refer to deleting a word or the entire line. Position the cursor on the line that contains Delete line 1. Issue the command: 3dd What happened as a result? 3 lie, g‘ Wm if Now issue the undo command: u / What happened? fiafi fart { a {in I Now, delete the two lines containing the statements Delete line. Next position the cursor to delete one of the duplicated words (word and delete) in the two remaining sentences. What commands did you issue to accomplish this? / udw H Copying orrmoving lines to other locations, in a document If you want to copy a line or lines to a new location in a document, you can do this by using the y for the yank command. The p for put is used to then place the line in the new location. Be sure that you are in the command mode. Move to the line that starts with (1). Type in the letter: y (lowercase Y) You will not see any change at this time. But the lines with the (l) and (2) will be kept in a buffer. Now put the cursor at the beginning of the line that says You can also move lines to a new location by using the "y" for yank. Type in: p (lowercase P) ‘ a i} What are the reSUItS? TlM ‘l‘wD lire-i 3 WW; {ME swig if}: (re-an: / a? 3 ‘ The y will copy two lines. The p should place two lines after the line listed above. Now remove the two lines by typing in: d <cr> Next, go to the line that starts with the (1). Type in: yy (lowercase YY) Next, move the cursor to the line that starts with: You can also move lines to a new location by using the "y" for yank. Type in: http://www.cs.okstate.edu/~c5235l/unix01.html 1/13/201 1 Unix Lab#l — vi editor Page 10 of 12 P (uppercase P) What happened this time? iii/w {a a”, If a». wafer .5355?” flag The yy will only yank one line just the same as the dd only removes one line. The P will put the line before the line containing the cursor. You can also copy more than one line at a time if you want by using the nyy where the "n" is the number of lines you want to copy. Try this out by moving to the beginning of the line: You can also move lines to a new location by using the "y" for yank. This time, type in: 31/1! Next move to the line with the (2) in it. Place your cursor on that line. Type in: p (lowercase P) What happened this time? 3 like; \p (LA L,,,c>t%i5,¢i 5-1,».fi {fit}? 5’ i After you copy lines to a new location, you can remove the other copies of the lines by using the "dd" or the "d" commands if you wish. Changingwords and other error corrections Next, look over the text to correct any other errors present. Note that both the h and the 1 key say that they move to the left. Only one of them moves to the left——the "1" key moves to the right. You can change this by using the cw command as you used before. You can also use the "R" for replace word. Position the cursor on the letter "1" in left. Then, type in: Rright What message did you see at the bottom of your screen? ~ Ramada“ What do you have to do to get back to the "command mode"? <c5 c. '7 Also the word, currsor is spelled wrong. Remove the extra "r" in currsor. What command did you use // to fix this problem? '<Jck/c> ZWEI on! #{ *fik aria Next, go search for the pattern " 011a”. There is supposed to be a space between the "on" and the "a". Place your cursor on the letter "n" . Next, type in "a" for append. What happens to the cursor and what message do you get on the bottom of the screen? The. Curie-I Mom “in 4%. ‘is’v’fiu/ "’Z/VSEIQVMM http://wwwcsokstate.edu/~cs23 51/unix01.html 1/13/2011 Unix Lab#1 - vi editor Page 11 of 12 Press the space bar next. What key do you have to press to get out of the "Append Mode"? <69c> Adding newliines Next, move to the line that starts with "Delete word 1". Get the cursor on the "D" in Delete. Type in: O (This is capital O—---not zero) What happened when yop typed in the "O"? A was Midfici ./f You have opened a new line above the line that you were just on. You are already in insert mode. You are now ready to type in the next three lines. If you want to open a new line above, you can press the "0" key. <cr> If you want to open a new line below the line you are on, <cr> press the "0" (lower case "0") key. <esc> Joining lines together Another feature that may be useful is to join two lines together. Go to the end of the line that says "If H H you want to open a new line below the line you are on,". Put the cursor on the , on that line. Type in: J Note that this is the capital "J". What happens when you do this? at Vm ha 3a: is at. that I)" we? Saving your file Next, save your file so it will be available to you the next time you need it. After correcting the errors, issue the command: :wq ' Z 1" “SC” r " l '7! 7 mm M: L What was pnnted? ~ betas ' st £1-21.» (if h H. The wq stands for "write" and "quit". You used the ":w " earlier to save the file. What was the difference in size of the two versions of the file that you saved? (You wrote down the file size earlier). Tit}; rm: has with; The first file was replaced by the file that you just finished. You have used examples where you have been in the "INSERT MODE", "APPEND MODE”, "CHANGE MODE”, "REPLACE MODE", and also "OPEN MODE”. After you have finished in any of these modes, you had to press the escape key to get out of them. When you are finished and want to log off of the UNIX system, at the $ prompt, type in: http://www.cs.okstate.edu/Ncs2351/unix01 .html I 1/13/2011 Unix Lab#l - Vi editor Page 12 of 12 exit or ctrl d At the end of each lab, there will be a space for you to write comments about parts of the labs that you did not understand. The professor looks at these to see if there are problems that many students have with certain parts of the lab. If there are, the professor tries to fix these in the next set of labs. Your comments are appreciated. If the lab is easy to understand, then that information is also useful. Please indicate any parts of this lab that were hard to understand. Also indicate parts of the lab that you had difficulty with and the nature of the difficulty. j: Mud! 9w?" mark/rive m Aaftfem r???“ {a Mr 6*” 4/74“ ’ 3?,“ H 1:9; N. fl/so ,TF I Sl‘ifi c 01/ e a: FallO7 http://www.cs.okstate.edu/~cs2351/unix01.html 1/13/2011 ...
View Full Document

Page1 / 12

Lab 1 - Unix Lab#1 - vi editor [CE Page 1 of 12 [m LAB #1...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online