Visual Studio Code and Markdown

Matthew DeHaven

Course Home Page

2024-01-01

Lecure Goals

  • Become comfortable with VS Code’s interface
  • Introduce Markdown

Visual Studio Code

Writing Code

  • You can write code using any text editor.
  • Then you can run it from the command line:
Rscript some-code-script.r
  • And you’ll see the output, go back, make some edits, repeat.

But this is an awful way to code!

Integrated Development Environment (IDE)

Combines

  1. Editing and
  2. Running code

Usually you get some other perks:

  • Current environment variable view
  • Debugger
  • Autocomplete for specific coding languages
  • etc.

Some IDEs

Visual Studio Code

  • A free, lightweight editor
  • Lots of customization
  • Extensions to support many different programming languages
    • R, Python, Julia, LaTeX, Javascript, C++, Java, …

VS Code Welcome Screen

The welcome screen provides many useful quick links.

VS Code Welcome Screen

See that the welcome page is a tab and can be closed (leave it open for now).

VS Code Activity Bar

On the left-hand side, we have the activity bar, where we can toggle different views.

VS Code Explorer

The first is the Explorer, which shows files and subfolders, if you have a folder open.

VS Code Explorer Opened Folder

Opening a new folder called “temp”, we can see that it is empty.

VS Code Explorer New File

By clicking the “+ document” icon, we can create a new file and give it a name.

VS Code Explorer New Subfolder

By clicking the “+ folder” icon, we can create a new subfolder and give it a name.

VS Code Open a File

By clicking on a file, we can open it in the editor space.

VS Code Open Multiple Files

Creating a couple more files, we can see that we can open all three in the editor space.

VS Code Split Editor

Dragging the third file tab to the right hand side, we can create a split editor view.

VS Code Too Many Split Editors

You can split the editor many times.

VS Code New Window Editor

Drag a file off the window to create a new window (useful for multiple monitors).

VS Code Source Control

The third activity bar icon is the Source Control tool. Let’s initialize a repository with it.

VS Code Source Control Staging

It immediately notices all five of my files. Hit the “+” to stage them.

VS Code Source Control Committing

Write a useful message and hit the commit button.

VS Code Source Control Committed

If we had a linked Github repository, we would now see a “Sync Changes” button.

VS Code Debugger

The third activity bar icon is the Debugger. We won’t use this for now.

VS Code Extensions

The fourth activity bar icon is for Extensions. You should see the R extension you installed.

VS Code Extensions

This is where you can search for and install new extensions or uninstall them.

VS Code R

The final activity bar icon is for R. This was added when you installed the R extension.

VS Code R

It doesn’t show much at the moment, but if we add and run a simple R script…

VS Code R

It shows the variable x has the value 4. We will talk more about R next class.

VS Code Settings

At the bottom of the activity bar is a gear for Settings.

  • Command Palette... opens the command palette
  • Settings opens tab for all settings for VS Code
  • Keyboard Shortcuts customize keyboard shortcuts
  • Themes customize the theme (colors) for your VS Code

Command Palette

It contains all possible commands for VS Code.

This makes it very convenient.

To open…

  • ⇧+⌘+P (Windows, ⇧+ctrl+P)
  • Clicking “View” in the menu bar, then “Command Palette…”
  • Clicking the search bar, then “Show and Run Commands >”
  • Clicking the gear, then “Command Palette…”

You can almost always avoid using the command palette by clicking some buttons, but its easier to write directions that say run X command in the command palette than to say “click A, then B, then C”. In the long run its easier for you too!

Command Palette

Opens as an empty field at the top with a dropdown of recently used commands.

Command Palette

First, lets run Terminal: Create New Terminal.

New Terminal

First, lets run Terminal: Create New Terminal.

New Terminal

This opens a new pane at the bottom of the window with our terminal.

New Terminal

With this we can write commands to the computer, i.e. ls to list all the files and folders.

New Terminal

With this we can write commands to the computer, i.e. ls to list all the files and folders.

New R Terminal

Back to the Command Palette, let’s start a new R terminal.

New R Terminal

And this launches a new terminal at the bottom of the screen, but with R running.

New R Terminal

Notice that we now have a second tab on the right side of the terminal pane…

New R Terminal

If we drag that space bigger, we can see the labels for each tab. The “zzh” terminal is our one from earlier!

Switching Between Terminals

Click on a terminal to switch between them. Also notice the trash can to delete them.

Multiple R Terminals

You can create multiple R terminals. This is useful if you have code that takes a long time.

