Skip to content

Instantly share code, notes, and snippets.

@AdamGagorik
Last active January 25, 2024 23:19
Show Gist options
  • Save AdamGagorik/64d2d0d563d9dab003c39bfa32787ca7 to your computer and use it in GitHub Desktop.
Save AdamGagorik/64d2d0d563d9dab003c39bfa32787ca7 to your computer and use it in GitHub Desktop.
A script to find and open files from the command line
#!/usr/bin/env bash
DEFAULT_MACOS_OPEN="open"
DEFAULT_LINUX_OPEN="gvfs-open"
if ! [ -x "$(command -v ${DEFAULT_LINUX_OPEN})" ]; then
DEFAULT_LINUX_OPEN="browse"
fi
declare -A OPEN_MACOS
OPEN_MACOS[PDF]="${DEFAULT_MACOS_OPEN}"
OPEN_MACOS[TXT]="${EDITOR}"
OPEN_MACOS[TEX]="${EDITOR}"
OPEN_MACOS[YML]="${EDITOR}"
OPEN_MACOS[CSV]="vd"
OPEN_MACOS[TML]="${EDITOR}"
OPEN_MACOS[JSN]="${EDITOR}"
OPEN_MACOS[WEB]="${DEFAULT_MACOS_OPEN}"
OPEN_MACOS[XXX]="${EDITOR}"
declare -A OPEN_LINUX
OPEN_LINUX[PDF]="${DEFAULT_LINUX_OPEN}"
OPEN_LINUX[TXT]="${EDITOR}"
OPEN_LINUX[TEX]="${EDITOR}"
OPEN_LINUX[YML]="${EDITOR}"
OPEN_LINUX[CSV]="vd"
OPEN_LINUX[TML]="${EDITOR}"
OPEN_LINUX[JSN]="${EDITOR}"
OPEN_LINUX[WEB]="${DEFAULT_LINUX_OPEN}"
OPEN_LINUX[XXX]="${EDITOR}"
function usage() {
local CAT_COMMAND
if [ -x "$(command -v mdcat)" ]; then
CAT_COMMAND="mdcat"
else
CAT_COMMAND="cat"
fi
"${CAT_COMMAND}" << EOF
Find a file and open it with the configured editor.
**SETUP**:
> brew install fd fzf
> pipx install visidata
**USAGE**:
- Search . for report.pdf and open it
> $(basename "${0}") report.pdf
- Search path for report.pdf and open it
> $(basename "${0}") report.pdf path
- Search . for results.csv and open it with fleet
> FORCE=fleet $(basename "${0}") result.csv .
- Pass extra arguments to fd engine (i.e. allow hidden and ignored files)
> $(basename "${0}") report.pdf . -u
EOF
echo
}
function error() {
echo "error: ${1}"
exit 1
}
if ! [ -x "$(command -v fd)" ]; then
# shellcheck disable=SC2124
export FZF_DEFAULT_COMMAND="find ${2:-.} -type f ${@:3}"
else
# shellcheck disable=SC2124
export FZF_DEFAULT_COMMAND="fd ${2:-.} --type f ${@:3}"
fi
if ! [ -x "$(command -v fzf)" ]; then
error "please install fzf"
fi
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
case "$(uname -s)" in
Linux*) machine=Linux;;
Darwin*) machine=MacOS;;
*) machine=Error;;
esac
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
function macos-open() {
local ext="${1##*.}"
case "${ext}" in
pdf) "${OPEN_MACOS[PDF]}" "$1";;
txt) "${OPEN_MACOS[TXT]}" "$1";;
tex) "${OPEN_MACOS[TEX]}" "$1";;
yml) "${OPEN_MACOS[YML]}" "$1";;
csv) "${OPEN_MACOS[CSV]}" "$1";;
toml) "${OPEN_MACOS[TML]}" "$1";;
json) "${OPEN_MACOS[JSN]}" "$1";;
html) "${OPEN_MACOS[WEB]}" "$1";;
*) "${OPEN_MACOS[XXX]}" "$1";;
esac
}
function linux-open() {
local ext="${1##*.}"
case "${ext}" in
pdf) "${OPEN_LINUX[PDF]}" "$1";;
txt) "${OPEN_LINUX[TXT]}" "$1";;
tex) "${OPEN_LINUX[TEX]}" "$1";;
yml) "${OPEN_LINUX[YML]}" "$1";;
csv) "${OPEN_LINUX[CSV]}" "$1";;
toml) "${OPEN_LINUX[TML]}" "$1";;
json) "${OPEN_LINUX[JSN]}" "$1";;
html) "${OPEN_LINUX[WEB]}" "$1";;
*) "${OPEN_LINUX[XXX]}" "$1";;
esac
}
function find-and-open() {
local found
found="$(fzf -q "$1" -1)"
echo "found: ${found}"
if [[ -f "${found}" ]]; then
if [ -z "${FORCE}" ]; then
"$2" "${found}"
exit 0
else
"${FORCE}" "${found}"
exit 0
fi
fi
error "can not open file"
}
if [[ "${machine}" = "Error" ]]; then
error "unsupported machine: ${machine}"
fi
if [[ "${machine}" = "MacOS" ]]; then
find-and-open "${1}" macos-open
fi
if [[ "${machine}" = "Linux" ]]; then
find-and-open "${1}" linux-open
fi
@AdamGagorik
Copy link
Author

Find a file and open it with the configured editor.

SETUP

brew install fd fzf
pipx install visidata

USAGE

  • Search ~ for report.pdf and open it
fopen report.pdf
  • Search . for report.pdf and open it
fopen report.pdf .
  • Search . for results.csv and open it with vim
FORCE=vim fopen result.csv .
  • Pass extra arguments to fd engine (i.e. allow hidden and ignored files)
fopen report.pdf . --no-ignore --hidden

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment