Skip to content

Instantly share code, notes, and snippets.

@chooglen
Last active April 6, 2024 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chooglen/c76b2a6950aade154408fad2c42f97d7 to your computer and use it in GitHub Desktop.
Save chooglen/c76b2a6950aade154408fad2c42f97d7 to your computer and use it in GitHub Desktop.
Interactive `jj` with `fzf`
# Copyright 2022 Google LLC.
# SPDX-License-Identifier: Apache-2.0
export FZF_MOVEMENT="--bind='ctrl-n:preview-down' \
--bind='ctrl-p:preview-up'"
export FZF_DEFAULT_OPTS="$FZF_MOVEMENT"
jjl() {
_getCandidates="jj log $@"
_reload="toggle-preview+reload($_getCandidates)+toggle-preview"
_jjLogLineToCommitId="echo {} | grep -o '[a-f0-9]\{7,\} ' | sed '1q;d' | tr -d '\n'"
_jjLogLineToChangeId="echo {} | grep -o '[a-f0-9]\{7,\} ' | sed '2q;d' | tr -d '\n'"
_show="$_jjLogLineToCommitId | xargs -I % jj show --color=always %"
_show_git="$_jjLogLineToCommitId | xargs -I % jj show --color=always --git %"
_squash="$_jjLogLineToCommitId | xargs -I % jj squash -r %"
_edit="$_jjLogLineToCommitId | xargs -I % jj edit %"
_checkout="$_jjLogLineToCommitId | xargs -I % jj checkout %"
# Passing the --disabled flag disables the fuzzy-search, which lets you
# input whatever you want into the query e.g. you can use the query as a
# command builder for invocations like `jj rebase -s A -d B`.
eval $_getCandidates | \
fzf --ansi --no-sort --reverse --tiebreak=index --disabled \
--preview="$_show" \
--bind "ctrl-r:reload($_getCandidates)" \
--bind "ctrl-o:execute-silent:($_jjLogLineToCommitId | pbcopy)" \
--bind "ctrl-h:execute-silent($_jjLogLineToChangeId | pbcopy)" \
--bind "alt-s:execute-silent($_squash)+$_reload" \
--bind "alt-e:execute-silent($_edit)+$_reload" \
--bind "alt-c:execute-silent($_checkout)+$_reload" \
--bind "ctrl-c:cancel" \
--bind "ctrl-y:execute-silent(echo -n {q} | pbcopy)" \
--bind "enter:execute-silent(eval {q})+$_reload"
}
@tp-woven
Copy link

This is really useful, thanks!

A couple of changes I made, that may be useful for others:

  • For edit/checkout, I replaced reload($_getCandidates) with abort to exit fzf after checking out the new commit.
  • Added a binding for enter that does the same thing as alt-e for convenience.

@chooglen
Copy link
Author

Since the last comment, I've amended this to make it easier to build and run jj commands, e.g. adding enter to evaluate the current query. I use this as a long-running interactive jj client, so I intentionally avoid aborting this process.

@tp-woven
Copy link

I use this as a long-running interactive jj client, so I intentionally avoid aborting this process.

Yup, that makes sense, just made the comment in case someone finds that more useful. Regardless, I might also give it a try as a long-running client, see how that feels 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment