> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/sxyazi/yazi/llms.txt
> Use this file to discover all available pages before exploring further.

# emit & emit-to

> Send commands to Yazi instances

## Overview

The `emit` and `emit-to` commands allow you to send commands to running Yazi instances. This enables automation, remote control, and integration with other tools.

## emit

Emit a command to be executed by the current Yazi instance.

### Synopsis

```bash theme={null}
ya emit <NAME> [ARGS]...
```

### Arguments

<ParamField path="NAME" type="string" required>
  Name of the command to execute.

  This should be a valid Yazi command name (e.g., `cd`, `refresh`, `search`, `select`).
</ParamField>

<ParamField path="ARGS" type="string[]" optional>
  Arguments to pass to the command.

  Arguments can include hyphens and are passed directly to the command handler.

  ```bash theme={null}
  # Command with arguments
  ya emit cd --path="/home/user/Downloads"

  # Multiple arguments
  ya emit select --items=file1.txt --items=file2.txt
  ```
</ParamField>

### Examples

```bash theme={null}
# Navigate to parent directory
ya emit leave

# Navigate to a specific path
ya emit cd --path="/etc"

# Refresh the current directory
ya emit refresh

# Toggle selection of current item
ya emit toggle

# Search for files matching pattern
ya emit search --pattern="*.conf"

# Quit Yazi
ya emit quit
```

### Use Cases

#### Automation Scripts

```bash theme={null}
#!/bin/bash
# Automated file organization

ya emit cd --path="$HOME/Downloads"
ya emit search --pattern="*.pdf"
ya emit select-all
ya emit copy
ya emit cd --path="$HOME/Documents/PDFs"
ya emit paste
```

#### Integration with Other Tools

```bash theme={null}
# Trigger Yazi actions from external scripts
if [ -f "important.txt" ]; then
    ya emit reveal --path="important.txt"
fi
```

#### Shell Functions

```bash theme={null}
# Quick navigation helper
function ycd() {
    ya emit cd --path="$1"
}

ycd /var/log
```

## emit-to

Emit a command to be executed by a specific Yazi instance.

### Synopsis

```bash theme={null}
ya emit-to <RECEIVER> <NAME> [ARGS]...
```

### Arguments

<ParamField path="RECEIVER" type="number" required>
  Receiver instance ID.

  This is the client ID of the target Yazi instance (specified via `--client-id` when starting Yazi).

  ```bash theme={null}
  ya emit-to 12345 refresh
  ```
</ParamField>

<ParamField path="NAME" type="string" required>
  Name of the command to execute.
</ParamField>

<ParamField path="ARGS" type="string[]" optional>
  Arguments to pass to the command.
</ParamField>

### Examples

```bash theme={null}
# Send refresh command to instance 1000
ya emit-to 1000 refresh

# Navigate a specific instance
ya emit-to 1000 cd --path="/var/log"

# Control multiple instances
ya emit-to 1000 cd --path="/home/user"
ya emit-to 2000 cd --path="/etc"
ya emit-to 3000 cd --path="/var"

# Quit specific instance
ya emit-to 1000 quit
```

### Use Cases

#### Multi-Instance Orchestration

```bash theme={null}
#!/bin/bash
# Synchronize multiple Yazi instances to same directory

TARGET_DIR="/home/user/project"

for instance in 1000 2000 3000; do
    ya emit-to $instance cd --path="$TARGET_DIR"
done
```

#### Remote Control

```bash theme={null}
# Control Yazi instance from another terminal
INSTANCE_ID=12345

# Navigate to directory
ya emit-to $INSTANCE_ID cd --path="$HOME/Downloads"

# Trigger refresh
ya emit-to $INSTANCE_ID refresh
```

#### Build System Integration

```bash theme={null}
# Refresh file view after build completes
make build
if [ $? -eq 0 ]; then
    ya emit-to $YAZI_ID refresh
    echo "Build succeeded, view refreshed"
fi
```

## Command Serialization

Commands are serialized as JSON arrays with the command name followed by arguments:

```json theme={null}
["cd", "--path=/home/user"]
```

Arguments that contain special characters are properly encoded to handle:

* Unicode characters
* Spaces and special shell characters
* Binary data (converted to byte arrays)

## Error Handling

Both commands will exit with status code `1` and print an error message if:

* The target instance is not reachable
* The instance ID is invalid (for `emit-to`)
* The command fails to serialize
* DDS communication fails

Example error:

```bash theme={null}
ya emit-to 99999 refresh
# Cannot emit command: Connection refused
```

## Environment Variables

<ParamField path="YAZI_ID" type="string">
  Current Yazi instance ID. Set automatically by Yazi.

  Used by `emit` to determine the target instance.
</ParamField>

<ParamField path="YAZI_PID" type="string">
  Alternative to `YAZI_ID`. Contains the instance process ID.
</ParamField>

## See Also

* [ya](ya) - Main CLI utility
* [pub-sub](pub-sub) - Pub/sub messaging for custom events
* [yazi](yazi) - Start Yazi with specific client ID
