# Command checker helper function check_cmd() { test -x "$(command -v $1)" } ## Nushell and jc functions # nushell: df function nudf() { nu -c 'jc df | from json' } # nushell: free function numem() { nu -c 'jc free | from json' } # nushell: lsof -i function nunetcons() { nu -c 'jc lsof -i | from json' } # nushell: nutulp function nutulp() { nu -c 'jc ss -tulp | from json' } # nushell: openports function nuopenports() { nu -c 'sudo jc lsof -i | from json | find "LISTEN"' } # nushell: timestamp Z function nutsz() { nu -c 'date now | format date "%FT%T%:z"' } # watch directory listing, update every 0.1s function watchdir() { watch -n0,1 "ls -lh $1/ | tail" } # get time in timezone function timein() { continent=$(find /usr/share/zoneinfo -maxdepth 1 -mindepth 1 -type d -not -name "posix" -not -name "right" -exec basename {} \; | gum choose --limit=1) city=$(find "/usr/share/zoneinfo/$continent" -type f -exec basename {} \; | gum choose --limit=1) TZ="$continent/$city" date } # Generate a pseudo-random 16-character string. function genrand() { openssl rand -base64 16 } # Remove ~/.ssh/known_hosts* function rmknownhosts() { rm -v "${HOME}/.ssh/known_hosts"* } # Empty trash function rubbish() { if check_cmd trash-empty; then trash-empty -f -v fi } # 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 rmpics() { find "${HOME}/downloads" \ -maxdepth 1 \ -type f \ \( -name "*.jpg" -o -name "*.jpg_original" -o -name "*.jpeg" -o -name "*.svg" -o -name "*.png" \) \ -delete } # Remove all ISOs and disc images from downloads function rmisos() { find "${HOME}/downloads" \ -maxdepth 1 \ -type f \ \( -name "*.iso" -o -name "*.img" \) \ -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; }