repl.md

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