Last active
September 24, 2023 15:08
-
-
Save dmix/41350b4833e8625a6130115bf0bf92ac to your computer and use it in GitHub Desktop.
elvish aliases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================================== | |
# Elvish Aliases | |
# ============================================================================== | |
# Aliases | |
# ----------------------------------------------------------------------------- | |
# Git | |
git-aliases = [ | |
&prefix= "g" | |
&name= "git" | |
&aliases=" | |
s git status | |
l git log | |
c git commit | |
cl git clone | |
b git checkout -b | |
p git pull | |
r git remote -v | |
pl git pull --rebase origin | |
po git push origin | |
undo git reset --soft HEAD~1 | |
" | |
] | |
# Mix | |
mix-aliases = [ | |
&prefix= "mx" | |
&name= "Mix" | |
&aliases=" | |
credo mix credo | |
server mix phoenix.server | |
init mix deps.get && mix ecto.setup && npm install | |
deps mix deps.get | |
test mix test | |
" | |
] | |
# Elixir | |
elixir-aliases = [ | |
&prefix= "elx" | |
&name= "Elixir" | |
&aliases=" | |
repl iex -S mix | |
" | |
] | |
# Linux/MacOS | |
os-aliases = [ | |
&prefix= "os" | |
&name= "Linux/MacOS Aliases" | |
&aliases= [ | |
[v "nvim"] | |
[e "emacs -nw"] | |
[l "k -h"] | |
[ll "k -ah"] | |
[p "print"] | |
[q "exit"] | |
[h "history -n 1"] | |
[rp "fc -ln -1"] | |
[rps "sudo $(fc -ln -1)"] | |
[paths "print -l ${(s.:.)PATH}"] | |
[ska "sudo killall -9"] | |
[ka "killall -9"] | |
[ps-ls "ps -ef"] | |
[copydir "pwd | tr -d \"\r\n\" | pbcopy"] | |
[mergefiles "tail -n +1"] | |
[_ "sudo"] | |
[si "sudo -i"] | |
[siu "sudo -i -u"] | |
[siur "sudo -i -u root"] | |
[genpw "openssl rand -base64 50"] | |
[yt "youtube-dl --external-downloader aria2c"] | |
[dl "aria2c"] | |
[dns "sudo chattr -i /etc/resolv.conf && sv /etc/resolv.conf"] | |
[whatsip "curl ipinfo.io"] | |
[whatsip2 "wget -qO - icanhazip.com"] | |
[pg "ping google.com"] | |
[iplink "sudo ip link set"] | |
[nscan "nmap 192.168.1.0/24"] | |
[shc "cat .ssh/config | grep Host"] | |
[scc "ssh control"] | |
[scr "ssh relay -t -- /bin/sh -c \"tmux has-session && exec tmux attach || exec tmux\""] | |
] | |
] | |
# Alias builder | |
# ----------------------------------------------------------------------------- | |
# Trim leading and trailing white-space | |
fn trim [str]{ | |
x = (re:replace &posix=$false &longest=$false '(^\s*|\s*$)' '' $str) | |
re:replace &posix=$false &longest=$false '(\s\s*)' ' ' $x | |
} | |
# Convert string into list with[<first word> '<rest>']. Useful for executing a | |
# string as a shell command. | |
# | |
# first-word "git commit -am" | |
# > git 'commit -am' | |
# | |
fn first-word [str]{ | |
first @rest = (splits &sep=' ' (trim $str)) | |
put $first (joins ' ' $rest) | |
} | |
# Parse aliases within multi-line string with format: | |
# | |
# " | |
# <alias> <action> | |
# <alias> <action> | |
# " | |
# | |
# For ex: | |
# | |
# " | |
# s git status | |
# c git commit | |
# p git pull --rebase origin | |
# " | |
# | |
# Generates: | |
# | |
# [[s "git status"], [c "git commit"], [p "git pull --rebase origin"]] | |
# | |
fn parse-alias-strings [aliases]{ | |
print $aliases| from-lines | each [c]{ | |
alias action = (first-word $c) | |
if (!=s $alias "") { | |
put [$alias $action] | |
} | |
} | |
} | |
fn make-aliases [aliases]{ | |
if (eq (kind-of $aliases) string) { | |
parse-alias-strings $aliases | |
} elif (eq (kind-of $aliases) list) { | |
for a $aliases { put $a } | |
} | |
} | |
# Execute action of first matching alias | |
fn call-alias [aliases cmd]{ | |
try { | |
for a $aliases { | |
alias = $a[0] | |
action = $a[1] | |
if (eq $alias $cmd) { | |
program args = (first-word $action) # exec | |
$program (splits &sep=' ' $args) | |
return | |
} | |
} | |
fail "Error: Command "$cmd" is not an assigned alias" | |
} except e { | |
if (not (eq (echo $e) "?(return)")) { | |
echo $e | |
} | |
} | |
} | |
fn print-aliases [prefix name aliases]{ | |
print "---\n"$name" aliases\n---\n" | |
for a $aliases { | |
print "\t"$prefix" "$a[0]"\t- "$a[1]"\n" | |
} | |
print "\n" | |
} | |
fn aliases [options cmd]{ | |
@aliases = (make-aliases $options[aliases]) | |
if (> (count $cmd) 0) { | |
call-alias $aliases $cmd[0] | |
} else { | |
print-aliases $options[prefix] $options[name] $aliases | |
} | |
} | |
# Create aliases | |
# ----------------------------------------------------------------------------- | |
fn g [@cmd]{ aliases $git-aliases $cmd } | |
fn mx [@cmd]{ aliases $mix-aliases $cmd } | |
fn elx [@cmd]{ aliases $elixir-aliases $cmd } | |
fn os [@cmd]{ aliases $os-aliases $cmd } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment