|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- howto |
| 4 | +date: '2026-03-09T12:29:09Z' |
| 5 | +draft: false |
| 6 | +tags: |
| 7 | +- howto |
| 8 | +- personal |
| 9 | +- kitty |
| 10 | +- yazi |
| 11 | +- terminal |
| 12 | +- macos |
| 13 | +title: Open a tab in an existing yazi instance |
| 14 | +type: blog |
| 15 | +--- |
| 16 | +Recently I have been trying to switch to a terminal only workflow. I am doing this partly for fun, but honestly the terminal is the fastest way to work with a computer once you get the hang of it. |
| 17 | + |
| 18 | +Because of this shift I am using Yazi [[1]](https://yazi-rs.github.io/). Yazi is a blazing fast terminal file manager written in Rust that offers asynchronous operations and a highly customizable interface. |
| 19 | + |
| 20 | +## The problem with multiple windows |
| 21 | + |
| 22 | +I wanted to integrate Yazi with the other tools on my macOS system. However I ran into a small annoyance. Every time I triggered a folder to open from another application it launched a completely separate instance of Yazi. I was looking for a way to open a new tab within an existing Yazi window instead of spawning a new process every single time. |
| 23 | + |
| 24 | +## The solution using DDS |
| 25 | + |
| 26 | +It turns out that Data Distribution Service is the way to go. Yazi uses this internally as a publish and subscribe messaging system between its instances. |
| 27 | + |
| 28 | +When you launch Yazi normally it automatically generates its own internal ID. This is exposed inside a Yazi subshell as the environment variable `$YAZI_ID`. But you can also use the `--client-id` flag. This flag lets you define that ID in advance so your external scripts can reference it easily. It is important to note that this ID is not a standard process ID. It is a Data Distribution Service subscriber ID. |
| 29 | + |
| 30 | +You can define your ID in a few ways. For example you can drop it right into your `.zshrc` file. Here is how you can set it up and send commands to it: |
| 31 | + |
| 32 | +```sh |
| 33 | +export YA_ID="$(date +%s)$RANDOM" |
| 34 | +# or just hardcode it |
| 35 | +export YA_ID="123456" |
| 36 | + |
| 37 | +yazi --client-id "$YA_ID" |
| 38 | + |
| 39 | +# then from elsewhere: |
| 40 | +ya emit-to "123456" ~/Downloads |
| 41 | +ya emit-to "123456" tab_create ~/Cloud |
| 42 | +``` |
| 43 | + |
| 44 | +## Bringing it all together with Kitty |
| 45 | + |
| 46 | +As an extra touch I wanted to bring my Kitty terminal to the front automatically using `osascript` on macOS. I wrote a quick script to handle the logic. It tries to open a tab in the existing instance. If that succeeds it brings Kitty into focus. If it fails because Yazi is not running yet it launches a fresh instance. |
| 47 | + |
| 48 | +```sh |
| 49 | +#!/bin/bash |
| 50 | +MY_ID=12345678 |
| 51 | +FOLDER="${1:-$HOME}" |
| 52 | + |
| 53 | +if ya emit-to "$MY_ID" tab_create "$FOLDER" 2>/dev/null; then |
| 54 | + # emit succeeded so the instance with that ID is running |
| 55 | + osascript -e 'tell application "System Events" to set frontmost of process "kitty" to true' |
| 56 | +else |
| 57 | + # emit failed so there is no instance with that ID, launch one |
| 58 | + kitty yazi --client-id "$MY_ID" "$FOLDER" & |
| 59 | +fi |
| 60 | +``` |
| 61 | + |
| 62 | +This setup makes my workflow feel incredibly smooth. I get the speed of Yazi without cluttering my screen with a dozen different file manager windows. |
| 63 | + |
| 64 | +## References |
| 65 | + |
| 66 | +[1] Yazi - <https://yazi-rs.github.io/> |
0 commit comments