Thoughts, tips, and customization of fzf for old-school CLI enjoyers?
from sebastiancarlos@lemmy.sdf.org to linux@lemmy.ml on 12 Jun 16:28
https://lemmy.sdf.org/post/18138820

Hey,

As an avid CLI user, I always aimed to master non-interactive tools to perform most of my work, given that they are easy to use, create, extend, and connect.

However, I found myself dealing with software projects with many files (mostly under the yoke of corporate oppression; an ordeal which I endure to sustain myself, as most of those reading me do, and therefore I will not go further into this topic) and started to hit the limits of non-interactive tools to find and edit files. Indeed, I could go faster if I followed the temptation of monstrous IDEs, as I did in my innocent past.

I did not despair, as naturally I heard of the usefulness of interactive fuzzy finders such as fzf. After spending an afternoon evaluating the tool, I concluded that it indeed increases the complexity of my workflow. Still, this complexity is managed in a sensible way that follows the UNIX tradition.

I now ask you two general questions:

I also ask this very specific question:

#! /usr/bin/env bash

# gf: grep + fzf
# basically a wrapper for 'grep <ARGS> | fzf | cut -f 1 -d:'

# print usage on -h/--help
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    echo "Usage: gf <grep-args>"
    echo
    echo "~~~ that feel when no 'gf' ~~~"
    echo
    echo "- Basically a wrapper for 'grep <ARGS> | fzf | cut -f 1 -d:'"
    echo "- Opens fzf with grep results, and prints the selected filename(s)"
    echo "- Note: As this is meant to search files, it already adds the -r flag"
    echo
    echo "Example:"
    echo "  $ nvim \`gf foobar\`"
    echo "  $ gf foobar | xargs nvim"
    exit 0
fi

# run grep with arguments, pipe to fzf, and print the filename(s) selected
custom_grep () {
    grep -E --color=always --binary-files=without-match --recursive "$@"
}
remove_color () {
    sed -E 's/\x1b\[[0-9;]*[mK]//g'
}
custom_fzf () {
    fzf --ansi --height ~98%
}
grep_output=$(custom_grep "$@")
if [[ "$?" -ne 0 ]]; then
    exit 1
else
    echo "$grep_output" | custom_fzf | remove_color | cut -f 1 -d:
fi
#! /usr/bin/env bash

# ge: grep + fzf + editor
# basically a wrapper for 'grep <ARGS> | fzf | cut -f 1 -d: | $EDITOR'

# print usage on -h/--help
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    echo "Usage: ge <grep-args>"
    echo
    echo "- Basically a wrapper for 'grep <ARGS> | fzf | cut -f 1 -d: | \$EDITOR'"
    echo "- Opens fzf with grep results, and edits the selected file(s)"
    echo "- Note: As this is meant to search files, it already adds the -r flag"
    echo "- Note: Internally, it uses the 'gf' command"
    echo
    echo "Example:"
    echo "  $ ge foobar"
    exit 0
fi

# takes output from 'gf' and opens it in $EDITOR
grep_fzf_output=$(gf "$@")
if [[ -n "$grep_fzf_output" ]]; then
  $EDITOR "$grep_fzf_output"
fi

Have a wonderful day, you CLI cowboys.

#linux

threaded - newest

Kualk@lemm.ee on 12 Jun 18:00 next collapse

Fish is all I need for daily CLI. It is zero customization effort for me. Spend your time on productive side, not fzf your shell history.

fishshell.com/docs/current/tutorial.html

Keep bash as default root shell and just start fish manually when using root. It is for cases when linux panics on boot.

sebastiancarlos@lemmy.sdf.org on 12 Jun 18:02 collapse

And what’s your workflow when working with lots of files in projects with fish?

Kualk@lemm.ee on 14 Jun 00:45 collapse

I don’t get the question.

I usually use vscode to work with files. It has excellent remote editing over ssh. For example, I have large private collection of markdown notes that is kept on remote server.

At work I deal with large GO project that targets Docker images and my setup is:

  • windows 11 laptop
  • WSL Debian with full systemd integration installed (that’s the hardest part)
  • visual studio installed on windows, I have no development tools on windows: no docker, no git, no GO compiler
  • debian on WSL has all the dev tooling: git, go lang, ssh server is turned on

My workflow is to start Debian WSL and forget about terminal. Start vscode on windows, connect to Debian over ssh, open project directory. Work on project without ever leaving editor, use built in terminal in vscode. Fish runs inside vscode. Editor is primary. Fish is secondary and it excels at recalling history.

Use each tool for what it was designed. No terminal will ever match my productivity in vscode. Vscode has all the fuzzy search built-in.

I used to use vim for heavy coding, but abandoned that route 20 years ago. I am still able to use vim for quick short changes in config files, but anything serious is handled with visual studio code over ssh.

Primary vim scenario:

sudo vim /etc/config-file-name

Vscode 1st approach is a modern day version of emacs approach Or vim with plugins. Only difference is vscode is actually low effort to get started on new machine, low learning curve, low maintenance effort unless you have sunken months into your terminal editor and refuse to abandon your investment.

vvv@programming.dev on 12 Jun 18:10 next collapse

I use these two vim plugins for the same functionality without leaving $EDITOR:

I’ve also started dabbling with using fzf in scripts for the team to use. Don’t sleep on the –query and –select-1 flags!

sfera@beehaw.org on 14 Jun 20:51 collapse

I recently discovered fzf-git.sh and decided that I don’t use fzf enough. For now I am experimenting with using fzf to switch git branches, to see if it integrates with my git workflow. If it proves helpful, then I’ll investigate further options.