Replies: 4 comments 2 replies
-
|
This is something that I'd really love to see too. My use case is almost identical. |
Beta Was this translation helpful? Give feedback.
-
|
I had the same need too. Then i spotted #1007 and figured i could use that. |
Beta Was this translation helpful? Give feedback.
-
|
I vibe-coded a wrapper to Zoxide that works with my employer’s monorepo workspaces (like worktrees). If you are in /workspaces/foo/ and select /workspaces/bar/somedir, then the tool will cd to /workspaces/foo/somedir instead. If you are outside of any workspace, the tool will show FZF to select a workspace. You should be able to modify it to whatever workflow you want: The commands are zc and zci: function _zc_get_target_workspace -d "Determines the target workspace"
set -l current_dir $argv[1]
set -l ws_prefix $argv[2]
if string match --quiet "$ws_prefix*" "$current_dir"
set -l path_without_prefix (string trim --chars=/ (string sub --start (string length "$ws_prefix") "$current_dir"))
set -l parts (string split / "$path_without_prefix")
echo $parts[1]
return 0
else
# Not in a workspace, ask user to choose a workspace
set -l chosen_workspace (ls -1 "$ws_prefix" | fzf-tmux)
if test $status -ne 0 -o -z "$chosen_workspace"
echo "No workspace selected." >&2
return 1
end
echo $chosen_workspace
return 0
end
end
function _zc_helper
set -l current_dir (readlink -f .)
set -l zoxide_args $argv
set -l ws_prefix /workspaces
set -l result (command zoxide query --exclude "$current_dir" $zoxide_args)
if test $status -ne 0 -o -z "$result"
return 1
end
set result (readlink -f "$result")
if not string match --quiet "$ws_prefix*" "$result"
# Result is not in a workspace, cd directly
__zoxide_cd "$result"
return $status
end
set -l result_path_without_prefix (string trim --chars=/ (string sub --start (string length "$ws_prefix") "$result"))
set -l result_parts (string split / "$result_path_without_prefix")
set -l result_suffix (string join / $result_parts[2..-1])
set -l target_workspace (_zc_get_target_workspace "$current_dir" "$ws_prefix")
if test $status -ne 0
return 1
end
set -l final_path "$ws_prefix$target_workspace/$result_suffix"
__zoxide_cd "$final_path"
return $status
end
function zc
_zc_helper $argv
end
function zci
_zc_helper --interactive $argv
end |
Beta Was this translation helpful? Give feedback.
-
Here is the concrete workaround/behavior I had in mind (in my dotfiles). The key idea is: keep normal zoxide scoring, then rewrite the selected path into the current Git worktree when that is possible. A small zsh wrapper could look like this: zwt() {
local current_root target root rel rewritten
current_root=$(git rev-parse --show-toplevel 2>/dev/null) || {
z "$@"
return
}
target=$(zoxide query --exclude "$PWD" -- "$@") || return
root=$(git -C "$target" rev-parse --show-toplevel 2>/dev/null) || {
cd "$target"
return
}
# If zoxide picked another worktree of the same repo, preserve the relative path.
if [[ "$(git -C "$root" rev-parse --git-common-dir 2>/dev/null)" == "$(git -C "$current_root" rev-parse --git-common-dir 2>/dev/null)" ]]; then
rel=${target#$root/}
rewritten="$current_root/$rel"
[[ -d "$rewritten" ]] && cd "$rewritten" && return
fi
cd "$target"
}How it works:
Neutral example:
One implementation detail: in a production version, the two This is slightly different from “prefer any match in the current worktree”: zoxide still picks the best-ranked match first, then same-repository matches are remapped into the current worktree when possible. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
So let's say I have a git repository in
~/src/project. I can use zoxide to easily jump to dirs within the project like~/src/project/packages/authand~/src/project/packages/design-systemby just doingz authandz design.However, let's say I create another worktree because I want to take a closer look at my coworker's PR but I don't want to disrupt my main directory. Now, I have checked out
feature/new-componentbranch to the directory~/src/project+new-component.With the way that zoxide currently works, when I go into that directory and run
z designit will take me to~/src/project/packages/design-system. What I would like would be greater git integration and awareness on the part of zoxide, so that it knew that because I'm in the other worktree that it should take me to~/src/project+new-component/packages/design-system.If you run zoxide out of a git directory, it should probably work exactly the same as now. But if you run zoxide within a git directory, it should be more aware of git worktrees. This would be a massive quality of live improvement for worktrees.
Beta Was this translation helpful? Give feedback.
All reactions