dotfiles/.zshrc.d/functions.zsh

91 lines
2.2 KiB
Bash

# vim: set filetype=sh nospell:
source ~/.zshrc.d/colors.zsh
## Use bat as cat, except use glow for Markdown files
function cat() {
if [[ $# -eq 1 ]]; then
extension=$([[ "$1" = *.* ]] && echo ".${1##*.}" || echo '')
if [[ "$extension" == ".md" ]]; then
glow --pager -w0 $1
elif [[ "$extension" == ".html" ]]; then
reader $1 | ov
elif [[ "$extension" == ".json" ]]; then
bat $1 | jq --color-output | bat
else
bat $1
fi
else
bat $@
fi
}
## Use bat to colorize help text
## alias bathelp='bat --plain --language=help'
function help() {
"$@" --help 2>&1 | bat --plain --language=help
}
## If there is no man page for given command then use help
function man() {
$(whence -p man) $@
[[ $? -ne 0 ]] && help $1 || true
}
## Syntax highlighting for apropos
function apropos() {
$(whence -p apropos) "$@" | bat
}
## Make directory and cd into it
function mcd() {
mkdir $@
cd $_
}
## List dot files
function lsdot() {
eza -d $@ .??* 2>/dev/null
}
## Wrapper for yazi to provide ability to change current working directory when exiting yazi
function y() {
local tmp="$(mktemp -t "yazi-cwd.XXXXXX")"
yazi "$@" --cwd-file="$tmp"
if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
builtin cd -- "$cwd"
fi
rm -f -- "$tmp"
}
function hstr() {
local cmd_file="$(mktemp -t "hist-cmd.XXXXXX")"
1>&2 2>"$cmd_file" /usr/bin/hstr
local cmd="$(cat $cmd_file)"
echo "$cmd" >>"$HISTFILE"
echo "$cmd"
eval "$cmd"
rm "$cmd_file"
}
# set kitty user var `in_editor` when inside sk (skim fuzzy finder)
# need to use whence to find path of executible, functions can be recursive
# so if we don't do this it will just keep calling the sk() function
function sk() {
kitten @ set-user-var in_editor=1
$(whence -p sk) "$@"
kitten @ set-user-var in_editor
}
function fzf() {
echo "fzf not installed, using sk (skim)"
sk
}
function plz() {
last_command=$(fc -l -n -1)
echo "${COLOR_1}Okay, ${COLOR_SUCCESS}${TEXT_ITAL}sudo${TEXT_RESET} ${COLOR_SUCCESS}${last_command}. ${COLOR_1}You're welcome.${TEXT_RESET}"
echo "$last_command" | xargs sudo
}