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="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- [R (Ark)](#r)
 32- [TypeScript (Deno)](#typescript-deno)
 33
 34Once 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.
 35
 36## Using the REPL
 37
 38To 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.
 39
 40The `repl: run` command will be executed on your selection(s), and the result will be displayed below the selection.
 41
 42Outputs can be cleared with the `repl: clear outputs` command, or from the REPL menu in the toolbar.
 43
 44### Cell mode
 45
 46Zed 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.
 47
 48The `repl: run` command will run each block of code between the `# %%` markers as a separate cell.
 49
 50```python
 51# %% Cell 1
 52import time
 53import numpy as np
 54
 55# %% Cell 2
 56import matplotlib.pyplot as plt
 57import matplotlib.pyplot as plt
 58from matplotlib import style
 59style.use('ggplot')
 60```
 61
 62## Language specific instructions
 63
 64### Python {#python}
 65
 66#### Global environment
 67
 68<div class="warning">
 69
 70On 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.
 71
 72</div>
 73
 74To setup your current python to have an available kernel, run:
 75
 76```
 77pip install ipykernel
 78python -m ipykernel install --user
 79```
 80
 81#### Conda Environment
 82
 83```
 84source activate myenv
 85conda install ipykernel
 86python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
 87```
 88
 89#### Virtualenv with pip
 90
 91```
 92source activate myenv
 93pip install ipykernel
 94python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
 95```
 96
 97### R
 98
 99Install [Ark](https://github.com/posit-dev/ark/releases) by downloading the release for your operating system. E.g. for macOS just unpack `ark` binary and put it into `/usr/local/bin`. Then run:
100
101```
102ark --install
103```
104
105### Typescript: Deno {#typescript-deno}
106
107[Install Deno](https://docs.deno.com/runtime/manual/getting_started/installation/) and then install the Deno jupyter kernel:
108
109```
110deno jupyter --install
111```
112
113### Other languages
114
115The following languages and kernels are also supported. You can help us out by expanding their installation instructions and configuration:
116
117- [Julia (IJulia)](https://github.com/JuliaLang/IJulia.jl)
118- R
119  - [Ark Kernel](https://github.com/posit-dev/ark) - via Positron, formerly RStudio
120  - [Xeus-R](https://github.com/jupyter-xeus/xeus-r)
121- [Scala (almond)](https://almond.sh/docs/quick-start-install)
122
123## Changing which kernel is used per language {#changing-kernels}
124
125Zed automatically detects the available kernels on your system. If you need to configure a different default kernel for a
126language, you can assign a kernel for any supported language in your `settings.json`.
127
128```json
129{
130  "jupyter": {
131    "kernel_selections": {
132      "python": "conda-env",
133      "typescript": "deno",
134      "javascript": "deno",
135      "r": "ark"
136    }
137  }
138}
139```
140
141## Debugging Kernelspecs
142
143Available kernels are shown via the `repl: sessions` command. To refresh the kernels you can run, use the `repl: refresh kernelspecs` command.
144
145If you have `jupyter` installed, you can run `jupyter kernelspec list` to see the available kernels.
146
147```
148$ jupyter kernelspec list
149Available kernels:
150  ark                   /Users/z/Library/Jupyter/kernels/ark
151  conda-base            /Users/z/Library/Jupyter/kernels/conda-base
152  deno                  /Users/z/Library/Jupyter/kernels/deno
153  python-chatlab-dev    /Users/z/Library/Jupyter/kernels/python-chatlab-dev
154  python3               /Users/z/Library/Jupyter/kernels/python3
155  ruby                  /Users/z/Library/Jupyter/kernels/ruby
156  rust                  /Users/z/Library/Jupyter/kernels/rust
157```
158
159Note: 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.