DEV Community

Discussion on: Turn any Bash function/alias into a hybrid executable/source-able script.

Collapse
 
flexible profile image
Info Comment hidden by post author - thread only visible in this permalink
Felix Franz

If you like the scripts in this post, then I can only imagine what this is gonna do to you:

$ alias ls="ls --group-directories-first --time-style=full-iso -l -A -g -G"
$ ls --reverse -t -c
drwxr-xr-x 15   480 2022-03-07 18:02:46.137044599 +0100 puppet
drwxr-xr-x 47  1504 2022-03-20 18:46:38.562413488 +0100 puppetlabs-motd
drwxr-xr-x 23   736 2022-03-26 19:20:07.091106952 +0100 _ansible
drwxr-xr-x  4   128 2022-03-30 12:14:26.936141669 +0200 puppet5
drwxr-xr-x 10   320 2022-04-08 22:40:52.012258343 +0200 _tmp
-rw-r--r--  1  8196 2022-03-09 04:10:13.278738177 +0100 .DS_Store
-rw-r--r--  1 49022 2022-03-16 13:37:21.047662608 +0100 BrokerKeys.kdbx

$ ls --reverse -t | grep '^d' | tail -n 1
drwxr-xr-x 10   320 2022-04-08 22:40:52.012258343 +0200 _tmp
Enter fullscreen mode Exit fullscreen mode

Well, I think you get the idea.
For your function loading woes, try zsh and put something like these lines in your ~/.zshrc file.

pmodload 'helper'

# autoload functions
ZSH_FUNC_DIR="${0:h}/.zsh/functions"
if [[ -z ${fpath[(r)$ZSH_FUNC_DIR]} ]]; then
  fpath+=( "$ZSH_FUNC_DIR" )
  autoload -U "$ZSH_FUNC_DIR"/*(.:t)
fi
Enter fullscreen mode Exit fullscreen mode

In that folder, just put a file named as the desired function and as content put the function body and let zsh do it's magic.

# filename: dbg
# Activate shell debug mode for next command.
set -o xtrace; eval "$@"; rc=$?; set +o xtrace
return $rc
Enter fullscreen mode Exit fullscreen mode

The function or however many you put there, will be lazy-loaded on first execution. For that, I just converted most of my aliases into functions and never looked back.

$ which dbg
dbg () {
    local -a fpath
    fpath=("/Users/www-data/.zsh/functions")
    builtin autoload -X -U
}

$ dbg "echo oh yes" # output omitted
$ which dbg
dbg () {
    set -o xtrace
    eval "$@"
    rc=$?
    set +o xtrace
    return $rc
}
Enter fullscreen mode Exit fullscreen mode

An approach more similar to yours for sourcing the files is to use shell globbing instead of find. With the right shopt settings this one-liner in ~/.zshrc or ~/.bashrc should do the trick.

for file in .zsh/{functions,aliases}/*.zsh; do
  source "$(realpath "$file")"
done
Enter fullscreen mode Exit fullscreen mode

I strongly recommend you to try zsh + prezto, you just gonna love the tab completion!

Put that in your .zpreztorcas a starting point and play around with ssh and scp.

zstyle ':filter-select:highlight' matched fg=yellow,bold
zstyle ':filter-select' max-lines 18 # restrict lines for filter-select
zstyle ':filter-select' rotate-list yes # enable rotation for filter-select
zstyle ':filter-select' case-insensitive yes # enable case-insensitive search
zstyle ':filter-select' extended-search yes # see below
zstyle ':filter-select' hist-find-no-dups yes # ignore duplicates in history source
zstyle ':filter-select' escape-descriptions no # display literal newlines, not \n, etc
zstyle ':completion:*' menu select
zstyle ':completion:*' accept-exact '*(N)'
zstyle ':completion::complete:*' cache-path "${XDG_CACHE_HOME:-$HOME/.cache}/zinit/zcompcache"
# Set the entries to ignore in static */etc/hosts* for host completion.
zstyle ':prezto:module:completion:*:hosts' etc-host-ignores \
  '0.0.0.0' '127.0.0.1'
Enter fullscreen mode Exit fullscreen mode

Some comments have been hidden by the post's author - find out more