repl.md

  1# REPL
  2
  3## Getting started
  4
  5Bring the power of [Jupyter kernels](https://docs.jupyter.org/en/latest/projects/kernels.html) to your editor! The built-in REPL for Zed allows you to run code interactively in your editor similarly to a notebook with your own text files.
  6
  7<figure style="width: 100%; margin: 0; overflow: hidden; border-top-left-radius: 2px; border-top-right-radius: 2px;">
  8    <video loop controls playsinline>
  9        <source
 10            src="https://customer-snccc0j9v3kfzkif.cloudflarestream.com/aec66e79f23d6d1a0bee5e388a3f17cc/downloads/default.mp4"
 11            type='video/webm; codecs="vp8.0, vorbis"'
 12        />
 13        <source
 14            src="https://customer-snccc0j9v3kfzkif.cloudflarestream.com/aec66e79f23d6d1a0bee5e388a3f17cc/downloads/default.mp4"
 15            type='video/mp4; codecs="avc1.4D401E, mp4a.40.2"'
 16        />
 17        <source
 18          src="https://zed.dev/img/post/repl/typescript-deno-kernel-markdown.png"
 19          type="image/png"
 20        />
 21    </video>
 22</figure>
 23
 24## Installation
 25
 26Zed supports running code in multiple languages. To get started, you need to install a kernel for the language you want to use.
 27
 28**Currently supported languages:**
 29
 30- [Python (ipykernel)](#python)
 31- [TypeScript (Deno)](#typescript-deno)
 32- [R (Ark)](#r-ark)
 33- [R (Xeus)](#r-xeus)
 34- [Julia](#julia)
 35- [Scala (Almond)](#scala)
 36
 37Once installed, you can start using the REPL in the respective language files, or other places those languages are supported, such as Markdown. If you recently added the kernels, run the `repl: refresh kernelspecs` command to make them available in the editor.
 38
 39## Using the REPL
 40
 41To start the REPL, open a file with the language you want to use and use the `repl: run` command (defaults to `ctrl-shift-enter` on macOS) to run a block, selection, or line. You can also click on the REPL icon in the toolbar.
 42
 43The `repl: run` command will be executed on your selection(s), and the result will be displayed below the selection.
 44
 45Outputs can be cleared with the `repl: clear outputs` command, or from the REPL menu in the toolbar.
 46
 47### Cell mode
 48
 49Zed supports [notebooks as scripts](https://jupytext.readthedocs.io/en/latest/formats-scripts.html) using the `# %%` cell separator in Python and `// %%` in TypeScript. This allows you to write code in a single file and run it as if it were a notebook, cell by cell.
 50
 51The `repl: run` command will run each block of code between the `# %%` markers as a separate cell.
 52
 53```python
 54# %% Cell 1
 55import time
 56import numpy as np
 57
 58# %% Cell 2
 59import matplotlib.pyplot as plt
 60import matplotlib.pyplot as plt
 61from matplotlib import style
 62style.use('ggplot')
 63```
 64
 65## Language specific instructions
 66
 67### Python {#python}
 68
 69#### Global environment
 70
 71<div class="warning">
 72
 73On macOS, your system Python will _not_ work. Either set up [pyenv](https://github.com/pyenv/pyenv?tab=readme-ov-file#installation) or use a virtual environment.
 74
 75</div>
 76
 77To setup your current Python to have an available kernel, run:
 78
 79```sh
 80pip install ipykernel
 81python -m ipykernel install --user
 82```
 83
 84#### Conda Environment
 85
 86```sh
 87source activate myenv
 88conda install ipykernel
 89python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
 90```
 91
 92#### Virtualenv with pip
 93
 94```sh
 95source activate myenv
 96pip install ipykernel
 97python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
 98```
 99
100### R (Ark Kernel) {#r-ark}
101
102Install [Ark](https://github.com/posit-dev/ark/releases) by downloading the release for your operating system. For example, for macOS just unpack `ark` binary and put it into `/usr/local/bin`. Then run:
103
104```sh
105ark --install
106```
107
108### R (Xeus Kernel) {#r-xeus}
109
110- Install [Xeus-R](https://github.com/jupyter-xeus/xeus-r)
111- Install the R Extension for Zed (search for `R` in Zed Extensions)
112
113<!--
114TBD: Improve R REPL (Ark Kernel) instructions
115-->
116
117### TypeScript: Deno {#typescript-deno}
118
119- [Install Deno](https://docs.deno.com/runtime/manual/getting_started/installation/) and then install the Deno jupyter kernel:
120
121```sh
122deno jupyter --install
123```
124
125<!--
126TBD: Improve R REPL (Ark Kernel) instructions
127-->
128
129### Julia
130
131- Download and install Julia from the [official website](https://julialang.org/downloads/).
132- Install the Julia Extension for Zed (search for `Julia` in Zed Extensions)
133
134<!--
135TBD: Improve Julia REPL instructions
136-->
137
138### Scala
139
140- [Install Scala](https://www.scala-lang.org/download/) with `cs setup` (Coursier):
141  - `brew install coursier/formulas/coursier && cs setup`
142- REPL (Almond) [setup instructions](https://almond.sh/docs/quick-start-install):
143  - `brew install --cask temurin` (Eclipse foundation official OpenJDK binaries)
144  - `brew install coursier/formulas/coursier && cs setup`
145  - `coursier launch --use-bootstrap almond -- --install`
146
147## Changing which kernel is used per language {#changing-kernels}
148
149Zed automatically detects the available kernels on your system. If you need to configure a different default kernel for a
150language, you can assign a kernel for any supported language in your `settings.json`.
151
152```json [settings]
153{
154  "jupyter": {
155    "kernel_selections": {
156      "python": "conda-env",
157      "typescript": "deno",
158      "javascript": "deno",
159      "r": "ark"
160    }
161  }
162}
163```
164
165## Debugging Kernelspecs
166
167Available kernels are shown via the `repl: sessions` command. To refresh the kernels you can run, use the `repl: refresh kernelspecs` command.
168
169If you have `jupyter` installed, you can run `jupyter kernelspec list` to see the available kernels.
170
171```sh
172$ jupyter kernelspec list
173Available kernels:
174  ark                   /Users/z/Library/Jupyter/kernels/ark
175  conda-base            /Users/z/Library/Jupyter/kernels/conda-base
176  deno                  /Users/z/Library/Jupyter/kernels/deno
177  python-chatlab-dev    /Users/z/Library/Jupyter/kernels/python-chatlab-dev
178  python3               /Users/z/Library/Jupyter/kernels/python3
179  ruby                  /Users/z/Library/Jupyter/kernels/ruby
180  rust                  /Users/z/Library/Jupyter/kernels/rust
181```
182
183> Note: Zed makes best effort usage of `sys.prefix` and `CONDA_PREFIX` to find kernels in Python environments. If you want explicitly control run `python -m ipykernel install --user --name myenv --display-name "Python (myenv)"` to install the kernel directly while in the environment.