Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active June 5, 2025 11:23
Show Gist options
  • Save coderofsalvation/46549e3788ade2f3a938 to your computer and use it in GitHub Desktop.
Save coderofsalvation/46549e3788ade2f3a938 to your computer and use it in GitHub Desktop.

Revisions

  1. coderofsalvation revised this gist Mar 23, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@ DMENU_ARGS="-b"
    TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
    # if nothing happens, try "xdotool click 2", "xdotool key ctrl+v" or "xdotool key ctrl+shift+v"
    GUIPASTE="xdotool click 2"
    CLIPASTE="xdotool key ctrl+v"

    # smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
    # @link http://github.com/coderofsalvation/bashdown
    @@ -78,15 +79,14 @@ run(){
    fi
    xsel --input < $TMPFILE
    # Paste into the current application.
    paste_cli || ${GUIPASTE} # cli or gui paste
    [[ is_window ]] && ${GUIPASTE} || ${CLIPASTE} # cli or gui paste
    }


    paste_cli(){
    is_window(){
    name="$(xdotool getwindowname $(xdotool getwindowfocus) | tr '[:upper:]' '[:lower:]')"
    [[ ! "$name" =~ term|tilda ]] && return 1
    ${GUIPASTE}
    return 0
    }

    init && run

    init && run
  2. coderofsalvation revised this gist Mar 5, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion snippy.sh
    Original file line number Diff line number Diff line change
    @@ -71,7 +71,8 @@ run(){
    FILE=`find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS}`
    if [ -f ${DIR}/${FILE} ]; then
    # Put the contents of the selected file into the paste buffer.
    cat ${DIR}/${FILE} | bashdown > $TMPFILE
    content="$(cat ${DIR}/${FILE} | bashdown)"
    [[ "${#content}" == 0 ]] && printf "${FILE}" > $TMPFILE || printf "%s" "$content" > $TMPFILE
    else
    ${FILE} &> $TMPFILE # execute as bashcommand
    fi
    @@ -88,3 +89,4 @@ paste_cli(){
    }

    init && run

  3. coderofsalvation revised this gist Dec 16, 2014. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,8 @@ DIR=${HOME}/.snippy
    APPS="xdotool xsel dmenu"
    DMENU_ARGS="-b"
    TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
    # if nothing happens, try "xdotool click 2", "xdotool key ctrl+v" or "xdotool key ctrl+shift+v"
    GUIPASTE="xdotool click 2"

    # smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
    # @link http://github.com/coderofsalvation/bashdown
    @@ -75,14 +77,14 @@ run(){
    fi
    xsel --input < $TMPFILE
    # Paste into the current application.
    paste_cli || xdotool key ctrl+v # cli or gui paste
    paste_cli || ${GUIPASTE} # cli or gui paste
    }


    paste_cli(){
    name="$(xdotool getwindowname $(xdotool getwindowfocus) | tr '[:upper:]' '[:lower:]')"
    [[ ! "$name" =~ term|tilda ]] && return 1
    [[ "$name" =~ xterm$ ]] && xdotool click 2 || xdotool key ctrl+shift+v
    ${GUIPASTE}
    }

    init && run
    init && run
  4. coderofsalvation revised this gist Dec 9, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,6 @@ DIR=${HOME}/.snippy
    APPS="xdotool xsel dmenu"
    DMENU_ARGS="-b"
    TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
    XSEL_ARGS="--clipboard --input"

    # smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
    # @link http://github.com/coderofsalvation/bashdown
    @@ -74,15 +73,16 @@ run(){
    else
    ${FILE} &> $TMPFILE # execute as bashcommand
    fi
    xsel ${XSEL_ARGS} < $TMPFILE
    xsel --input < $TMPFILE
    # Paste into the current application.
    is_terminal && xdotool key ctrl+shift+v || xdotool key ctrl+v # cli or gui paste
    paste_cli || xdotool key ctrl+v # cli or gui paste
    }

    is_terminal(){

    paste_cli(){
    name="$(xdotool getwindowname $(xdotool getwindowfocus) | tr '[:upper:]' '[:lower:]')"
    [[ "$name" =~ term|tilda ]] && return 0 || return 1
    [[ ! "$name" =~ term|tilda ]] && return 1
    [[ "$name" =~ xterm$ ]] && xdotool click 2 || xdotool key ctrl+shift+v
    }

    init && run

    init && run
  5. coderofsalvation revised this gist Dec 2, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion snippy.sh
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,7 @@ run(){
    cd ${DIR}
    # Use the filenames in the snippy directory as menu entries.
    # Get the menu selection from the user.
    FILE=`find . -type f | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS}`
    FILE=`find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS}`
    if [ -f ${DIR}/${FILE} ]; then
    # Put the contents of the selected file into the paste buffer.
    cat ${DIR}/${FILE} | bashdown > $TMPFILE
  6. coderofsalvation revised this gist Nov 14, 2014. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@
    DIR=${HOME}/.snippy
    APPS="xdotool xsel dmenu"
    DMENU_ARGS="-b"
    TMPFILE="/tmp/.snippy.tmp"
    TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
    XSEL_ARGS="--clipboard --input"

    # smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
    @@ -71,10 +71,12 @@ run(){
    if [ -f ${DIR}/${FILE} ]; then
    # Put the contents of the selected file into the paste buffer.
    cat ${DIR}/${FILE} | bashdown > $TMPFILE
    xsel ${XSEL_ARGS} < $TMPFILE
    # Paste into the current application.
    is_terminal && xdotool key ctrl+shift+v || xdotool key ctrl+v # cli or gui paste
    else
    ${FILE} &> $TMPFILE # execute as bashcommand
    fi
    xsel ${XSEL_ARGS} < $TMPFILE
    # Paste into the current application.
    is_terminal && xdotool key ctrl+shift+v || xdotool key ctrl+v # cli or gui paste
    }

    is_terminal(){
  7. coderofsalvation revised this gist Nov 10, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion snippy.sh
    Original file line number Diff line number Diff line change
    @@ -73,9 +73,14 @@ run(){
    cat ${DIR}/${FILE} | bashdown > $TMPFILE
    xsel ${XSEL_ARGS} < $TMPFILE
    # Paste into the current application.
    [[ -t 0 ]] && xdotool key ctrl+shift+v || xdotool key ctrl+v # cli or gui paste
    is_terminal && xdotool key ctrl+shift+v || xdotool key ctrl+v # cli or gui paste
    fi
    }

    is_terminal(){
    name="$(xdotool getwindowname $(xdotool getwindowfocus) | tr '[:upper:]' '[:lower:]')"
    [[ "$name" =~ term|tilda ]] && return 0 || return 1
    }

    init && run

  8. coderofsalvation revised this gist Nov 1, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -40,9 +40,11 @@ XSEL_ARGS="--clipboard --input"
    # fetches a document and interprets bashsyntax in string (bashdown) templates
    # @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
    bashdown(){
    IFS=''; cat - | while read line; do
    txt="$(cat - )"; lines="$(cat ${DIR}/${FILE} | wc -l )"
    (( lines > 1 )) && enter="\n" || enter=""
    IFS=''; echo "$txt" | while read line; do
    [[ "$line" =~ '$' ]] && line="$(eval "printf \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")";
    printf "$line"
    printf "$line""$enter"
    done
    }

  9. coderofsalvation revised this gist Oct 30, 2014. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -5,12 +5,13 @@
    # Based on "snippy" by "sessy"
    # (https://bbs.archlinux.org/viewtopic.php?id=71938)
    #
    # You will also need "dmenu", "xsel" and "xdotool", it will be installed
    # automatically when using debianbased distros (ubuntu).
    # You will also need "dmenu", "xsel" and "xdotool". Get them from your linux
    # distro in the usual way.
    #
    # To use:
    # 1. run snippy
    # 2. Create a file in ~/.snippy for each snippet that you want.
    # 1. Create the directory ~/.snippy
    #
    # 2. Create a file in that directory for each snippet that you want.
    # The filename will be used as a menu item, so you might want to
    # omit the file extension when you name the file.
    #
    @@ -24,7 +25,7 @@
    # 3. Bind a convenient key combination to this script.
    #
    # TIP: If you're using XMonad, add something like this to xmonad.hs
    # ((mod4Mask, xK_s), spawn "/path/to/snippy"). In XFCE its Settings->Keyboard->Shortcuts
    # ((mod4Mask, xK_s), spawn "/path/to/snippy")
    #
    DIR=${HOME}/.snippy
    APPS="xdotool xsel dmenu"
    @@ -40,8 +41,8 @@ XSEL_ARGS="--clipboard --input"
    # @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
    bashdown(){
    IFS=''; cat - | while read line; do
    [[ "$line" =~ '$' ]] && line="$(eval "echo \"$( echo "$line" | sed 's/"/\\"/g')\"")";
    echo "$line"
    [[ "$line" =~ '$' ]] && line="$(eval "printf \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")";
    printf "$line"
    done
    }

    @@ -75,3 +76,4 @@ run(){
    }

    init && run

  10. coderofsalvation revised this gist Oct 30, 2014. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,12 @@
    # Based on "snippy" by "sessy"
    # (https://bbs.archlinux.org/viewtopic.php?id=71938)
    #
    # You will also need "dmenu", "xsel" and "xdotool". Get them from your linux
    # distro in the usual way.
    # You will also need "dmenu", "xsel" and "xdotool", it will be installed
    # automatically when using debianbased distros (ubuntu).
    #
    # To use:
    # 1. Create the directory ~/.snippy
    #
    # 2. Create a file in that directory for each snippet that you want.
    # 1. run snippy
    # 2. Create a file in ~/.snippy for each snippet that you want.
    # The filename will be used as a menu item, so you might want to
    # omit the file extension when you name the file.
    #
    @@ -25,7 +24,7 @@
    # 3. Bind a convenient key combination to this script.
    #
    # TIP: If you're using XMonad, add something like this to xmonad.hs
    # ((mod4Mask, xK_s), spawn "/path/to/snippy")
    # ((mod4Mask, xK_s), spawn "/path/to/snippy"). In XFCE its Settings->Keyboard->Shortcuts
    #
    DIR=${HOME}/.snippy
    APPS="xdotool xsel dmenu"
  11. coderofsalvation created this gist Oct 30, 2014.
    78 changes: 78 additions & 0 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    #!/bin/bash
    # video demo at: http://www.youtube.com/watch?v=90xoathBYfk

    # written by "mhwombat": https://bbs.archlinux.org/viewtopic.php?id=71938&p=2
    # Based on "snippy" by "sessy"
    # (https://bbs.archlinux.org/viewtopic.php?id=71938)
    #
    # You will also need "dmenu", "xsel" and "xdotool". Get them from your linux
    # distro in the usual way.
    #
    # To use:
    # 1. Create the directory ~/.snippy
    #
    # 2. Create a file in that directory for each snippet that you want.
    # The filename will be used as a menu item, so you might want to
    # omit the file extension when you name the file.
    #
    # TIP: If you have a lot of snippets, you can organise them into
    # subdirectories under ~/.snippy.
    #
    # TIP: The contents of the file will be pasted asis, so if you
    # don't want a newline at the end when the text is pasted, don't
    # put one in the file.
    #
    # 3. Bind a convenient key combination to this script.
    #
    # TIP: If you're using XMonad, add something like this to xmonad.hs
    # ((mod4Mask, xK_s), spawn "/path/to/snippy")
    #
    DIR=${HOME}/.snippy
    APPS="xdotool xsel dmenu"
    DMENU_ARGS="-b"
    TMPFILE="/tmp/.snippy.tmp"
    XSEL_ARGS="--clipboard --input"

    # smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
    # @link http://github.com/coderofsalvation/bashdown
    # @dependancies: sed cat
    # @example: echo 'hi $NAME it is $(date)' | bashdown
    # fetches a document and interprets bashsyntax in string (bashdown) templates
    # @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
    bashdown(){
    IFS=''; cat - | while read line; do
    [[ "$line" =~ '$' ]] && line="$(eval "echo \"$( echo "$line" | sed 's/"/\\"/g')\"")";
    echo "$line"
    done
    }

    init(){
    for APP in $APPS; do
    which $APP &>/dev/null || {
    read -p "install the following required utils? : $APPS (y/n)" reply
    [[ "$reply" == "y" ]] && sudo apt-get install ${APPS};
    }
    done
    [[ ! -d "$DIR" ]] && {
    echo -e "created $DIR\n";
    mkdir "$DIR";
    printf 'hi it is $(date)' > "$DIR""/test";
    }
    return 0
    }

    run(){
    cd ${DIR}
    # Use the filenames in the snippy directory as menu entries.
    # Get the menu selection from the user.
    FILE=`find . -type f | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS}`
    if [ -f ${DIR}/${FILE} ]; then
    # Put the contents of the selected file into the paste buffer.
    cat ${DIR}/${FILE} | bashdown > $TMPFILE
    xsel ${XSEL_ARGS} < $TMPFILE
    # Paste into the current application.
    [[ -t 0 ]] && xdotool key ctrl+shift+v || xdotool key ctrl+v # cli or gui paste
    fi
    }

    init && run