[HN Gopher] Improving Shell Workflows with Fzf
___________________________________________________________________
Improving Shell Workflows with Fzf
Author : todsacerdoti
Score : 154 points
Date : 2021-03-30 13:33 UTC (9 hours ago)
(HTM) web link (seb.jambor.dev)
(TXT) w3m dump (seb.jambor.dev)
| kbd wrote:
| Figure I'd link my git aliases here, that make _heavy_ use of
| fzf. The goal is generally to never have to type a filename (eg.
| for git add) or a commit hash (eg. for cherry-pick).
|
| Here's a link to my 'cp' alias that lets me choose a branch, then
| a commit to cherry pick into my current branch:
|
| https://github.com/kbd/setup/blob/e23b3e8e2363284c3c766c0be2...
| JulianWasTaken wrote:
| I have to read through yours which indeed look nice from a
| quick scan, but if your goal is firstly to save typing file
| paths, I presume you instead considered just having a shell
| mapping to do that instead of needing to instrument aliases for
| each command? Here's mine, which I get by hitting ctrl-s
| anywhere in any command line:
| https://github.com/Julian/dotfiles/blob/main/.config/zsh/com...
| kbd wrote:
| You can get similar behavior out of the box with fzf by
| typing `git add **[tab]`. The difference with what I do in my
| aliases is that I take the choices of files to add from the
| output of git status.
| JulianWasTaken wrote:
| Ah, interesting -- as in fzf wants you to integrate it with
| your shell's tab completion by default?
| kbd wrote:
| Yeah https://github.com/junegunn/fzf#files-and-
| directories
|
| Though I just remembered fzf maps ctrl+t by default as
| well (I never use it...)
|
| https://github.com/junegunn/fzf#key-bindings-for-command-
| lin...
| smartmic wrote:
| I just switched my time tracking to a fzf-based solution named
| tmt [1], also a great use case for fzf!
|
| [1] https://github.com/sitaramc/notes/blob/master/tmt
| seiferteric wrote:
| This is great. I created something vaguely similar a while back,
| but probably does not allow for as much complexity.
| https://github.com/seiferteric/clamp
| [deleted]
| IHLayman wrote:
| I use fzf in this very way and could have written this article if
| I weren't lazy lol. However, I can add two other use cases:
|
| 1. I use i3wm at home (haven't yet made the jump into
| Sway/Wayland), and don't want to change my screen resolution by
| opening an app, clicking with my mouse, and then waiting. So I
| use fzf for quick resolution changes using xrandr:
| cr() { xrandr | sed -En "s/^[
| ]+([0-9]{3,4}x[0-9]{3,4}0.\*/\1/p" | fzf | xargs -I{} xrandr
| --output HDMI-0 --mode {} }
|
| 2. At work I switch between multiple Kubernetes contexts during
| the day. To make the switch fast without typing a sentence, I
| will use this helper function: kc () {
| kubectl config get-contexts | tail -n +2 | fzf | cut -c 2- | awk
| '{print $1}' | xargs kubectl config use-context }
|
| I found this to be better than tab-completion because with tab
| completion I only get the name of the context, and not the
| cluster/user/namespace fields that make it easier to select the
| right one.
|
| Use the above at your own risk, as they are fast hack-ups and
| could be far more clear (my sed skills are prone to error and
| overly verbose).
|
| --Edited for HN formatting
| jpetrucc wrote:
| I would highly recommend looking at something like kubectx [0]
| to manage switching between kubernetes contexts! I've been
| using this for a while now and have it aliased to kx. It
| supports tab completion, as well as fzf integration if you have
| it installed [1]. There's also kubens in there which does the
| same for namespaces!
|
| [0]: https://github.com/ahmetb/kubectx
|
| [1]: https://github.com/ahmetb/kubectx#interactive-mode
| oblio wrote:
| Fzf is great, but unfortunately it doesn't have line wrap
| (there's an open bug basically marked as WONTFIX). So if you
| frequently work with long commands and want to use fzf to search
| for older commands with Ctrl-R, things will be quite complicated
| :-(
| bombcar wrote:
| Couldn't you "one time" process your history file to remove the
| line wraps (and make it appear that you ran a bunch of very
| long commands)?
| jhardy54 wrote:
| Alternatively, you can add fzf to the <TAB> menu and have it
| available anywhere that you have tab-completion:
| https://github.com/Aloxaf/fzf-tab
|
| (I use this and it works great.)
| aisamu wrote:
| This is wonderful and works better than what I expected!
| notyourday wrote:
| My only constant usage pattern for this is integration with 10000
| last unique lines of Redis stored history across all the systems
| I use. Push every unique command into a single Redis across all
| the systems, Ctrl-R to pull it via fzf. It really improves
| command line performance.
| JulianWasTaken wrote:
| These are great.
|
| I'll throw in a fifth, which is I've slowly been cobbling
| together a simple fuzzy CLI music player (though I use fzy rather
| than fzf but same idea).
|
| Code is here:
| https://github.com/Julian/dotfiles/tree/main/.config/zsh/fun...
|
| Look at say `artist` or `play` or `shuffle` which are the music
| ones.
| stsewd wrote:
| If you use vim, I created this plugin to manage branches using
| fzf https://github.com/stsewd/fzf-checkout.vim
| habitue wrote:
| If anyone is interested in a rust version (probably the main
| benefit being you can use it as a rust library) there is a
| similar tool called skim[1]
|
| It's actively developed and seems to have a non-overlapping set
| of features with fzf other than the basics (they both have
| features the other doesn't, and skim copies most of the core ui
| from fzf)
|
| [1] https://github.com/lotabout/skim
| ducktective wrote:
| Last I heard, skim's fuzzy search was not "as good as" fzf (I
| don't know in what measure?)...
|
| And there were talks of how skim could not utilize concurrency
| as efficiently as fzf and that has something to do with how
| Rust handles concurrency and how Go does it...
|
| These were just the result of me skimming (ha!) over internet
| posts so I'd be curious if anyone could elaborate on them...
| bcgraham wrote:
| Fzf can be a tremendous time-sink because its credible promise to
| improve your quality of life in the terminal.
|
| One of my most-frequently-used pry customizations is a keybinding
| that opens the current backtrace in fzf, with the preview window
| showing the selected location, allowing me to quickly walk up or
| down the stack, with tons of context. `enter` takes me to that
| frame, and `ctrl-v` opens that file location in vim.
| ghostpepper wrote:
| I'd love to hear more about this - is it a gdb integration?
| bcgraham wrote:
| The vim integration piece uses the same concepts as the gdb
| integration. The rest uses some (very ugly) local scripts,
| cobbled together in 10-minute snatches of time over years.
| Hopefully, the functionality of the pieces not included here
| can be inferred from the names.
|
| https://gist.github.com/bcgraham/f88e7e500e1b933bed1c7077f86.
| ..
|
| I don't have the time now to clean it up, unfortunately.
| Before you judge too harshly, let him without sin cast the
| first stone.
| RBerenguel wrote:
| I also have a "create feature branch from JIRA issue" (it was in
| the list of ideas in this post I wrote for the engineering blog
| at work [1] about autocompleting EC2 instances, although I was
| already using it). The JIRA API is a bit too slow for fast
| response, so what I do is populate a text file via a cron job
| that requests from the API regularly, and use the text in the
| file for the completion. [1]:
| https://engineering.hybridtheory.com/aws/devops/2018/08/15/fzf-
| autocompletions/
| RBerenguel wrote:
| Forgot to add that at some point I had a similar autocompleter
| to the one in the blog above, that would connect to N chosen
| machines (using the _multiple_ fzf setting), pane them in a
| tmux session automatically and thus allow you to send the same
| command to all of them. I only used it twice, but was kind of
| fun to write.
| corytheboyd wrote:
| Nice, I love fzf! It's an amazing recursive search replacement,
| but my favorite use was a script I wrote for changing git
| branches, where they were sorted by most recently changed so that
| the branch you're probably looking for is only a few lines away,
| but you can go back pretty far too if you want. It feels like tig
| but for changing branches, it's awesome.
| donquichotte wrote:
| Some excellent scripts, I like the one with `--multi` to delete
| git branches.
|
| My most powerful bash alias has become this one [1], where `subl`
| is the editor of my choice. I no longer search files, I find
| them. Really, fzf is almost indistinguishable from magic and has
| become crucial to my workflow.
|
| [1] alias fzubl='fzf -m | xargs subl'
| petepete wrote:
| If you use fzf-vim, pressing `alt+a` in Ag or Rg mode will mark
| all of your matches, pressing enter will them open them all in
| a quickfix list.
|
| Amazing trick, I use it frequently.
| jeromenerf wrote:
| Junegunn, the fzf author, makes great vim plugins too. Both the
| user experience and the developer experience (the code of these
| plugins) feel just right.
|
| Fzf is better integrated in vim than in emacs. So I use dmenu on
| X/wofi on wayland instead, which are basically GUI fzf
| replacement.
| ducktective wrote:
| Both fzf and dmenu are "selector menu" utilities, in turn, for
| terminal and X, but for dmenu, fuzzy search is not its main
| thing. It has a fuzzy patch though...
| tdfirth wrote:
| Those are very creative use cases for fzf, I had never considered
| doing something like that.
|
| My usage of it probably counts as quite basic. That said, I use
| it constantly. I just love the feeling of real time interaction
| it gives - it is well and truly part of my muscle memory now.
|
| I use the vim plugin too, which is also brilliant and makes
| navigating larger projects very quick and easy. I use these two
| extra bindings all the time: nnoremap <silent>
| <C-Space> yiw:Rg <C-r>"<CR> vnoremap <silent> <C-Space>
| y:Rg <C-r>"<CR>
|
| They're very simple, but when combined with the ability to add
| search items to the quickfix list it makes for a pretty powerful
| workflow.
|
| The first one in particular gives a great approximation of symbol
| search that 'just works' anywhere.
| lelandbatey wrote:
| Yeah, the ability to fake a "show all references to this
| identifier" via integrated ':Rg' is the greatest feature of FZF
| I've encountered yet, and makes for a passable substitute of
| one language-server must-haves.
| petr_tik wrote:
| I concur.
|
| I saw this tool to find pytest cases after I implemented my own
| simple fzf shell function that uses bat for preview.
| https://github.com/dbaty/testfinder
|
| I also implemented simple bash functions to:
|
| + Select a CMake target to build
|
| Extract all add_executables defined in the CMakeLists of my
| project directory and pipe the fzf selection to cmake to build
|
| + Find and sort (by mtime) all compiled executables placed in the
| cmake build directory
|
| It can be hard to remember the path of the executable produced by
| cmake, so i find all executable files in the cmake build dir, ls
| -alh, sort them by mtime (you want to execute the most recently
| compiled binary) and pipe to fzf
|
| Any time I pipe something to grep, I think if this could become
| an fzf-based command. Highly recommend people do the same with
| their workflows.
| xcambar wrote:
| I love fzf and tend to use it whenever repeatitive, non-static
| behavior is in play.
|
| Replacing Ctrl-R with fzf is the most basic and most useful
| usage.
|
| Basically every git command involving history, branches etc have
| fzf wrappers in my setup.
|
| My latest, least common usage is having built a wrapper around
| KeepassX and keepassx-cli to access my secrets from the CLI with
| fzf to fuzzy search entries. Saves me a lot of time every day.
|
| Thanks junegunn for theamazimg tool!
| CGamesPlay wrote:
| I think the first example should have been done with direnv
| rather than having a manual step, but I supposed to each their
| own. The other examples are all pretty neat.
|
| I recently set up fasd, which I recommend, but I tied it together
| with fzf, which makes it really nice.
|
| fasd: https://github.com/clvv/fasd
|
| tie it with fzf:
| https://github.com/CGamesPlay/dotfiles/blob/master/files/.co...
| cube2222 wrote:
| fzf is also great as a live preview window for a command:
| echo '' | fzf --print-query --preview-window wrap --preview 'cat
| test.json | jql {q}'
|
| Here shown with jql[0] to write json queries and see the live
| output.
|
| [0]: https://github.com/cube2222/jql
| akavel wrote:
| You might like to also check out https://github.com/akavel/up
| when in need for some more complex filtering with live preview
___________________________________________________________________
(page generated 2021-03-30 23:01 UTC)