3 ms·
fzf is such an amazing tool. Like yes it can do fuzzy search but it's so hackable and you can do so many unrelated things with it. Because it's also a tool tha
by schindlabua 3y ago
fzf is such an amazing tool. Like yes it can do fuzzy search but it's so hackable and you can do so many unrelated things with it.
Because it's also a tool that you can pipe an array of things into, select an element from it, and run a command on that element.
Just the other day I needed to scour the git history of a huge project, so I wrote a function that first selects a file by running fzf, and then lets me see the git history of any single line of that file by running fzf again.
gblame2 () {
local file=$(fzf)
cat "$file" | awk '{printf("%5d %s\n", NR, $0)}' | fzf --layout reverse --preview-window up --preview "echo {} | awk '{print \$1}' | xargs -I _ sh -c \"git log --color -L_,'+1:${file}'\""
}
chatgpt cooked that up for me in 5 minutes, saved me many many hours.
- overtomanu 3y agofound out that this has problems when file name has underscore (_) Better use some long rare string as place holder for xargs gblame2 () { local file=$(fzf) cat "$file" | awk '{printf("%5d %s\n", NR, $0)}' | fzf --layout reverse --preview-window up --preview "echo {} | awk '{print \$1}' | xargs -I __XZY__ sh -c \"git log --color -L__XZY__,'+1:${file}'\"" }