dotfiles/.zshrc.d/functions.zsh
2023-09-15 15:59:26 -05:00

137 lines
3.5 KiB
Bash

# Remove all .jpeg .jpg .png .svg files from downloads
function rmimg() {
find "${HOME}/downloads" \
-maxdepth 1 \
-type f \
\( -name "*.jpg" -o -name "*.jpeg" -o -name "*.svg" -o -name "*.png" \) \
-delete
}
# Create a bookmark.
function mark() {
if ! test -f "${HOME}/.shellmarks"; then
touch "${HOME}/.shellmarks"
fi
mark_to_add="$(pwd)"
if grep -qxFe "${mark_to_add}" "${HOME}/.shellmarks"; then
gum style \
--foreground 210 \
--margin "1 2" \
"This bookmark already exists: ${mark_to_add}"
else
echo "${mark_to_add}" >> "${HOME}/.shellmarks"
gum style \
--foreground "#73F59F" \
--margin "1 2" \
"${mark_to_add} added to shellmarks file"
fi
return 0
}
# Remove bookmarks.
function delmark() {
selection=$(cat "${HOME}/.shellmarks" | gum choose --no-limit)
if test -n "${selection}"; then
while read -r line; do
perl -n -i -e "print unless /^\\Q${line//\//\\/}\\E\$/" "${HOME}/.shellmarks"
done <<< "${selection}"
else
return 0
fi
gum format -t markdown -- \
"# The following bookmarks were deleted:" \
"$(printf "%s\n" "${selection}")"
return 0
}
# Goto a bookmark.
function gotomark() {
if ! test -f "${HOME}/.shellmarks"; then
echo "No bookmarks exist yet. Add some!"
return 1
fi
echo
builtin cd $(cat "${HOME}/.shellmarks" | gum choose --limit=1)
local precmd
for precmd in $precmd_functions; do
$precmd
done
zle && zle reset-prompt
}
zle -N gotomark
bindkey '^[g' gotomark
# Choose a directory from the directory stack.
function dstack() {
echo "# Choose a directory" | gum format -t markdown
selection=$(dirs -lp | gum choose --limit=1)
cd $selection
}
# Print timestamp as %Y-%m-%d %H:%M:%S.
function tstamp() {
emulate -L zsh
date '+%Y-%m-%d %H:%M:%S'
}
# Print timestamp as %Y-%m-%dT%H:%M:%S%:z
function tstampz() {
emulate -L zsh
date '+%Y-%m-%dT%H:%M:%S%:z'
}
# Create a /overview/ of all available function()'s; the description for
# each funtion() *must* be the first line above the string `function'!
# Otherwise it wont work.
# Display all function()'s with their descriptions.
function funlist() {
grep -B 1 "^function" $HOME/.zshrc.d/functions.zsh | \
grep -v "^\-\-$" | \
awk '{ if(/^#/) { gsub(/^#[:space:]*/, ""); ht=$0 }; getline; gsub(/ ?\(\)/, ":"); printf("-> %-20s %s\n", $2, ht); }' | \
sort -u -k 3
}
# zrmcomp() remove *.zwc files.
function zrmcomp() {
local i
for i in ${HOME}/*.zwc(N); do
printf "Removing $i\n"
command rm -f $i
done
}
# Invoke this every time you change .zshrc to recompile it.
function src() {
autoload -U zrecompile
[ -f ~/.zshrc ] && zrecompile -p ~/.zshrc
[ -f ~/.zcompdump ] && zrecompile -p ~/.zcompdump
[ -f ~/.zcompdump ] && zrecompile -p ~/.zcompdump
[ -f ~/.zshrc.zwc.old ] && command rm -f ~/.zshrc.zwc.old
[ -f ~/.zcompdump.zwc.old ] && command rm -f ~/.zcompdump.zwc.old
source ~/.zshrc
}
# Do an ls after cd.
function cd() { builtin cd "$@" && ls; }
# Create new directory and enter it.
function mkd() { mkdir -p "$@" && cd "$_"; }
# Display pids of commands.
function pids() { pgrep -a "$@"; }
# Restart zsh.
function restart() { exec $SHELL $SHELL_ARGS "$@"; }
# cd to ~, clear screen, and restart zsh.
function rsrc() { cd && clear && restart; }