📜 ⬆️ ⬇️

Fzf Fuzzy search or how to quickly put npm packages and kill processes


I work on MacOS, almost never use Finder and spend all my time on the console. That is why I try to make work from the console as convenient as possible.


Relatively recently, the FZF utility caught my eye. And a week later became irreplaceable.


FZF presents the possibility of a fuzzy search in the style of UNIX: it can quickly and relatively well search the lines that are given to it at the entrance, and integrate with my other favorite programs.


I was surprised to find that about this program there is not a single article on Habré, except for some mentions in passing . I decided to fill this gap. If you already know about FZF, then the article most likely will seem to you uninformative, and everyone else is welcome.


Switch to the correct branch in git


We in Wrike for each task must be created its own branch. In the name of the branch there is a ticket number. Want to switch - remember the name and number. Or search. I am looking for something like this:


git branch -a | grep ___ 

Only instead of grep I use ripgrep


  git branch -a | rg ___ 

But every time typing this long team is lazy. Yes, and instead of angular, I often get agnular. In short, one disorder. Therefore, it is enough to prescribe a simple function in .zshrc or .bashrc (taken from the documentation).


 fbr() { local branches branch branches=$(git branch --all | grep -v HEAD) && branch=$(echo "$branches" | fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) && git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##") } 

And now you can search for all branches. It looks like this:



Kill the process


Approximately as it is possible to kill the hung up process. Only the function will now be called fkill


 fkill() { local pid pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}') if [ "x$pid" != "x" ] then echo $pid | xargs kill -${1:-9} fi } 


Installing the desired npm package


FZF supports multiple choice. That is, using the Tab button, you can select a line and send all selected output to the output (if, of course, you run FZF with the -m flag).


I use the all-the-package-names package to load all the package names that are in the official npm package registry:


 npm -gi all-the-package-names 

Further I write absolutely simple function:


 fnpm() { local packages packages=$(all-the-package-names | fzf -m) && echo "$packages" && npm i $(echo $packages) } 

And that's all. Now you can do something like this:



Other examples


There are a lot of useful examples in the documentation .


Alternatives


On github, I found two more utilities that do roughly the same thing:
GitHub - jhawthorn / fzy: A better fuzzy finder
GitHub - calleerlandsson / pick: A fuzzy search tool for the command-line


Instead of output


If you have eaten a dog in writing scripts under Bash or Zsh, then the article most likely will seem naive to you. You may even know how to improve the examples I gave. Or maybe you have been using FZF for a long time at home, and you have something to say.


If so, then write in the comments and I will update and supplement the article with your examples and comments.


')

Source: https://habr.com/ru/post/344770/


All Articles