Auto-starting Terminals

If you don’t have any R terminals open and run some code, VS Code will start one for you.

Running R Code

You can always run an entire script by hitting the “Play” button at the top right of an R file.

Running R Code

You can always run an entire script by hitting the “Play” button at the top right of an R file.

I much prefer to run code line by line with:

  • ⌘+⏎ (Windows: ctrl+⏎)

You can also highlight many lines of code and run just those with the same shortcut.

VS Code Summary

Visual Studio Code provides us both (1) an editor and (2) a terminal.

Why VS Code instead of R Studio?

This is a good question. Most R users use Rstudio.

VS Code has…

  • Support for multiple languages
  • Better Git and Github integration
  • Support for multiple R terminals/sessions
  • Benefits with cloud/cluster computing later in course
  • Live collobaration (two people editing the same code)

I can’t enforce using VS Code for this class, but it is what I am assuming everyone is using.

VS Code Workspaces

Workspaces

Workspaces allow VS Code to…

  • Save what files are open when closed
  • Have workspace-specific settings (themes, extensions, etc.)
  • Set your working directory when launching a terminal

When you open a single “folder”, VS Code automatically treats that as a workspace.

You will often see “folder” and “workspace” used interchangeably in VS Code documentation because of this. Technically, you can have multiple folders open in a workspace, but we will not be doing that.

Workspace Example

Our practice VS Code folder “temp” was opened as a workspace.

Workspace Example

We can change settings just for a specific workspace.

Workspace Example

If we change a setting (say the theme) that is applied only to this workspace.

And a new “.vscode” folder is created to save these settings.

Workspace Summary

  • Folders you open in VS Code are workspaces
  • Workspaces set the working directory for terminals and code
  • If you see a “.vscode” folder, there are some workspace-specific settings

If you are coming from Rstudio, then workspaces are similar to R projects (.Rproj files).

Markdown

First, what are file extensions?

A basic text file has the extension “.txt”

This tells the computer that this file just has “plain” text

  • Just characters, numbers, punctuations, no formatting
Here is some plain text.
We could put this in a file with a ".txt" extension

Code Files

Most coding files also just store plain text.

  • “.r” “.py” “.jl” “.do”
## This could be saved as a ".txt"
print("Hello World")

Adding a file extension, like “.r”,

tells the computer that file is R code.

## This could be saved as a ".txt"
print("Hello World")

It’s useful to realize you can always open some code (say a Matlab “.m” file) as plain text without needing to install Matlab.

Changing a File Extension

You generally should not do this. You can break things.

But what if change the file extension of a “.pdf” to a “.txt”?

Well then we can open it in a text editor and see…

%PDF-1.5
%–‘≈ÿ
10 0 obj
<< /S /GoTo /D [11 0 R /Fit] >>
endobj
71 0 obj
<<
/Length 1322      
/Filter /FlateDecode
>>
stream
x⁄ÌZKo9æ˚WË¥∞Vı~Ï≠[§≈ÿn„‰VÏ!;yç̃I∞Ëø_R3í∆c'éùÿuà5¢HJ$?r§ôa‰ä0Ú©«:-áñC{F§‘IMÑîCʉ≤wº$¥qK©ˇ-ô_ëvwÙ≥¥F√óµGöÁj≈î ¢≥8RRÈ°‘*G™IÔéÙñÿ…

File Extensions Summary

File extensions tell the computer (and other people) what format the data in the file is using.

Some formats, like PDFs, PNGs, are very far from plain text.

Others, like coding files, are plain text

and are just telling the computer what syntax highlighting should be used and what software should be called to run it.

Markdown

Markdown is an “easy-to-read” and “easy-to-write” langauge for formatting plain text.

It is like LaTeX, but with an emphasis on easy to read code.

  • It’s file extension is “.md”

You’ll see this extension on Github’s Readme documents: “README.md”

Writing Markdown

Some plain text.

# Header 1 (biggest)

## Header 2

#### Header 4

*italicized text*

**bold text**

- A list
- A second item in a list

Writing Markdown

A basic reference guide

It’s also useful to know you can add links like so:

[A basic reference guide](https://www.markdownguide.org/cheat-sheet/)

Github Markdown

Github allows you to format some important documents with Markdown:

  • README files
  • Issues
  • Pull Requests

For today, we will be editing README files.

Summary

Summary

  • How to use VS Code
    • Open folders / workspaces
    • Create new files
    • Use multiple panes
    • Change settings
    • Launch terminals
    • Use source control
  • Basics of markdown

Live Coding Session