-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
open same file in already running instance of Helix
- Loading branch information
1 parent
0b1a23f
commit e5b531b
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
posts/2023/08/02/open-same-file-in-already-running-helix.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
title: "Open same file in already running instance of Helix" | ||
date: 2023-08-02 | ||
description: | ||
categories: | ||
- Development Environment | ||
tags: | ||
- helix | ||
- wezterm | ||
--- | ||
Recently, I came across [this issue](https://github.com/helix-editor/helix/issues/2177#issuecomment-1278123316): | ||
|
||
> 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](https://wezfurlong.org/wezterm/index.html) | ||
|
||
The proposed solution involves the following steps: | ||
|
||
- Use the command [wezterm cli list](https://wezfurlong.org/wezterm/cli/cli/list.html) 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: | ||
|
||
```sh | ||
#!/usr/bin/env sh | ||
|
||
tty=$(tty) | ||
hostname=$(hostname | tr '[:upper:]' '[:lower:]') | ||
pwd=$(pwd) | ||
file_path=$1 | ||
|
||
pane_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') | ||
if [ -z "$pane_id" ]; then | ||
~/.cargo/bin/hx $1 | ||
else | ||
if [ -n "$file_path" ]; then | ||
echo ":open ${file_path}\r" | wezterm cli send-text --pane-id $pane_id --no-paste | ||
else | ||
echo ":open ${pwd}\r" | wezterm cli send-text --pane-id $pane_id --no-paste | ||
fi | ||
wezterm cli activate-pane --pane-id $pane_id | ||
fi | ||
``` |