The purpose of this lab is for you to learn how to use vim as a text editor and to familiarize yourself with Regular Expressions.
Before starting the lab, please navigate to the lecture material and make sure you have a basic understanding of Vim and regular expression.
You should also open up the Vim Cheatsheet we created for quick reference.
Depending on your working environment, perform the appropriate action:
Environment | Action |
---|---|
Linux Lab Machine | Open a terminal window |
Windows or Personal Computer | SSH into data.cs.purdue.edu (No X11 Forwarding) |
You need to download a sample .vimrc to make using Vim a little easier.
# NOTE: Don't do this if you already have a .vimrc customized to your liking!!!
wget -O ~/.vimrc https://raw.github.com/ClydeByrdIII/cs190lab7/master/vimrc
You can find the GitHub clone URL on right side of page towards the top.
git clone <github_clone_url>
NOTE: DO NOT USE INSERT MODE UNLESS TOLD TO DO SO. USE THE COMMANDS DESCRIBED TO FIX THE ERRORS.
This file contains duplicate lines, words, and misspellings that need to be fixed. You need to edit this file using Vim and fix the errors.
You can find the commands in the Vim Cheatsheet.
This is the time you should probably open up the Vim Cheatsheet mentioned in the beginning of the lab :)
-
Open
errors.txt
in Vimvim errors.txt
The file originally looks like the above text. As you can see there are multiple errors in this text file, such as lines 3-6 being duplicated and lines 8 having a mispelled 'I'.
-
Remove duplicate lines
BIG HINT: The commands listed here correspond to commands that can be found in the Vim Cheatsheet, most are under the section "Editing Text in Command Mode"
GOAL: Remove duplicated lines appearing on lines 4 and 5. COMMANDS: delete current line, delete n lines.
-
Remove duplicate words
GOAL: Remove duplicated words appearing on line 5 COMMANDS: delete word, delete n words
-
Fix misspellings
GOAL: Remove characters causing misspellings on lines 6 and 7. COMMANDS: Remove character
-
Save the file and quit
:wq
This file contains more than 1000 lines and sometimes more than 1000 words on a single line. The labs asks us to edit it in annoying ways. It would be really annoying to make these edits with gedit
. Vim will save the day.
-
Open
annoying.txt
in Vim.This file has lines containing
'Please Keep This LineX'
and'delete this lineX'
. You need to delete the lines that ask you to delete themselves. -
Remove 3 lines
GOAL: Delete lines 6-8 (3 lines) COMMANDS: delete current line
-
Remove 20 lines
GOAL: Delete lines 11-30 (20 lines) COMMANDS: delete n lines
-
Remove 1000 lines
GOAL: Delete lines 16-1015 (1000 lines) COMMANDS: delete n lines
-
Remove 3 words
Just as you can remove 1000 lines, you can also remove n words.
GOAL: Remove the BADWORD words from the last line of the file COMMANDS: delete n words
-
Save the file and quit
GOAL: Save the file annoying.txt COMMANDS: Save and quit Vim ```
This is a really long file...actually a book called The Young Engineers. There are little messages interleaved in the middle.
- Open
jump_around.txt
in Vim. - Press
Shift + G
to go the very end of the file - Type
gg
to go to the beginning of the file - Type
:50
to go to line 50. - Type
:457
to go to line 457. - Type
$
to go to the end of line 457. - Type
^
to go to the beginning of line 457. - Get comfortable with what you just did. Do it over if you have to.
- Type the letter
i
on the keyboard. - Insert some text.
- Quit Vim without saving.
The vimrc file contains optional runtime configuration settings that get loaded each time Vim is run. This means that what ever the settings you have in this file, they will be executed when Vim is started.
For part two we will add a couple of settings to your .vimrc that maybe useful.
- Open
~/.vimrc
with Vimvim ~/.vimrc
-
Jump to the bottom of the file using
Shift + G
. -
Jump to the end of the line by typing
$
. -
Press
i
to enter Insert Mode. -
Insert a new line at the bottom of the file, and paste the code below on that line.
If you try and paste code without being in Insert Mode, each character that is part of the pasted string will act as input to Vim.
e.g., if you paste the word
insert
while still in Command Mode,i
will trigger Insert Mode and thennsert
will be pasted." Always display the current cursor position in the lower right corner of the Vim window. set ruler " Press space to clear search highlighting and any message already displayed. nnoremap <silent> <Space> :silent noh<Bar>echo<CR>
-
Now save the file and quit
<ESC> :wq
-
Now open the file again in vim and you should see the current cursor position in the lower right of the window.
In this section, we are going to work towards validating email addresses using regular expressions.
Now would be a good time to bring up the Regex Cheatsheet (it's the same one, I was lazy) (scroll to the bottom of it).
Imagine this. You built a website and have users sign up with their email. You would like to validate these emails to make sure that no errors occur and that no funny business is going on. When the user inputs his email into the field and clicks 'Submit', there should be code that checks if it is a valid email.
Regular Expressions can do this!
- Before the
@
symbol, any combination of letters, numbers,+
,_
,.
- Must have at least one letter/number before the
@
symbol - Must contain a
@
symbol - After
@
, domain is required to be a .com, .edu, or .net.
So when we're done, our regex should be something like this.
This is pseudocode, its just to give you a general idea of the layout of the regex
letters numbers + _ .
@letters
.com net edu
-
Go to http://regexpal.com
The top box is reserved for the Regular Expression pattern and the bottom for the test data. If a sequence of characters in the bottom box matches the pattern from the top, then it gets highlighted in blue or yellow (color irrelevant).
-
Paste the text below into the bottom large box on regexpal.
VALID EMAILS [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] INVALID EMAILS @FaceBook.com Username@ test@yahoo [email protected] [email protected] [email protected] BADemail@. [email protected] [email protected] [email protected] I'[email protected] user@web?com username@@example.com [email protected] [email protected]
-
Let's test. In the top box, type
[a-z]
This highlights every letter that is in the range a-z
-
In the top box, type
[a-z]+
Every sequence of letters in the range a-z that are next to each other are highlighted.
-
Let's include capital letters. In the top box, type
[A-Za-z]+
Your goal is to get each of the emails under "VALID EMAILS" to be completely highlighted by a single color (shown below)
If you need help, first look at the Cheat Sheet, then ask your neighbor, then ask the TA.
Fill out the CS190 survey, either now or later.
- Open up
errors.txt
in Vim and show us you have fixed the errors - Open up
annoying.txt
in Vim and show us you have removed the correct lines - Show us your regexpal screen (should look like the picture above!)
You can, and you should, either move all of your CS190 files to a separate folder or delete them. We littered your home drive (I'm sorry!), so you should clean it up. Deleting the folders relating to CS190 will not mess up any of your configuration.
We will have the lessons on the Internet so you can reference them in the future.
Please fill out the surveys.