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:
- Use the command wezterm cli list to list the panes.
- Check if there is any running instance of
hx
that shares the same current working directory as the file you want to open - If there is a matching instance, send the command
:open $file_path\r
to that pane to open the file - If no matching instance is found, call
hx
as usual
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
Related Posts:
- Turning Helix into an IDE with the help of WezTerm and CLI tools
- Debug Rust in Helix using lldb-vscode: display the contents of local string variables
- How to debug Rust in Helix?
- WezTerm: quickly select a command and open it in a new pane
- File tree workaround for Helix