Skip to content

Instantly share code, notes, and snippets.

@jamesoff
Last active September 24, 2015 08:59
Show Gist options
  • Save jamesoff/9e3fbc5a32aa3a03dbe6 to your computer and use it in GitHub Desktop.
Save jamesoff/9e3fbc5a32aa3a03dbe6 to your computer and use it in GitHub Desktop.
A couple of useful zsh functions
# Use ag and fzf to pick a result and open it in vim at the matching line,
# plus show the ag results in the quickfix window (needs Ag plugin in your vim)
function agvim () {
CHOICE=$(ag --color $* | fzf -0 -1 --ansi)
if [ ! -z "$CHOICE" ]; then
# Open vim at the selected file and line, but also run the Ag scan
# the ! on Ag! stops Ag jumping to the first match, and the wincmd gives the editor window focus
nvim $( echo "$CHOICE" | awk 'BEGIN { FS=":" } { printf "+%d %s\n", $2, $1 } ') +"Ag! '$*'" "+wincmd k"
fi
}
# Make fzf a little more sensible
export FZF_DEFAULT_OPTS='-e'
# Repeatedly try to connect to a host which is booting
# ssh's return code is a little unhelpful as it doesn't distinguish the failure
# reason properly so this is a little naive
function try_ssh () {
SUCCESS=0
while [ $SUCCESS -eq 0 ]; do
ssh -o "ConnectTimeout 30" $*
RESULT=$?
if [ $RESULT -ne 255 ]; then
SUCCESS=1
else
echo "--> SSH return code was $RESULT"
print "Waiting to retry ssh..."
sleep 10
echo "--> Retrying..."
fi
done
}
# Toggle the VPN state
function vpn () {
if $( pgrep vpnc > /dev/null ); then
echo "--> Disconnecting VPN"
sudo /usr/local/sbin/vpnc-disconnect
else
echo "--> Connecting VPN"
sudo /usr/local/sbin/vpnc
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment