I'm a big fan of peco to interactively grep lists, so here come a few of mines:
# pecohist show your command history and let you grep them, then copy your selection to your clipboard
alias hist="history | cut -c 8-"
pecohist() {
cmd=$(hist | peco | tr -d '\n')
$(echo $cmd | pbcopy)
echo $cmd
}
If you're using AWS a lot, ec2-cssh (I'm working on improving it, but it's already such a time saver) will let you list your ec2 instances (with their IPs, ids and tags) and automatically ssh into them, with a default key or the one you specify in args (requires aws cli, peco, jq)
function list_ec2s() {
local jq_query='.Reservations[] | .Instances[] | select(.State.Name != "terminated") | select(has("PublicIpAddress")) | [.PublicIpAddress,.PrivateIpAddress,.State.Name,(.Tags[] | select(.Key == "Name") | .Value // "")] | join("\t")'
aws ec2 describe-instances | jq -r $jq_query | peco
}
# Requires aws-cli, peco, jq and tmux-cssh (brew install it)
function ec2-cssh() {
local ssh_user=$2
local ssh_key=$1
local ip_addresses
# Default user
if [ -z ${ssh_user:+x} ]; then ssh_user="ec2-user"; fi
# Default key
if [ -z ${ssh_key:+y} ]; then ssh_key="~/.ssh/my_key.pem"; fi
ip_addresses=$(list_ec2s | awk '{ print $1 }' | tr '\n' ' ')
if [ -n $ip_addresses ]; then
echo "ssh -i ${ssh_key} ${ssh_user}@${ip_addresses}"
#sh -c "ssh -i $ssh_key ${ssh_user}@${ip_addresses}"
sh -c "tmux-cssh -u $ssh_user -i $ssh_key $ip_addresses"
fi
}
Easily display markdown in your console (when you're just looking for a sample command you know there, super easy to get it.... it's a man-like-formated markdown export displayed in less
# Markdown utilities (requires `brew install pandoc`)
function mdless () {
local mdfile=$1
if [ -z ${mdfile:+x} ]; then mdfile="README.md"; fi
case $mdfile in
-h | --help)
print "mdless usage: 'mdless myfile.md' or 'mdless' (defaults to README.md)"
;;
*)
mdcat $mdfile | less
;;
esac
}
function mdcat () {
local mdfile=$1
if [ -z ${mdfile:+x} ]; then mdfile="README.md"; fi
pandoc -s -f markdown -t man $mdfile | groff -T utf8 -man
}
If you're like me and have a tons of commands in the scripts node of your package.json, then list them to display an interactive list, and copy a npm run command of the selected one to your clipboard:
function npm_run(){
$(echo npm run $(cat package.json | jq -r '.scripts | keys' | grep '"' | sed 's/[",]//g' | peco | tr -d '\n' ) | pbcopy)
echo `pbpaste`
}