Skip to content

Instantly share code, notes, and snippets.

@opennomad
Forked from coderofsalvation/snippy.sh
Last active November 5, 2019 15:59
Show Gist options
  • Select an option

  • Save opennomad/15c4d624dce99066a82d to your computer and use it in GitHub Desktop.

Select an option

Save opennomad/15c4d624dce99066a82d to your computer and use it in GitHub Desktop.

Revisions

  1. opennomad revised this gist Nov 5, 2019. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,9 @@ case ${MENU_ENGINE} in
    'zenity')
    MENU_ARGS=${ZENITY_ARGS}
    ;;
    'rofi')
    MENU_ARGS=${ROFI_ARGS}
    ;;
    *)
    echo "incorrecty MENU_ENGINE: ${MENU_ENGINE} for snippy"
    exit 1
    @@ -71,8 +74,8 @@ esac
    # fetches a document and interprets bashsyntax in string (bashdown) templates
    # @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
    bashdown(){
    while read line; do
    line="$(eval "printf \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")";
    while read -r line; do
    line="$(eval "printf -- \"$( printf -- "%s" "$line" | sed 's/"/\\"/g')\"")";
    echo -e "$line"
    done < <(cat "${DIR}/${FILE}")
    }
    @@ -96,14 +99,15 @@ run(){
    cd "${DIR}"
    # Use the filenames in the snippy directory as menu entries.
    # Get the menu selection from the user.
    FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | ${MENU_ENGINE} ${MENU_ARGS})
    FILE=$(find -L . -type f | sort | grep -v '^\.$' | sed 's!\.\/!!' | ${MENU_ENGINE} ${MENU_ARGS})
    # FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | zenity )
    # just return if nothing was selected
    [[ -z "${FILE}" ]] && return 1

    if [ -f "${DIR}/${FILE}" ]; then
    # Put the contents of the selected file into the paste buffer.
    content="$(bashdown < "${DIR}/${FILE}")"
    echo "::::${content}::::"
    if [[ "${#content}" == 0 ]]; then
    printf "%s" "${FILE}" > $TMPFILE
    else
  2. opennomad revised this gist Jul 9, 2016. 1 changed file with 40 additions and 7 deletions.
    47 changes: 40 additions & 7 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    #!/usr/bin/env bash
    # video demo at: http://www.youtube.com/watch?v=90xoathBYfk

    # written by "mhwombat": https://bbs.archlinux.org/viewtopic.php?id=71938&p=2
    # augmented by "opennomad": https://gist.github.com/opennomad/15c4d624dce99066a82d
    # originally 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
    # You will also need "dmenu", "zenity", "xsel" and "xdotool". Get them from your linux
    # distro in the usual way.
    #
    # To use:
    @@ -27,14 +28,42 @@
    # TIP: If you're using XMonad, add something like this to xmonad.hs
    # ((mod4Mask, xK_s), spawn "/path/to/snippy")
    #
    # 4. Set variables in ~/.snippy.conf. for example
    # MENU_ENGINE="dmenu"
    # DMENU_ARGS=' -p snippy -fn arial -sf green'
    # ZENITY_ARGS='--list --hide-header --column=Snippet --text=snippy'
    # GUIPASTE="xdotool key ctrl+v"
    # CLIPASTE="xdotool key ctrl+shift+v"


    CONFIG=${HOME}/.snippy.conf
    APPS="xdotool xsel dmenu zenity"
    DIR=${HOME}/.snippy
    APPS="xdotool xsel dmenu"
    DMENU_ARGS="-b"
    TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
    MENU_ENGINE="dmenu"
    DMENU_ARGS='-b'
    ZENITY_ARGS='--list --hide-header --column=Snippet --text=snippy'
    # if nothing happens, try "xdotool click 2", "xdotool key ctrl+v" or "xdotool key ctrl+shift+v"
    GUIPASTE="xdotool key ctrl+v"
    CLIPASTE="xdotool key ctrl+shift+v"

    TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE

    # source the config file if it exists
    [[ -f ${CONFIG} ]] && source ${CONFIG}

    # set the menu options depending on the engine
    case ${MENU_ENGINE} in
    'dmenu')
    MENU_ARGS=${DMENU_ARGS}
    ;;
    'zenity')
    MENU_ARGS=${ZENITY_ARGS}
    ;;
    *)
    echo "incorrecty MENU_ENGINE: ${MENU_ENGINE} for snippy"
    exit 1
    esac

    # 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
    @@ -50,7 +79,7 @@ bashdown(){

    init(){
    for APP in $APPS; do
    which "$APP" &>/dev/null || {
    command -v "$APP" &>/dev/null || {
    read -p "install the following required utils? : $APPS (y/n)" reply
    [[ "$reply" == "y" ]] && sudo apt-get install "${APPS}";
    }
    @@ -67,7 +96,11 @@ run(){
    cd "${DIR}"
    # Use the filenames in the snippy directory as menu entries.
    # Get the menu selection from the user.
    FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS})
    FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | ${MENU_ENGINE} ${MENU_ARGS})
    # FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | zenity )
    # just return if nothing was selected
    [[ -z "${FILE}" ]] && return 1

    if [ -f "${DIR}/${FILE}" ]; then
    # Put the contents of the selected file into the paste buffer.
    content="$(bashdown < "${DIR}/${FILE}")"
  3. opennomad revised this gist Jul 23, 2015. No changes.
  4. opennomad revised this gist Jul 23, 2015. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/bash
    #!/usr/bin/env bash
    # video demo at: http://www.youtube.com/watch?v=90xoathBYfk

    # written by "mhwombat": https://bbs.archlinux.org/viewtopic.php?id=71938&p=2
    @@ -81,7 +81,7 @@ run(){
    fi
    xsel -b --input < $TMPFILE
    # Paste into the current application.
    if [[ $(is_window) ]]; then
    if is_window; then
    ${GUIPASTE}
    else
    ${CLIPASTE}
    @@ -90,8 +90,7 @@ run(){

    is_window(){
    name="$(xdotool getwindowname "$(xdotool getwindowfocus)" | tr '[:upper:]' '[:lower:]')"
    echo "$name" > ~/tmp/foo
    [[ ! "$name" =~ term|tilda|@${HOSTNAME} ]] && return 1
    [[ "$name" =~ term|tilda|@${HOSTNAME} ]] && return 1
    return 0
    }

  5. opennomad revised this gist Jun 25, 2015. 1 changed file with 49 additions and 43 deletions.
    92 changes: 49 additions & 43 deletions snippy.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    # 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"
    # 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
    @@ -13,12 +13,12 @@
    #
    # 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.
    # omit the file extension when you name the file.
    #
    # TIP: If you have a lot of snippets, you can organise them into
    # 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
    # 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.
    #
    @@ -32,61 +32,67 @@ 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"
    CLIPASTE="xdotool key ctrl+v"
    GUIPASTE="xdotool key ctrl+v"
    CLIPASTE="xdotool key ctrl+shift+v"

    # 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
    # fetches a document and interprets bashsyntax in string (bashdown) templates
    # @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
    bashdown(){
    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""$enter"
    done
    while read line; do
    line="$(eval "printf \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")";
    echo -e "$line"
    done < <(cat "${DIR}/${FILE}")
    }

    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};
    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";
    }
    done
    [[ ! -d "$DIR" ]] && {
    echo -e "created $DIR\n";
    mkdir "$DIR";
    printf 'hi it is $(date)' > "$DIR""/test";
    }
    return 0
    return 0
    }

    run(){
    cd ${DIR}
    # Use the filenames in the snippy directory as menu entries.
    # Get the menu selection from the user.
    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.
    content="$(cat ${DIR}/${FILE} | bashdown)"
    [[ "${#content}" == 0 ]] && printf "${FILE}" > $TMPFILE || printf "%s" "$content" > $TMPFILE
    else
    ${FILE} &> $TMPFILE # execute as bashcommand
    fi
    xsel --input < $TMPFILE
    # Paste into the current application.
    [[ is_window ]] && ${GUIPASTE} || ${CLIPASTE} # cli or gui paste
    cd "${DIR}"
    # Use the filenames in the snippy directory as menu entries.
    # Get the menu selection from the user.
    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.
    content="$(bashdown < "${DIR}/${FILE}")"
    if [[ "${#content}" == 0 ]]; then
    printf "%s" "${FILE}" > $TMPFILE
    else
    printf "%s" "$content" > $TMPFILE
    fi
    else
    ${FILE} &> $TMPFILE # execute as bashcommand
    fi
    xsel -b --input < $TMPFILE
    # Paste into the current application.
    if [[ $(is_window) ]]; then
    ${GUIPASTE}
    else
    ${CLIPASTE}
    fi
    }


    is_window(){
    name="$(xdotool getwindowname $(xdotool getwindowfocus) | tr '[:upper:]' '[:lower:]')"
    [[ ! "$name" =~ term|tilda ]] && return 1
    return 0
    name="$(xdotool getwindowname "$(xdotool getwindowfocus)" | tr '[:upper:]' '[:lower:]')"
    echo "$name" > ~/tmp/foo
    [[ ! "$name" =~ term|tilda|@${HOSTNAME} ]] && return 1
    return 0
    }

    init && run
    init && run
  6. @coderofsalvation 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
  7. @coderofsalvation 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

  8. @coderofsalvation 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
  9. @coderofsalvation 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
  10. @coderofsalvation 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
  11. @coderofsalvation 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(){
  12. @coderofsalvation 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

  13. @coderofsalvation 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
    }

  14. @coderofsalvation 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

  15. @coderofsalvation 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"
  16. @coderofsalvation 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