dotfiles/zsh/.zshrc.d/functions.zsh
2024-03-17 03:37:27 -05:00

164 lines
4.2 KiB
Bash

# Generate a pseudo-random 16-character string.
function genrand() {
openssl rand -base64 16
}
# Compatibility with emacs-libvterm
function vterm_printf() {
if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ]); then
# Tell tmux to pass the escape sequences through
printf "\ePtmux;\e\e]%s\007\e\\" "$1"
elif [ "${TERM%%-*}" = "screen" ]; then
# GNU screen (screen, screen-256color, screen-256color-bce)
printf "\eP\e]%s\007\e\\" "$1"
else
printf "\e]%s\e\\" "$1"
fi
}
# Remove all .jpeg .jpg .png .svg files from downloads
function rmimg() {
find "${HOME}/downloads" \
-maxdepth 1 \
-type f \
\( -name "*.jpg" -o -name "*.jpg_original" -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
}
# List bookmarks.
function lsmarks() {
echo "# Shellmarks" | gum format
while IFS= read -r line; do
echo "- $line"
done < "${HOME}/.shellmarks" | gum format
}
# 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; }