Getting Started with VS Code - jonathancolmer/lab-guide GitHub Wiki

Why We Use VS Code

VS Code is our lab’s default editor for code and reproducible research because it supports all our main languages (Python, R, Stata, LaTeX), integrates with Git for version control, and works across operating systems. This guide will walk you through installing VS Code, setting it up for our workflows, and adding the extensions we use most. It’s not exhaustive—VS Code has far more features—but it covers the essentials for getting productive quickly.


1. Installing VS Code

Download and install VS Code from the official website. Follow the instructions for your operating system:

  • macOS: Download the .dmg file, open it, and drag VS Code to the Applications folder.
  • Windows: Download the .exe file and follow the installation wizard.
  • Linux: Download the .deb or .rpm file and install it using your package manager.

2. Setting Up Your Workspace

VS Code organizes work using folders:

  • Open a folder as a workspace: File > Open Folder.
  • Use the integrated terminal: View > Terminal (Ctrl + ~).

3. Installing Extensions

Extensions enhance VS Code’s functionality. To install them:

  1. Open the Extensions panel: Activity Bar > Extensions Page
  2. Search for the extension name and click Install

We recommend the installing the following extensions:

  • Python
    • Developer: Microsoft
  • Python Debugger
    • Developer: Microsoft
  • PyLance
    • Developer: Microsoft
  • Jupyter
    • Developer: Microsoft
  • GitHub Copilot
    • Developer: GitHub
  • stataRun
    • Developer: Yeaoh Media Inc.
  • Stata Enhanced
    • Developer: Kyle Barron
  • R
    • Developer: REditorSupport
  • R Extension Pack
    • Developer: Yuki Ueda
  • ** R Debugger**
    • Developer: R Debugger
  • LaTeX Workshop
    • Developer: James Yu
  • ** Quarto**
    • Developer: Quarto
  • Rewrap Revived
    • Developer: Drew Nutter

Additional Information on managing extensions using VSCode’s extension marketplace can be found here.

4.Using GitHub Copilot

GitHub Copilot is an AI code assistant that can suggest code, functions, and documentation as you type. It can be especially useful for speeding up routine coding tasks, learning new libraries, and drafting boilerplate code.

If you have an .edu email address, you can get Copilot for free via the GitHub Student/Teacher Benefits Program.

  • Sign in to GitHub, apply for the education benefits, and once approved, enable Copilot in your GitHub account settings.
  • Use Copilot as a helper, not a source of truth -- always read and understand the code before running it. It is a productivity tool, not a shortcut for thinking.

5. Working with Git in VS Code

Version control is essential for collaborative projects, and VS Code has built-in Git support.

