Answers to Lab 2: Learning the vim editor.
The answers are in parenthesis, in bold, after the question. Also, in the
syntax :%s/old/new/g, the percent sign (%) is equivalent to 1,$ (as in your notes).
- Download (save link/page ) the file vi_practice file
to your home directory.
- Open this file in the vim (vi) editor.
- (1 point) Practice cursor movement commands:
(1 point) Practice editing commands:
- move the cursor right and left one character, and down and up one line
(right: l, left: h, up: k, down: j)
- move the cursor 10 characters to the right, then 5 lines down by pressing only
(right 10 characters: 10l, down 5: 5j. together: 10l5j)
- move the cursor to the beginning of a line, then go word by word across the
(beginning of line: 0, press w to go across word by word)
- move the cursor to the beginning and the end of the line with single character
(beginning of line: 0, end of line: $)
- move the cursor to line 20 without counting the lines, then go up one page
(To line 20: :20, up one page: Control-F)
(1 point) Practice search and replace commands:
- delete the first line of the file (there is a delete-line command)
(delete a single line: dd)
- delete the first 2 characters of what is now line 7
(go to line 7: :7, beginning of line: 0, delete 2 characters: xx)
- Add words "AND DYNAMICS" right after line 15, on the same line.
(go to line 15: :15, end of line: $, append "AND DYNAMICS": aAND DYNAMICS)
- join lines 19 and 20
(go to line 19: :19, join with line after: J)
Save the file and quit vi, which returns you to the shell prompt.
- go to the beginning of the file and find the next occurrence of the word "and".
Replace it with "OR".
(beginning of file: :0, next occurrence of "and": /and, delete the word: dw,
insert "OR": iOR)
- Replace all occurences of string "and" with "or"
- Find string "understor". Where did this one come from?
(/understor, which came from replacing the last three letters of "understand" with "or")
- Now find every occurence of "And" and replace it with "But"
sure you did everything correctly by typing the command
diff vi_practice vi_practice_edited
This command looks for any differences in the contents of the two
files. If you get no output from this command, you've done everything
correctly. You can also compare the two files next to each other by
diff -y vi_practice vi_practice_edited
we're going to edit another text file - this time a PDB file that describes a real protein. Right
click on this link and choose "Save link
as... Or Save to disk...". Save the file in your home directory. Now open another unix shell and
create a file named
lab2_my_answers in your home directory. You will put your answers to questions Q1, Q2 and Q3 (below) in it,
each answer on its own line. Eaxmple: Q1: Bla-bla-bla ...
(if vim complains about the file being funny, don't worry.)
the file in the vi editor, and make the following changes:
- (1 point) Change the date on the first line to today's date
(jump over to '15-Jan-97', the fourth word: www. Delete the old date: xxxxxxxxx (or 9x), Insert the new one: i2-Feb-06)
- (2 points ) Delete all lines from 130 to 230.
(go to line 130: :130. Delete 100 lines: 100dd)
- (1 point ) Replace every string "ATOM" with "ELEMENT"
(1 point ) Q1: How many total lines are in your file now?
(show line numbers: :set number, scrolling down to the last line, you'll see line 621)
( 3 points ) Decrease the spacing between string "ELEMENT" and the number next to it by
exactly two spaces, for EVERY LINE in the file.
(you can match just those occurrances of "ELEMENT" by matching the double spaces in front. So use :%s/ELEMENT /ELEMENT/g)
(1 point each ) Q2: What animal ( ORGANISM_COMMON ) does this protein come from? Q3: What
is the scientific name for this animal?
(go to beginning of file: :0, search for "ORGANISM_COMMON": /ORGANISM_COMMON, it's a Chicken, called GALLUS GALLUS)
- Save each file and quit vi.