Assignments > Lab 1: Code Editors
Due on Wed, 08/27 @ 11:59PM. 6 Points.
Introduction
Welcome to your first CSCI 338 lab! The goal of today’s lab is to get you a little more comfortable doing “configuration things,” including working on the command line, configuring your command line environment, and working with command line code editors. You will complete the following tasks:
I have curated a list of useful resources on the course resources page. Please see the “Command Line” and “Code Editors” sections:
Part 1. Set-Up
- Install VS Code if it isn’t already installed on your machine.
- If you are a Windows user, follow these instructions to install WSL and a Linux distribution (Windows Subsystem for Linux). Once you’re done, verify your installation by opening a WSL terminal and typing
pwd.- Note: please read / watch the instructions for WSL carefully. If you skip steps, you will likely have to rebuild / reinstall your Linux distro, so going slower will save you time in the long run.
- Create a directory called
csci338- If you are a windows user, you will create that directory within your home directory within your WSL instance. (e.g.,
/Users/your_username). Important: Your WSL home directory is in a totally different place than your home directory on your host machine. To find it:- Activate WSL on your command line
- Navigate to your Linux home directory by typing
cd(which will automatically take you to your home directory on Linux), and - Type
pwdto see where your home directory is. You will create yourcsci338directory here.
- If you are a Mac user, you can create your
csci338directory anywhere except for in your Downloads directory.
- If you are a windows user, you will create that directory within your home directory within your WSL instance. (e.g.,
Part 2. VS Code Exercises
2.1. Install VS Code and Extensions
Please install the following VS Code Extensions:
- Live Server (by Ritwick Dey)
- Prettier (by Prettier should have the blue “verified” badge)
- Prettier ESLint (by Rebecca Vest)
To install VS Code Extensions:
- From within VS Code, open the extensions window by clicking the extensions icon (looks like 4 squares on the gray, left-hand bar).
- Search for the extension name using the search textbox.
- When you find the extension, install it.
2.2. Configuration Tasks
Configuring Prettier
Configure “Format on Save” using Prettier by modifying settings.json file (a configuration file used to customize your VS Code Editor). To find settings.json, type Shift + CMD + P (MacOS) or Shift + CTRL + P (Windows) and then type settings.json in the search textbox that appears. Then, add the following code to settings.json within the curly braces. Note that in JSON, each key-value pair must be separated by a comma or else there will be syntax errors:
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
You can read more about configuring “Format on Save” using Prettier here.
When you’re done, test that the “format on save” functionality works by creating a test.js JavaScript file with following code:
function foo(a, b) {const c=a+b; const d = c**2; return c+d;}
When you save the file, it should be autoformatted as follows:
function foo(a, b) {
const c = a + b;
const d = c ** 2;
return c + d;
}
Before Moving On, Verify that VS Code autoformats when you save.
Part 3. Complete the Command Line Exercises
Please complete the following command line exercises with the help of the Command Line Reference that has been compiled for you. Feel free to collaborate with your classmates!
3.1 Open a Terminal
- If you are a Mac user, open the Terminal program
- If you are a Windows user, open WSL
3.2. Navigation
- Figure out which directory you’re in (use
pwd)- Windows users: if you type
explorer.exefrom within WSL, it should open a Windows Explorer window to show you where your WSL files are located.
- Windows users: if you type
- Navigate to the folder where you plan to save your coursework (use
cd).
Pro-tips:- If any of your folder names have spaces, you’ll have to surround the path with quotes
- Use the tab key to autocomplete the path
- Use the up and down keys to revive old commands
- Use the
historycommand to see the commands you’ve issued in the past
3.3. Create
- Navigate to the
csci338you made in Part 1 on the Terminal. - Create a directory called
lab01withincsci338(usemkdir) - Navigate into the
lab01directory you just made. - Create a new file called
index.html(usetouch) - Create another new file called
style.css(usetouch) - Copy the Google homepage locally as follows:
curl https://www.google.com > google-home.html
If you did everything correctly, you should have a directory structure that looks like this:
csci338
├── google-home.html
├── index.html
└── style.css
3.4. List
- Verify that the two new files exist in your current directory (use
ls) - List all of the files and folders in your home (use
ls ~) - List all of the files and folders on your home directory including hidden files (use
ls -la) - List all of the files and folders in your home recursively, Try:
tree ~ -La 1tree ~ -La 2tree ~
If
treeis not installed, see if you can figure out how to install it. On WSL:sudo apt-get update sudo apt-get install treeOn Mac:
brew install tree
3.5. Read
- Read the contents of the
google-home.htmlfile you just created (usecat) - Inspect the file using some of the other read commands (e.g.,
less,head,tail, open wc).- Pro-tip: For
less,head, andtail, use the space bar to scroll down, and “q” to quit
- Pro-tip: For
3.6. Write
- Append the sentence “Hello World” to the contents of the
index.htmlfile (echo "Hello World" >> index.html). - Do it again.
- Read the contents of
index.html(usecat) - Now replace the contents of
index.htmlwith “Goodbye” (echo "Goodbye" > index.html) - Read the contents of
index.html(usecat) - You can also use the
>>and>operators to direct the output of the terminal to a non-existant file:echo 'Yo yo' > new.txt - Read ‘new.txt’ (use cat)
- Now remove it (rm new.txt)
- Make sure you understand the difference between
>>and>?
3.7. Move & Copy
- Copy a directory and all subdirectories (try:
cp -r my_folder copy_of_my_folder) - Copy a file (try:
cp foo.txt bar.txt) - Rename a file (try:
mv bar.txt bar1.txt) - Move a file to a parent directory (try
mv bar.txt ../.)
3.8. Search
Use grep to search for files for strings / text…
- To find the word “Goodbye” in your current directory or any descendents (try:
grep "Goodbye" ./ -r) - To find the word “goodbye” – case insensitive – in your current directory or any descendents (try:
grep "goodbye" ./ -ri) - To find the word “goodbye” – case insensitive – anywhere in your home directory or its descendents (try:
grep "goodbye" ~ -ri)
3.9. Make a bash script
You can also combine multiple commands into a bash script (use the .sh extension). Let’s make a bash script that sets up a basic web app in your current directory. Try the following:
- Create a script called
start-web-prj.sh - Add the following lines of code to the script:
#!/bin/bash
# Prompt the user for the folder name
read -p "Enter the folder name: " DIR_NAME
# 1. Create a new directory if it doesn't already exist
if [ -d "$DIR_NAME" ]; then
echo "Directory '$DIR_NAME' already exists. Exiting."
exit 1
else
mkdir "$DIR_NAME"
echo "Directory '$DIR_NAME' created."
fi
# 2. Navigate into it
cd "$DIR_NAME" || { echo "Failed to navigate into $DIR_NAME. Exiting."; exit 1; }
# 3. Create a new starter index.html file
echo '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css" />
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
<p>Your starter file.</p>
</body>
</html>
''' > index.html
echo "index.html created."
# 4. Create a new starter styles.css file
echo '''
body * {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
}
''' > styles.css
echo "styles.css created."
# 5. Navigate back to the original directory
cd ..
# 6. Optional: Open the index.html file in a web browser
# Try one of these commands:
# wslview "$DIR_NAME/index.html" # WSL
# open "$DIR_NAME/index.html" # macOS
When you’re done, execute the script as follows:
bash ./start-web-prj.sh
Take a look at the new src folder (and nested files) that were created by typing: tree .
Now, open the HTML file you just made in a new browser by typing one of the following commands from the command line:
- Mac Users:
open my_new_folder/index.html - WSL Users:
wslview my_new_folder/index.html - Linux Users:
xdg-open my_new_folder/index.html
And finally, open the folder you just made in VS Code:
code .
If that didn’t work and you’re on a Mac, see this Stack Overflow post.
Before Moving On, Verify That…
- You have practiced working with the following commands:
- cat (read)
- cd (change directories)
- tree (list files and subdirectories)
- touch (create new empty file)
- ls (list files)
- rm (delete a file)
- mv (rename a file)
- cp (copy a file)
- history (view recent bash commands)
>>(append operator)>(overwrite operator)- grep (search)
- running a bash script:
bash ./start-web-prj.sh- You can open VS Code from the command line
- Your file structure looks like the file structure below:
csci338
└── lab01
├── google-home.html
├── index.html
├── my_new_folder
│ ├── index.html
│ └── style.css
├── start-web-prj.sh
└── style.css
Part 4. OS Environment Exercises
In Linux-style operating systems, you can create shortcuts, aliases, and customizations by editing your .zshrc file in your home directory. From your command line, please navigate to your home directory and try making an alias to your csci338 directory:
- To learn more about how to create an alies, see this resource: The Significance of .bashrc or .zshrc Configuration File
- If you did it correctly, when you type:
338on the command line, you should be put into thecsci338directory. - Hint: Here is what Sarah’s
.zshrcentry looks like:alias 338='cd /Users/svanwart/unca/csci338'
Before moving on, verify that when you type 338 on the command line, it automatically jumps you to the csci338 directory
Part 5. Vim / Emacs Exercises
- Open a file from the command line
- Edit it
- Save it
- Exit the editor
What do I turn in?
Under Lab 1, paste a dump of all of the relevant command line history commands that you used today (type history on the command line, copy it, and paste it into the Moodle).
What to study / have done after completing this lab…
- If you are a Windows user, make sure your WSL is installed and configured
- Make sure your VS code editor is set up.
- Make sure you know some basic shell commands, and specifically how to navigate, search, create, delete, copy, read, and move files. Here are some sample quiz / exam questions: Sample command line quiz questions
- Make sure you know how to open, edit, save, and exit either vim or emacs.