Getting Started with Git

  1. Initialize a Repository:
    • Open the Source Control panel (Ctrl+Shift+G).
    • Click Initialize Repository to track changes in your folder.
  2. Cloning an Existing Repository:
    • Open the Command Palette (Ctrl+Shift+P) and search for Git: Clone.
    • Enter the repository URL (e.g., https://github.com/your-org/repo-name.git).
    • Select the local folder where the repository should be saved.
    • Open the cloned folder in VS Code.
  3. Staging, Committing, and Pushing Changes:
    • Use the Source Control panel to stage (+), commit, and push changes.
    • To sync with GitHub, use Push to upload and Pull to retrieve updates.
  4. Additional Information:

6. Working with LaTeX in VS Code

VS Code supports LaTeX via the LaTeX Workshop extension. In order to run LaTeX in VSCode, you must have a locally installed compiler. Instructions can be found here.

Setting Up LaTeX

  1. Install the LaTeX Workshop Extension:
    • Open the Extensions panel (Ctrl+Shift+X).
    • Search for LaTeX Workshop and install it.
  2. Compiling LaTeX Documents:
    • Create or open a.tex file.
    • Click the Compile button (top-right of the editor, next to the play button).
  3. Cleaning Auxiliary Files:
    LaTeX generates auxiliary files such as .aux, .bbl, .blg, and .fls. To clean these automatically:
    • Go to Extensions > LaTeX Workshop > Settings
    • Find the setting Latex-workshop > Latex > Clean: Method and select glob.
    • Find the setting Latex-workshop > Latex > Auto Clean: Run and select onSucceeded.
  4. Additional Information:
    • This article more carefully details the steps to setup LATEX in VS Code.
    • This source provides further context and includes a section on how to integrate Git.

7. Working with Python in VS Code

VS Code is an excellent Python editor with support for virtual environments, debugging, and Jupyter notebooks.

Setting Up Python

  1. Install the Python Extension:
    • Open the Extensions panel (Ctrl+Shift+X).
    • Search for Python and install it.
  2. Create a Virtual Environment:
    • Open the Command Palette (Shift+Cmd+P) and type:
      Python: Create Environment
    • Select Venv as the environment type.
    • Choose your installed Python interpreter.

Running Python Code

  • Open a .py file and click the Run Python File button (green play icon).
  • Select code and press Shift+Enter to execute in the Python terminal.
  • Use Jupyter Notebooks by installing the Jupyter Extension.

Debugging Python Code

VS Code provides built-in debugging capabilities for Python using the Python Debugger Extension.

  1. Set Breakpoints: Click to the left of a line number in the editor to create a red circle (breakpoint).
  2. Start Debugging:
    • Open the Run and Debug panel (Ctrl+Shift+D).
    • Click Run and Debug (F5).
    • Select Python File as the debug configuration.
  3. Using the Debug Toolbar:
    • Continue (F5) - Resume execution until the next breakpoint.
    • Step Over (F10) - Move to the next line without entering functions.
    • Step Into (F11) - Enter a function call.
    • Step Out (Shift+F11) - Exit the current function.

8. Working with Stata in VS Code

VS Code supports Stata scripting via the stataRun and Stata Enhanced extensions. First, confirm that you have Stata installed on your local device, and determine the version (Eg. StataBE, StataSE, or StataMP). StataBE is not supported.

Setting up stataRun

  1. Under VSCode Settings, navigate to Extensions > stataRun
  2. [For Windows] Locate the filepath for the executable:
    • An example file path looks like this: C:\Program Files (x86)\Stata15\StataSE-64.exe
    • Paste it into the “Stata Path” section.
  3. [For MacOS] Determine the Stata Version and select the appropriate choice from the ‘Which App” dropdown.

Running a Stata Script

  1. Open the .do file in VS Code.
  2. Open the integrated terminal (Ctrl+~).

Run the Stata script using:
stata-mp -b do <your_script_name>.do

Helpful Keyboard Shortcuts (Replace ctrl with cmd on mac):

  • shift+ctrl+a: Run All Lines ==> This will run the entire file
  • shift+ctrl+s: Run Selection ==> To run a given selection. If no selection is made it default to Run All
  • shift+ctrl+c: Run Current Line ==> This will run the current line code
  • shift+ctrl+d: Run Downward Lines==> from current line to downward
  • shift+ctrl+f: Run Frontward Lines ==> from first line till current line

9. Working with R in VS Code

For R development, VS Code provides an integrated terminal and enhanced support via extensions.

Setting Up R in VS Code

  1. Use Radian for an Enhanced Console:
    • Install Radian via (in your R terminal): pip3 install radian
    • Open the settings.json (cmd+shift+P for mac users)
    • Type in the following command:
      “r.rterm.mac”: “/usr/local/bin/radian”
  2. For a better user experience, install the following packages: languageserver, httpgd, and lintr. In the R Console run the following commands: install.packages(“<package_name>”)

Running R Files

  • Create a new .R file via File > New File > R Document.
  • To run code:
    • Select code and press Ctrl+Enter to execute it in the R terminal.
    • To source an entire file, use Ctrl+Shift+S.

For additional help