Skip to content

Instantly share code, notes, and snippets.

@lonetwin
Last active April 26, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lonetwin/0bc43ae3ae94ac6dfe7bc8e28b4d16ec to your computer and use it in GitHub Desktop.
Save lonetwin/0bc43ae3ae94ac6dfe7bc8e28b4d16ec to your computer and use it in GitHub Desktop.
Collection of bashrc functions (and their bash completions) I find useful
# execute an interactive python shell on a remote machine,
# over ssh, after coping my local pythonrc to that machine
# (my pythonrc available here: https://gist.github.com/lonetwin/5902720)
function rpython {
DEST='/tmp/.pythonrc.py'
scp -q $PYTHONSTARTUP $1:$DEST
ssh -t $1 -- "PYTHONSTARTUP=$DEST python"
ssh $1 "rm $DEST"
}
# - setup completion for rpython
complete -F _known_hosts rpython
# mount remote directories using sshfs (https://github.com/libfuse/sshfs)
# by executing "sshfs host:/dest/path $HOME/src/sshfs/dest/path",
# creating the mountpoint, if necessary. If leading / in /dest/path is
# missing, the path is relative to ~ on the destination
function mount_sshfs {
local USAGE="Usage: mount_sshfs <hostname:[path]>"
[ $# -lt 1 ] && echo $USAGE && return
local srcpath="${1}"
local destdir="${HOME}/src/sshfs/${srcpath/:*/}"
[ -d "${destdir}" ] || mkdir -p "${destdir}"
sshfs "${srcpath}" "${destdir}"
}
complete -F _known_hosts -S: mount_sshfs
function isolate {
# - filter to select all lines starting with pattern in $1 upto the next
# newline or the pattern in $2 if provided.
# For example,
# $ cat /etc/passwd | isolate :500:500 :1000:1000
# will print out all lines for uids between 500 and 1000 and
# $ cat /path/to/some_python_script.py | isolate 'def func_name'
# will print out the definition of 'func_name()' (assuming of course that
# the first line after the function definition is an empty line)
sed -n "/${1}/,/${2:-^\$}/p"
}
function pdfcompress {
# - reduce the size of a pdf file by re-writing it in lower resolution
# (default to the gs option -dPDFSETTINGS=/ebook)
local USAGE="USAGE: pdfcompress <inputfile> <outputfile> [<screen|ebook|printer|prepress>]"
[ $# -lt 2 ] && echo $USAGE && return
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${3:-ebook} -sOutputFile=$2 $1
}
complete -f -X '!*.pdf' pdfcompress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment