--- tags: labs-summer21 --- # Just for Fun: Terminal Commands One of the most useful tools you should get familiar with through your study of computer science is the **terminal**. Familiarizing youself with how to maneuver through the terminal and use some basic tools, like git and text editing, will be very helpful throughout CS18 and beyond. ## 1. Accessing the terminal * To open the terminal using macOS: Go to *Applications* -> *Utilities* -> Open *Terminal* * If you are using a a Windows machine: Search for *Command Prompt* ## 2. Basic Terminal Commands **Task: Skim Terminal Commands** This is just a list of basic useful commands; feel free to skim through then refer back to it as you do the tasks below. First, we have the `man` (manual) command and `--help` tag to help us learn about any command we might want to use. ``` man <command> ``` `man` prints the manual of a given command. For all of these commands and any new commands, use `man` to find out more about them, e.g. `man pwd` ``` <command> --help ``` Adding the `--help` tag after any command allows you to access the help menu for a specific command if available, e.g. `pwd --help` ``` pwd ``` `pwd` or "print working directory" will print the full (absolute) path to the folder the user/terminal is currently at. ``` cd <directory> ``` `cd` or "change directory" moves the user to the indicated directory if it exists inside the current directory. ``` cd </absolute-filepath> ``` putting a `/` before a filepath indicates that it's absolute, i.e. that it start from the root directory instead of from the current working directory (meaning it doesn't matter where you are now) ``` cd .. ``` `..` is a link within every directory to the previous directory that contains the current directory, `cd ..` lets you move back one folder ``` cd ~ OR cd ``` These commands will move you to your home directory (noted with a ~), which is usually your user name (e.g doing `cd` then `pwd` I get `/c/Users/mthain`) ``` ls ``` `ls` prints the contents of the current directory. ``` mkdir <directory> ``` `mkdir` creates a new empty directory named `<directory>` in the current directory. ``` rmdir <directory> ``` This command removes an **empty** directory. It must be called in the directory that contains what a user wants to remove. ``` rm -r <directory> ``` This command removes any directory and all of its contents. ``` cat <file> ``` `cat` prints the contents of `file` to your terminal ``` vim/nano <file> ``` `vim` or `nano` are basic text editors that work within the terminal; we'll talk more about them later ``` mv <filepath1> <filepath2> ``` `mv` moves what file is at filepath1 to filepath2. for example, you could put a file into a folder with `mv file folder`. `mv` will also rename the file if you include a non-folder name at the end, e.g. `mv file folder/newname` ___ **Task:** Within your own terminal, we will make a cs18 directory and resetup your file system to be based in your home directory and easily accessible. Feel free to change steps (e.g. disclude the browncs folder) or not user this, but at least go through the motions so you know how. First, cd home and make a new "browncs" folder to put everything in (I would also definitely just make a shortcut to this folder to put on your desktop and/or pin it in your Finder/File Explorer). ``` cd ~ mkdir browncs ``` Next, we'll hop inside and make a cs18 folder. ``` cd browncs mkdir cs18 ``` Now we'll make directories for different assignments. ``` cd cs18 mkdir hws mkdir labs mkdir projs ``` Finally, let's reclone the lab01 repo to our new labs folder. Let's also use a key trick! Whenever you're dealing with files or folders that exist, you can use tab to autofill what you've typed so far. Try typing `cd l` then pressing tab to complete the command. ``` cd labs ``` Then, click this url: https://classroom.github.com/a/hUL5du-F and clone the repo there. Now, let's also make a `lab99` directory to show off how the tab autocomplete works. If we press tab and there are multiple options to select from with the same prefix we've given, like if we only type l but there are multiple labs, pressing tab again will show us all of them. Then, once we see our options, we can type the necessary amount (lab) then press tab to make it work. ``` cd l cd lab cd lab01 ``` Now, if you use `ls` to list what's contained in lab01, you'll see that there is a "just-for-fun" folder. Get in there yourself and play around with your new pet using the commands we've learned above. Feel free to also mess around with other files included or skip them until we actually get to the vim editor section. **Task:** Create a home for the cat by making a new directory and moving the cat inside to protect it from the scary eagle. Then rename it also using! I've secretly included a few typos in the last parts so you'll have to type the interesting `mv` commands yourself (remember to use tab!) ``` cat cat-this-sample-text.txt cat scary-predator-eagle.txt mkdir new-cat-home mv cat-this-sample-typo.txt new-cat-home mv new-cat-home/cat-typo-sample-text.txt new-cat-home/Kryptonite ``` ## Basic Git There are three steps to updating your repository: staging, committing, and pushing. Before you perform these actions, make sure you are in the base directory of the repository, or in other words, on the main branch of your "repo." You should see something in your terminal ending with `(master)` or `(main)`, e.g. ``` Desktop ~/documents/brown/homework/cs18/lab01 (master) $ ``` If this doesn't show up, you can also run `git status` in your terminal, which will let you know if the current directory is a git repository. **Task:** Double check which branch of the repository you are in right now, and make sure that you're in the master. To stage your changes, you can use **`git add -A`**. There are many options you can use with `add`, but doing `git add -A` usually suffices. By staging, you are essentially telling Git, "These are the changes I want to save to my repository" (`-A` means stage *all* changes, which is usually what we want). **Task:** Prepare to commit by staging the changes you made to lab01. To commit your changes, use **`git commit -m "<some informative message>"`**. This command saves all the changes you specified in staging, and files it in the version history with the (hopefully) informative message you wrote. For instance: `git commit -m "Added feature X to the code."` `git commit -m` saves to your local filesystem, without updating your online GitHub repository. **Task:** Commit your changes to your local filesystem. Now your code is saved in local history, but you need to let the remote repository (GitHub) know what has changed. Do **`git push`** to upload your changes to GitHub. **Task:** Upload your changes to GitHub by pushing. (Don't worry, this won't override our version of the lab or break anyone else's lab! We've given everyone individual lab repositories.) And that's it! To recap, in order to save your changes to GitHub: 1. Double check that you're in the master branch 2. Use **`git add -A`** to stage changes 3. **`git commit -m "<your message here>"`** to commit 4. **`git push`** to push your commits to GitHub To learn more about all the fun things you can do with GitHub, check out this [link](https://nbviewer.jupyter.org/github/dscov-tutorials/gitintro/blob/master/gitintro.ipynb). ## Basic Text Editing with Vim :::spoiler **If you have Windows, click here!** Windows unfortunately does not come with Vim pre-installed. You will have to install it manually. Follow [this guide](https://www.freecodecamp.org/news/vim-windows-install-powershell/#:~:text=Vim%20is%20a%20powerful%20code,it%20running%20on%20your%20PC.) to get it set up. **Note:** Don't worry about the 'PowerShell' bit - as long as you can use it in the basic command prompt application. ::: Sometimes you might find yourself in a position where you're working in the terminal and can't easily open or access a file with an application that has an easy to understand interface (e.g. Intellij). In these cases, it will be helpful to work with a terminal level text editor, like Vim or nano. **Task:** Open your terminal and navigate to the folder that contains your code for lab 1. Remember to use `ls` to see what directories you can move into and `cd` to move into one of those directories! Once in that folder, navigate to the `src` folder. Opening a file with Vim is very simple. All you have to do is type `vim` followed by the file you want to open. So, if you want to open a file called `ILoveCS18.java`, noce you navigated to the proper directory, you would type the following command: ``` vim ILoveCS18.java ``` **Task:** Open `Leaf.java` with Vim. Once you've opened a file in Vim, your terminal will display all of the text that appears in the file. Vim has a lot of functionality, but we're only going to worry about insert mode right now. You are free to explore how the other commands work if you so wish. For a complete overview of everything you can possibly do with Vim, [look at this document](https://devhints.io/vim). Opening a file with Vim only displays it, so in order to edit any text in the file, we'll need to enter insert mode. **Task:** Enter insert mode by typing `i`. At the bottom of the terminal window, you should now see `---INSERT---`. Once you're in insert mode, you can make changes to a file and save them without having to open an application like TextEdit or IntelliJ. **Task:** Navigate around `Leaf.java` by using the arrow keys. Get to the last line and then hit enter. This should create a new line at the bottom of the file. Once you've done that, write yourself a comment (remember that comments start with a double backslash, i.e. `//`). **Note:** You can also scroll using a trackpad (though sometimes this can be a little finicky). Now that you've made a change to the document, you'll need to exit insert mode and save it! To exit insert mode, press the `esc` button on your keyboard. Once you've exited insert mode, you can save a file by typing `:w`. You can save and close a file by typing `:wq`. **Task:** Exit insert mode, then save and close `Leaf.java`. Those are the absolutely basics of Vim! Feel free to play around and change actual text in any of the lab 1 files to see how replacement works. You likely will not need to use this during this class, but you may find it helpful for the future! Now you know how to use some basic tools in the terminal! Hopefully, these skills give you a better sense of the inner workings of your computer. --- *Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS18 document by filling out the (mostly) [anonymous feedback form](https://cs.brown.edu/courses/cs018/feedback)!*