4 ms·
Congrats for getting this out! > I also wanted an excuse to learn This is the best reason ever, and not just for you: seeing your GIF I thought "could I achie
by lloeki 4y ago
Congrats for getting this out!
> I also wanted an excuse to learn
This is the best reason ever, and not just for you: seeing your GIF I thought "could I achieve that with fzf?". Turns out, yes I can:
function fcd() {
local dir;
while true; do
# exit with ^D
dir="$(ls -a1p | grep '/$' | grep -v '^./$' | fzf --height 40% --reverse --no-multi --preview 'pwd' --preview-window=up,1,border-none --no-info)"
if [[ -z "${dir}" ]]; then
break
else
cd "${dir}"
fi
done
}
Certainly not the best code (and definitely not a jab at your implementation!) but, hey, it was purely for the heck of it.
Lately I've been trying to find the joy in programming again, these kind of fun little challenges help a lot, so thanks for sharing, enthusiasm and creativity is contaminating :)
- mgunyho 4y agoThanks for the kind words! :) While digging up alternatives (of course after I had already written most of the functionality), I briefly tried out fzf. I think at that time I couldn't find an example snippet like yours to do the cding, so I didn't look into it much more. With some basic settings, it was also not easy (or even possible?) to go up in the folder tree, but I see that your example handles that. If you're unfamiliar with a big folder tree, fzf (or another very similar tool that is designed for this purpose, broot) can be more efficient. But it might take a while for it to scan all subfolders.
- lloeki 4y ago> fzf (or another very similar tool that is designed for this purpose, broot) can be more efficient Well, fzf is only a selection tool, it's more about the tool that feeds fzf data ;) That's why I used a dumb ls in my example. Maybe a difference with yours is that it does perform cd on each loop, and there's no way to bail out and not cd. > it might take a while for it to scan all subfolders. Haha yeah, for recursive cases I was using ripgrep as fzf default command: rg --files --no-ignore --hidden --follow --glob "!{.git,node_modules}/*" | fzf And now I just discovered fd[0]: fd --no-ignore --hidden --follow --strip-cwd-prefix | fzf (...at least essentially, my setup is a bit more tunable and complex, I have a few of these with varying flags depending on the situation and goal) [0]: https://github.com/sharkdp/fd#using-fd-with-fzf https://github.com/sharkdp/fd#using-fd-with-fzf
- capdeck 4y agoWhat about `skim` - integrating it as a library? fzf is nice, but it is external - you have to pre-install and configure it.
- gregwebs 4y agoNice! exa (an attempt at a better ls) has a -D option, so then it is `exa -a1D` without the grep. I created a second version that also adds -R for searching the recursive directory tree with `exa -a1DR | grep : | cut -d ':' -f 1`
- nickjj 4y ago> could I achieve that with fzf? I've been sporting this alias for a while now: alias pf="fzf --preview='less {}' --bind shift-up:preview-page-up,shift-down:preview-page-down" You can run `pf` (preview file) in a directory and it opens a split window with fzf where you can preview text files with less and optionally filter down which files are matched with fzf. fzf is great.
- lloeki 4y agoN'other silly one: git log --oneline --decorate --color | fzf --ansi --preview 'git show $(echo {} | cut -d" " -f1)' The possibilities, they are _endless_ !!11!1!
- nickjj 4y agoIndeed! If you're using the fzf.vim plugin[0] you can run `:Commits` to do something similar to your command too. It adds a bit more detail such as when the commit was made in relative format and color codes the git diff. [0]: https://github.com/junegunn/fzf.vim https://github.com/junegunn/fzf.vim
- obituary_latte 4y agoFYI: I think you meant "contagious" rather than "contaminating". The latter means to make something impure whereas the former means more to spread something. :)
- deleted 4y ago[deleted]
- msravi 4y agoThis is great, thank you! Just a couple of minor changes to handle softlinks to folders... function fcd() { local dir; while true; do # exit with ^D dir="$(ls -a1F | grep '[/@]$' | grep -v '^./$' | sed 's/@$//' | fzf --height 40% --reverse --no-multi --preview 'pwd' --preview-window=up,1,border-none --no-info)" if [[ -z "${dir}" ]]; then break elif [[ -d "${dir}" ]]; then cd "${dir}" fi done }
- make3 4y agoyou should put this on github
- deleted 4y ago[deleted]
- flobosg 4y agoHere’s another one: # For fuzzy-jumping down your home directory. # See http://richardmavis.info/fuzzy-jumping function fcd { cache=~/.config/home-dirs-list if (( $# == 0 )); then if [ -e $cache ]; then cd "`fzf --height=10 < ${cache}`" else echo "No directory cache." cd `find ~ -type d | fzf --height=10` fi elif (( $# == 1 )); then if [[ $1 == "--cache" ]]; then echo "Making home directory cache." find ~ -type d | sort > $cache elif [ -e $cache ]; then cd "`cat ${cache} | grep $1 | fzf --height=10`" else echo "No directory cache." cd `find $1 -type d | fzf --height=10` fi else echo "Usage: fcd [DIR]|--cache" fi }
- user00012-ab 4y agoSimple bash to do mostly the same thing: bind -x '"\C-p": cd "$(dirname "$(fd | fzf )")"'