Snippets

n snippy

Created by n last modified

sinppy

These scripts are 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
DMENU_ARGS="-b"
XSEL_ARGS="--clipboard --input"

cd ${DIR}

# Use the filenames in the snippy directory as menu entries.
# Get the menu selection from the user (ignore hidden files and directories).
FILE=`find .  -type f | grep -v '/\.' | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS}`

if [ -f ${DIR}/${FILE} ]; then
  #save old content
  oldselect=$(xsel ${xsel_ARGS})
  # Put the contents of the selected file into the paste buffer.
  xsel ${XSEL_ARGS} < ${DIR}/${FILE}
  # Paste into the current application.
  xdotool key ctrl+v
  #restore old clipboard
  echo -n ${oldselect} | xsel ${XSEL_ARGS}
fi

#!/bin/bash
#
# Based on "snippy" by "sessy" 
# (https://bbs.archlinux.org/viewtopic.php?id=71938)
#
# This version may be more convenient for people who only need 
# one-line snippets, because all your snippets can go in one file.
#
# You will also need "dmenu", "xsel" and "xdotool". Get them from your linux
# distro in the usual way.
#
# To use:
# 1. Create a file in your home directory called ".snippyrc".
#
# 2. The format of the file is shown below:
#
#        [tag] text_to_paste
#
#    Where "[tag]" starts in column 1. For example:
#
#        [hw] Hello, world!
#        [wombatSmilie] [img]http://nualeargais.ie/pictiuir/emoticons/wombatSmilie.gif[/img]
#
# 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")
#
CONFIG=${HOME}/.snippy/.snippyrc
DMENU_ARGS=""
XSEL_ARGS="--clipboard --input"

# Display the menu and get the selection
SELECTION=`sed 's/\].*/]/' ${CONFIG} | /usr/bin/dmenu ${DMENU_ARGS}`

# Strip out the square brackets...
PATTERN=`echo ${SELECTION} | tr -d "[]"`

# ...and put them back in, escaped with a backslash.
# Get the text associated with the selection.
TEXT=`grep "\[${PATTERN}\]" ~/.snippyrc | sed "s/\[${PATTERN}\] //"`

if [ "${TEXT}" ]; then
  #save old content
  oldselect=$(xsel ${xsel_ARGS})
  # Put the selected string (without the trailing newline) into the paste buffer.
  echo -n ${TEXT} | xsel ${XSEL_ARGS}
  # Paste into the current application.
  xdotool key ctrl+v
  #restore old clipboard
  echo -n ${oldselect} | xsel ${XSEL_ARGS}
fi

snippy1lineq.sh
#!/bin/bash
#
# Based on "snippy" by "sessy" 
# (https://bbs.archlinux.org/viewtopic.php?id=71938)
#
# This version may be more convenient for people who only need 
# one-line snippets, because all your snippets can go in one file.
#
# You will also need "dmenu", "xsel" and "xdotool". Get them from your linux
# distro in the usual way.
#
# To use:
# 1. Create a file in your home directory called ".snippyrc".
#
# 2. The format of the file is shown below:
#
#        [tag] text_to_paste
#
#    Where "[tag]" starts in column 1. For example:
#
#        [hw] Hello, world!
#        [wombatSmilie] [img]http://nualeargais.ie/pictiuir/emoticons/wombatSmilie.gif[/img]
#
# 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")
#
CONFIG=${HOME}/.snippy/.snippyrc
NUM_LINES=`cat ${CONFIG} | wc -l`
DMENU_ARGS="-l ${NUM_LINES}"
#DMENU_ARGS="-l 1"
XSEL_ARGS="--clipboard --input"

# Display the menu and get the selection
SELECTION=`sed 's/\].*/]/' ${CONFIG} | /usr/bin/dmenu ${DMENU_ARGS}`

# Strip out the square brackets...
PATTERN=`echo ${SELECTION} | tr -d "[]"`

# ...and put them back in, escaped with a backslash.
# Get the text associated with the selection.
TEXT=`grep "\[${PATTERN}\]" ${CONFIG} | sed "s/\[${PATTERN}\] //"`

if [ "${TEXT}" ]; then
  echo -n ${TEXT} | copyq write text/plain -
  # Paste into the current application.
  copyq select 0
  copyq paste
  # Restore old clipboard content
  copyq select 1
fi

#!/bin/bash
#
# novakmi
# based on https://gist.github.com/coderofsalvation/46549e3788ade2f3a938
# Modified menu reading and sorintg from file
#
# 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"; :>$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
# @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(){
  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
}

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.

# novakmi FILE=`find -L .  -type f | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS}`
  FILE=`find .  -type f | grep -v '/\.' | grep -v '^\.$' | sed 's!\.\/!!' | sort | /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
}


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

init && run
snippy_coderforsalvation_mod.sh
#!/bin/bash
#
# Based on "snippy" by "sessy" 
# (https://bbs.archlinux.org/viewtopic.php?id=71938)
#
# You will also need "dmenu" and "copyq". 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
DMENU_ARGS="-b"

cd ${DIR}

# Use the filenames in the snippy directory as menu entries.
# Get the menu selection from the user (ignore hidden files and directories).
FILE=`find .  -type f | grep -v '/\.' | grep -v '^\.$' | sed 's!\.\/!!' | sort | /usr/bin/dmenu ${DMENU_ARGS}`

if [ -f ${DIR}/${FILE} ]; then
  # Put the contents of the selected file into the paste buffer.
  copyq write text/plain - < ${DIR}/${FILE}
  # Paste into the current application.
  copyq select 0
  copyq paste
  # Restore old clipboard content
  copyq select 1
fi

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.