"Life is all about sharing. If we are good at something, pass it on." - Mary Berry

Open same file in already running instance of Helix

2023-08-02

Categories: Development Environment

Recently, I came across this issue:

I find that I have occasionally (frequently!) made the mistake of two instances of helix in different terminals editing the same file. This causes me a problem, because I will then have changes in one of the instances overwrite changes that are made in another. The worst case is when I accidentally switch back to an instance that has an older version of the file open.

after some consideration, I believe this issue can be solved by using WezTerm

The proposed solution involves the following steps:

Here’s the script I’ve come up with:

 1#!/usr/bin/env sh
 2
 3tty=$(tty)
 4hostname=$(hostname | tr '[:upper:]' '[:lower:]')
 5pwd=$(pwd)
 6file_path=$1
 7
 8pane_id=$(wezterm cli list --format json | jq --arg tty "$tty" --arg opening_cwd "file://$hostname$pwd" --arg file_path "file://$hostname$file_path" -r '.[] | .cwd as $running_cwd | select((.tty_name != $tty) and (.title | startswith("hx")) and (($opening_cwd | contains($running_cwd)) or ($file_path | contains($running_cwd)))) | .pane_id')
 9if [ -z "$pane_id" ]; then
10    ~/.cargo/bin/hx $1
11else
12    if [ -n "$file_path" ]; then
13        echo ":open ${file_path}\r" | wezterm cli send-text --pane-id $pane_id --no-paste
14    else
15        echo ":open ${pwd}\r" | wezterm cli send-text --pane-id $pane_id --no-paste
16    fi
17    wezterm cli activate-pane --pane-id $pane_id
18fi

Tags: helix wezterm

Edit on GitHub

Related Posts: