Using fzf and jq to edit chezmoi managed files #2631
Replies: 4 comments 6 replies
-
Nice! As an alternative to $ chezmoi managed --include=files |
Beta Was this translation helpful? Give feedback.
-
I have a similar solution, one that helps me manage a multi-layered chezmoi environment I've constructed for our team. At first I used grep with a hand-crafted regex to pick up 'best candidates' and if it was narrow enough, it would just open the editor with all the results. Then I started using
This helps me not only find the files to edit quickly, but it helps me keep the managed state tidy. CodeSince the code does a lot, and it's tied to my environment - I won't post all of it, but I'll share the
The key differences between my
|
Beta Was this translation helpful? Give feedback.
-
This is nice. I'm thinking through building something similar to this, bur with some differences for how I use to edit my dotfiles: Typically, I don't want to edit a single file for all of my dotfiles, but I want to edit the entire directory I started to manage with However I think there is no way to get from chezmoi the directories that were explicitly added, right? So I'll need to find a way to get the root folder of this type of dotfiles. Any thoughts are welcomed! |
Beta Was this translation helpful? Give feedback.
-
Thank you all for sharing your chezmoi fzf scripts. It was super helpful. Chezmoi has a plugin function that allows you to add custom commands. I modified the script above to make it a plugin. Simply put it in #!/bin/bash
# Based on the script from:
# https://github.com/twpayne/chezmoi/discussions/2631#discussioncomment-4457933
cfzf () {
file_path=$(chezmoi managed --include=files --exclude=externals | fzf --reverse --preview="cat {} | head -n 200" --query=$1)
if [ -z "$file_path" ]; then
>&2 echo "No file selected"
else
chezmoi edit --apply "$HOME/$file_path"
fi
}
cfzf $1 And I also created #!/bin/bash
# Capture the output of chezmoi status
output=$(chezmoi status)
# Check if the output is empty
if [ -z "$output" ]; then
echo "There are no differences."
else
# Use the captured output for fzf
echo "$output" | fzf --preview "chezmoi diff $HOME/{-1}"
fi |
Beta Was this translation helpful? Give feedback.
-
I wrote this small zsh function that gets a list of chezmoi managed config files, uses jq to parse them and then passes them to fzf. Once selection is made from fzf it is passed as a parameter to
chezmoi edit --apply
. If a query is provided (eg.cfzf zshrc
) and only there is only one match for it, it is automatically selected. I briefly tested it with bash and it seems to work fine.Suggestions for improvement welcome.
Beta Was this translation helpful? Give feedback.
All reactions