Skip to content

Instantly share code, notes, and snippets.

@dmix dmix/aliases.ev
Last active Jul 17, 2017

Embed
What would you like to do?
elvish aliases
# ==============================================================================
# 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
You can’t perform that action at this time.