4 ms·
I love how people share their fzf scripts whenever a new post comes along. I'll share some of my own. First, some env variables for setting defaults: expo
by bilalq 4y ago
I love how people share their fzf scripts whenever a new post comes along. I'll share some of my own.
First, some env variables for setting defaults:
export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
export FZF_DEFAULT_OPTS='--layout=reverse --inline-info'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
And some neat commands:
1. A command for fuzzy jumping between workspaces with tree previews (depends on `tree`):
export WORKSPACE_ROOT="$HOME/whatever-your-root-is"
ws() {
cd "$WORKSPACE_ROOT/`ls -a $WORKSPACE_ROOT | fgrep -v .DS_Store | fzf --preview '(cd $WORKSPACE_ROOT/{1}; pwd; tree -C -L 1 -I node_modules -I .DS_Store)'`"
}
2. A command to be able to fuzzy search files with previews and syntax highlighting. Depends on bat being installed.
alias fzp="fzf --preview 'bat --style=numbers --color=always --line-range :500 {}' --border --height='80%'"
3. A command to fuzzily run npm scripts with previews and syntax highlighting. Depends on bat and gojq, but you can sub gojq with jq. It does have a bug where it doesn't handle ":" characters in script keys well, but I'll fix that at some point.
npz() {
local script
script=$(cat package.json | gojq -r '.scripts | keys[] ' | fzf --preview 'cat package.json | gojq -r ".scripts | .$(echo {1})" | bat -l sh --color always --file-name "npm run $(echo {1})" | sed "s/File: /Command: /"') && npm run $(echo "$script")
}
4. A command for rapidly selecting an AWS profile. As a user of AWS SSO with many accounts/roles, this is a life-saver. I combine this with having my prompt show the currently selected role for added value.
alias aws-profile='export AWS_PROFILE=$(sed -n "s/\[profile \(.*\)\]/\1/gp" ~/.aws/config | fzf)'
alias ap="aws-profile"
5. A command for fuzzily finding and tailing an AWS cloudwatch log group. I didn't come up with this one, and I can't remember where I read about it, or I'd attribute it properly.
awslogs() {
export AWS_PROFILE=$(cat ~/.aws/config | awk '/^\[profile /{print $2}' | tr -d ']' | fzf)
local log_group=$(aws logs describe-log-groups | gojq -r '.logGroups[].logGroupName' | fzf)
aws logs tail "$log_group" --since 3h --follow --format=short
}
I also have a few other commands related using fzf, but they're more bespoke and probably not useful to others.
- bloopernova 4y agoThat AWS logs one is fantastic.