2024-07-10 16:20:52 +00:00
# REPL
2024-07-15 18:55:49 +00:00
## Getting started
2024-07-10 16:20:52 +00:00
2024-07-24 07:50:23 +00:00
Bring 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.
2024-07-10 16:20:52 +00:00
2024-09-06 16:43:54 +00:00
< figure style = "width: 100%; margin: 0; overflow: hidden; border-top-left-radius: 2px; border-top-right-radius: 2px;" >
2024-07-25 23:05:04 +00:00
< video loop controls playsinline >
< source
src="https://customer-snccc0j9v3kfzkif.cloudflarestream.com/aec66e79f23d6d1a0bee5e388a3f17cc/downloads/default.mp4"
type='video/webm; codecs="vp8.0, vorbis"'
/>
< source
src="https://customer-snccc0j9v3kfzkif.cloudflarestream.com/aec66e79f23d6d1a0bee5e388a3f17cc/downloads/default.mp4"
type='video/mp4; codecs="avc1.4D401E, mp4a.40.2"'
/>
< source
src="https://zed.dev/img/post/repl/typescript-deno-kernel-markdown.png"
type="image/png"
/>
< / video >
< / figure >
2024-07-10 16:20:52 +00:00
2024-07-15 18:55:49 +00:00
## Installation
2024-07-10 16:20:52 +00:00
2024-07-15 18:55:49 +00:00
Zed supports running code in multiple languages. To get started, you need to install a kernel for the language you want to use.
2024-07-10 16:20:52 +00:00
2024-07-15 18:55:49 +00:00
**Currently supported languages:**
2024-07-27 18:52:21 +00:00
- [Python (ipykernel) ](#python )
- [TypeScript (Deno) ](#typescript-deno )
2024-08-09 17:37:54 +00:00
- [R (Ark) ](#r-ark )
- [R (Xeus) ](#r-xeus )
- [Julia ](#julia )
- [Scala (Almond) ](#scala )
2024-07-15 18:55:49 +00:00
2024-07-25 21:26:14 +00:00
Once 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.
2024-07-15 18:55:49 +00:00
## Using the REPL
2024-07-25 21:26:14 +00:00
To 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.
2024-07-15 18:55:49 +00:00
The `repl: run` command will be executed on your selection(s), and the result will be displayed below the selection.
Outputs can be cleared with the `repl: clear outputs` command, or from the REPL menu in the toolbar.
2024-07-25 21:26:14 +00:00
### Cell mode
2024-07-24 17:31:25 +00:00
Zed 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.
The `repl: run` command will run each block of code between the `# %%` markers as a separate cell.
```python
# %% Cell 1
import time
import numpy as np
# %% Cell 2
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
```
2024-07-15 18:55:49 +00:00
## Language specific instructions
### Python {#python}
#### Global environment
< div class = "warning" >
2024-08-28 23:10:49 +00:00
On 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.
2024-07-15 18:55:49 +00:00
< / div >
2024-07-10 16:20:52 +00:00
To setup your current python to have an available kernel, run:
2024-08-09 17:37:54 +00:00
```sh
2024-07-15 18:55:49 +00:00
pip install ipykernel
2024-07-10 16:20:52 +00:00
python -m ipykernel install --user
```
2024-07-15 18:55:49 +00:00
#### Conda Environment
2024-07-10 16:20:52 +00:00
2024-08-09 17:37:54 +00:00
```sh
2024-07-10 16:20:52 +00:00
source activate myenv
conda install ipykernel
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
```
2024-07-15 18:55:49 +00:00
#### Virtualenv with pip
2024-07-10 16:20:52 +00:00
2024-08-09 17:37:54 +00:00
```sh
2024-07-10 16:20:52 +00:00
source activate myenv
pip install ipykernel
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
```
2024-08-09 17:37:54 +00:00
### R (Ark Kernel) {#r-ark}
2024-08-02 14:22:45 +00:00
Install [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:
2024-08-09 17:37:54 +00:00
```sh
2024-08-02 14:22:45 +00:00
ark --install
```
2024-08-09 17:37:54 +00:00
### R (Xeus Kernel) {#r-xeus}
- Install [Xeus-R ](https://github.com/jupyter-xeus/xeus-r )
- Install the R Extension for Zed (search for `R` in Zed Extensions)
<!--
TBD: Improve R REPL (Ark Kernel) instructions
-->
2024-07-15 18:55:49 +00:00
### Typescript: Deno {#typescript-deno}
2024-07-10 16:20:52 +00:00
2024-08-09 17:37:54 +00:00
- [Install Deno ](https://docs.deno.com/runtime/manual/getting_started/installation/ ) and then install the Deno jupyter kernel:
2024-07-10 16:20:52 +00:00
2024-08-09 17:37:54 +00:00
```sh
2024-07-23 18:03:22 +00:00
deno jupyter --install
2024-07-10 16:20:52 +00:00
```
2024-08-09 17:37:54 +00:00
<!--
TBD: Improve R REPL (Ark Kernel) instructions
-->
### Julia
2024-07-15 18:55:49 +00:00
2024-08-09 17:37:54 +00:00
- Download and install Julia from the [official website ](https://julialang.org/downloads/ ).
- Install the Julia Extension for Zed (search for `Julia` in Zed Extensions)
2024-07-10 16:20:52 +00:00
2024-08-09 17:37:54 +00:00
<!--
TBD: Improve Julia REPL instructions
-->
### Scala
- Install Scala with `cs setup` (Coursier): https://www.scala-lang.org/download/
- `brew install coursier/formulas/coursier && cs setup`
- REPL (Almond) Setup Instructions https://almond.sh/docs/quick-start-install
- `brew install --cask temurin` (Eclipse foundation official OpenJDK binaries)
- `brew install coursier/formulas/coursier && cs setup`
- `coursier launch --use-bootstrap almond -- --install`
2024-07-23 18:03:22 +00:00
## Changing which kernel is used per language {#changing-kernels}
Zed automatically detects the available kernels on your system. If you need to configure a different default kernel for a
language, you can assign a kernel for any supported language in your `settings.json` .
2024-07-27 18:52:21 +00:00
```json
2024-07-23 18:03:22 +00:00
{
"jupyter": {
"kernel_selections": {
"python": "conda-env",
"typescript": "deno",
2024-08-02 14:22:45 +00:00
"javascript": "deno",
"r": "ark"
2024-07-23 18:03:22 +00:00
}
}
}
```
2024-07-25 21:26:14 +00:00
## Debugging Kernelspecs
Available kernels are shown via the `repl: sessions` command. To refresh the kernels you can run, use the `repl: refresh kernelspecs` command.
2024-07-23 18:03:22 +00:00
If you have `jupyter` installed, you can run `jupyter kernelspec list` to see the available kernels.
2024-08-09 17:37:54 +00:00
```sh
2024-07-23 18:03:22 +00:00
$ jupyter kernelspec list
Available kernels:
ark /Users/z/Library/Jupyter/kernels/ark
conda-base /Users/z/Library/Jupyter/kernels/conda-base
deno /Users/z/Library/Jupyter/kernels/deno
python-chatlab-dev /Users/z/Library/Jupyter/kernels/python-chatlab-dev
python3 /Users/z/Library/Jupyter/kernels/python3
ruby /Users/z/Library/Jupyter/kernels/ruby
rust /Users/z/Library/Jupyter/kernels/rust
```
2024-07-25 21:26:14 +00:00
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.