{ "cells": [ { "cell_type": "markdown", "id": "8f6bbaa9", "metadata": {}, "source": [ "# Setting-up VS Code\n", "\n", "A very useful feature of VS Code is its support of various programming languages - if you decide to work with another language or with several languages in parallel in the future, you can do it all within VS Code.\n", "Let's open your VS Code to start exploring its features.\n", "\n", "```{admonition} Can't find VS Code on your Mac?\n", ":class: dropdown, hint\n", "\n", "If you've installed and closed VS Code, and cannot seem to find it anymore on your Mac, press `command` + `Space` bar to open Spotlight search, type in \"Visual Studio Code\", and you'll find VS Code. Once it's open, it may be handy to make a **shortcut** to VS Code to make it quickly accessible in the future. To do that, drag VS Code app icon to the left side of the line that separates recently used apps in the Dock (tool bar at the bottom of your screen). For more instructions on the Dock, check the [Mac user guide](https://support.apple.com/en-ie/guide/mac-help/mh35859/mac).\n", "\n", "```" ] }, { "cell_type": "markdown", "id": "ef9e6280", "metadata": {}, "source": [ "## Theme\n", "\n", "When you first start VS Code, you will automatically be asked to choose your theme, i.e., the appearance of your coding environment. Choose whichever one you like the most. \n", "\n", "```{admonition} Dark or light theme?\n", ":class: dropdown, hint\n", "\n", "Some find dark mode better for readability, while others praise the light one for helping people with dyslexia (you can read more [here](https://builtin.com/software-engineering-perspectives/dark-mode)).\n", "```\n", "\n", "You can also change the theme at a later point in time and browse for more options by following the instructions [here](https://code.visualstudio.com/docs/getstarted/themes).\n", "\n", "```{figure} images/mac/VSCode_Mac_2.png\n", "---\n", "height: 300px\n", "name: \n", "---\n", "```\n" ] }, { "cell_type": "markdown", "id": "f4a5b6f8", "metadata": {}, "source": [ "## Activity bar\n", "\n", "Next, familiarize yourself with VS Code’s activity bar on the left side:\n", "\n", "```{figure} images/vscode_start/VSC_start_1.png\n", "---\n", "height: 350px\n", "name: \n", "---\n", "```" ] }, { "cell_type": "markdown", "id": "07812e79", "metadata": {}, "source": [ "## Workspace\n", "\n", "To start working in VS Code, you need to open a folder to set your workspace. We recommend creating a new folder `Programming-Foundations/` in your home directory. Inside the new folder `Programming-Foundations/`, create another folder called `Getting-Started/`. From within VS Code, click **File** > **Open Folder...** and select `Getting-Started/` (or whichever folder you decided to use for coding).\n", "\n", "```{admonition} Hint\n", ":class: note\n", "\n", "If prompted whether you trust the authors of the files in this folder, select the “Trust” option and press “Yes, I trust the authors”.\n", "``` ", "\n", "```{admonition} How to find your home directory \n", ":class: note\n", "\n", "Your home directory is the folder that contains your Documents folder, your Downloads folder, and many others. It is called `Home` in your file explorer and often labeled with a little house icon.\n", "``` " ] }, { "cell_type": "markdown", "id": "7a83050a", "metadata": {}, "source": [ "## Terminal\n", "\n", "Next, press View > Terminal. This will open an additional part in your VS Code workspace, with tabs such as Problems, Output, Debug console, and Terminal. We will get acquainted with Terminal [in the next chapter](../chapter3/terminal.ipynb), and some of the other tabs in later chapters.\n", "\n", "```{figure} images/vscode_start/VSC_start_2.png\n", "---\n", "height: 300px\n", "name: \n", "---\n", "```", "\n", "```{admonition} (Windows) Using Git Bash as your VS Code terminal\n", ":class: dropdown, hint\n", "\n", "If you have installed Git Bash, you can also use it as your VS Code terminal. After opening your VS Code terminal, simply click the downward arrowhead `⌄` in the bar above the terminal and select `Git Bash`. If you want to set your VS Code terminal to be Git Bash by default, you can do so under `Select Default Profile`.\n", "```" ] }, { "cell_type": "markdown", "id": "7078ec5e", "metadata": {}, "source": [ "## New Python file\n", "\n", "To start your first Python script, click on New File. When prompted, select Text file (Built-In). In the bottom status bar, you can see that your file type is plain text.\n", "\n", "```{figure} images/vscode_start/VSC_start_3.png\n", "---\n", "height: 350px\n", "name: \n", "---\n", "```\n", "\n", "## Python extension\n", "\n", "As you have noticed, you are asked to select a programming language, so let’s click on “Select a language” and select Python. Notice the change in the status bar. You will also see a pop-up window at the bottom right corner asking if you want to install the recommended ‘Python’ extension from Microsoft for the Python language. Select “Install” and wait for the installation to finish in the Extensions part of the activity bar. If you are already asked to select your interpreter as a prompt on the search box, select `python 3.13.x (pf-env) ...`. This is the environment you created [earlier](setting-up-environment.md).\n", "\n", "If you haven't been prompted to choose your interpreter, do the following:\n", "1. **Click on the VS Code search bar** at the top of your screen.\n", "2. **Type `>`**, but do **not** hit enter yet.\n", "3. **Type `Python: Select Interpreter`** to bring up the action `Python: Select Interpreter`. Select this action.\n", "4. **Select environment `python 3.13.x (pf-env) ...`**. The `x` and the `...` will be a number and a path on your device respectively. " ] }, { "cell_type": "markdown", "id": "c57e80e6", "metadata": {}, "source": [ "## IntelliSense\n", "\n", "Back in your Python file, start typing “pri”. You will notice that VS Code is trying to read your mind and suggest options of what you may want to type. Among these options is `print`, which is indeed what you wanted to write, so you can go ahead and press `Tab` or `Enter` to fill in the full word. This smart completion is called IntelliSense and is included in the Python extension you installed in the previous step. " ] }, { "cell_type": "markdown", "id": "a69b04e2", "metadata": {}, "source": [ "## Executing code\n", "\n", "Let’s write your full first lines of Python code. Type in the following in VS Code:\n", "\n", "```\n", "print(\"Hello world\")\n", "print(\"Goodbye\")\n", "```\n", "\n", "Save this file as `my_first_code.py` and then press Run Python file (the “play” icon on the top right) or right-click > Run Python > Run Python File in Terminal to execute this code. The output will show up in the terminal at the bottom of VS Code. \n", "Note that you can also run selected lines of code if you mark them and then press `Shift`+`Enter` or right-click and select Run Python > Run Selection/Line in Python Terminal. This is convenient for testing a part of the code.\n" ] }, { "cell_type": "markdown", "id": "0d42b2dd", "metadata": {}, "source": [ "## Auto-save\n", "\n", "Now let’s make a change and add an exclamation point to our Hello world:\n", "\n", "`print(“Hello world!”)`\n", "\n", "You will notice a dot appearing next to your file name, indicating that you have some unsaved changes. To automatically save all your changes as soon as you make them, navigate to File > Auto Save.\n" ] }, { "cell_type": "markdown", "id": "09e34a6e", "metadata": {}, "source": [ "## Tips and tricks\n", "\n", "Mastering a few key shortcuts in VS Code can make coding faster and smoother, for example:\n", "\n", "- Commenting code: Press `Ctrl` + `/` (or `Cmd` + `/` on macOS) to comment or uncomment the selected line or block of code. This works for both single and multiple lines.\n", "\n", "- Indenting: Press `Tab` to indent an entire paragraph or block of selected code. Use `Shift` + `Tab` to unindent.\n", "\n", "- Wrapping in brackets: Highlight any text or code, then press an opening bracket like `(`, `{`, `[`, or `\"`. VS Code will automatically wrap the entire selection with matching brackets or quotes.\n", "\n", "For more useful keyboard shortcuts, visit the VS Code page for [Windows](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf), [macOS](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf), or [Linux](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf)." ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.18" } }, "nbformat": 4, "nbformat_minor": 5 }