Source: https://github.com/orschiro/scriptlets/ … ter/Snippy
]]>I didn't really like it that my clipboard was wiped through this, so I added two lines that saves the current content of your clipboard and then puts it back into it when it's finished pasting. My bash skills aren't really good, but it seems to work for me.
snippy
#!/bin/bash
#
# 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.
FILE=`find . -type f | 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
snippy1line
#!/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}/.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
I also fixed the two errors in the 1line version that chickenPie4tea pointed out.
Hope this is useful for somebody.
]]>Also with the 1line version I think there is an error on line 42 as it is not pointing to the .snippyrc file but to .snippets by mistake
Also (mentioned to me by GoTbleTU (of youtube fame) that it doesn't work if called from a hotkey as line 31
should read
XSEL_ARGS="--clipboard --input"
First of all, I like to thank you for this great little tool. However, I'm having problems with snippy inserting a line break after the snippet, even though there's no line break in the file defining the snippet.
It seams like it's the xsel command that may be causing this..?
Here are two versions of "snippy" that I created. Some of the features I added might be useful to others. Read the comments to see what's new, and to decide if you want to use one of the scripts. You only need one of them. (Although the could live side-by-side.
snippy
#!/bin/bash
#
# 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.
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.
xsel ${XSEL_ARGS} < ${DIR}/${FILE}
# Paste into the current application.
xdotool key ctrl+v
fi
snippy1line
#!/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}/.snippyrc
DMENU_ARGS="-b"
XSEL_ARGS="--clipboard"
# 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}\]" ~/.snippets | sed "s/\[${PATTERN}\] //"`
if [ "${TEXT}" ]; then
# 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
fi
What would be really cool is generalizing the caching script so any use of dmenu could use it (have different caches) -- for example -- normal dmenu, snippy, mpc playlist launcher.
Scott
I think I saw a patch in C for dmenu sometime that allowed a commandline arg to use a specific cache file. So that's probably exactly what you're looking for.
Personally I've never felt a need for it though
Oh btw, for those guys who use uzbl, I wrote snips which let you pick url's from your uzbl history/bookmarks. see http://github.com/Dieterbe/snip/tree/ma … les/snips/
]]>what about `ls -tu` ? this sorts files by access time. no need for extra caching scripts or files.
Well, that would work, except it wouldn't sort by frequency which is what brisbin33 solved w/ his caching script. I like having my regular dmenu pop up with my most used programs first, instead of alphabetical or most recently accessed (which is not necessarily most used). I don't think that would necessarily be important to other people, but it certainly could be an option.
Here's the code for launching dmenu with the caching feature (.dmenurc is sort of like your $DMENU_ARGS)
#!/bin/bash
if [ -f $HOME/.dmenurc ]; then
. $HOME/.dmenurc
else
DMENU='dmenu -i'
fi
TERMI='urxvt -e bash -lic'
CACHE="$HOME/.dmenu_cache_recent"
touch $CACHE
MOST_USED=$(LC_ALL="C"; sort $CACHE | uniq -c | sort -r | colrm 1 8)
RUN=$( (echo "$MOST_USED"; dmenu_path | grep -vxF "$MOST_USED") | $DMENU) && \n(echo $RUN; head -n99 $CACHE) > $CACHE.$$ && \nmv $CACHE.$$ $CACHE
case $RUN in
*\;) $TERMI ${RUN/;/} & ;;
*) $RUN & ;;
esac
exit 0
.dmenurc:
# makes dmenu identical across all scripts by sourcing this file
# aur/dmenu-vertical-xft required for xft fonts
DMENU='dmenu -i -fn xft:Inconsolata-10 -r -x 0 -y 17 -nb #303030 -nf #909090 -sb #909090 -sf #303030'
Again I take NO credit for this code!! I'm just passing it on because I like it Thanks brisbin33! What would be really cool is generalizing the caching script so any use of dmenu could use it (have different caches) -- for example -- normal dmenu, snippy, mpc playlist launcher.
Scott
]]>#!/bin/sh
if [ x"$XDG_DATA_HOME" != "x" ]
then
snips=$XDG_DATA_HOME/snip/snips
else
snips=$HOME/.local/share/snip/snips
fi
[ -d $snips ] || mkdir -p $snips
if [ x"$DMENU_ARGS" == "x" ]
then
DMENU_ARGS='-p snip: '
fi
if [ "$1" == 'load' ]
then
#if [ "$2" == 'name' ]
snip="$snips/$(ls $snips | dmenu $DMENU_ARGS)"
if [ -f $snip ]
then
if [ -x $snip ]
then
cb=`xclip -o -selection clipboard`
printf `"$snip"` | xsel -b -i
xdotool key ctrl+v
printf $cb | xsel -b -i
else
cb=`xclip -o -selection clipboard`
printf `cat "$snip"` | xsel -b -i
xdotool key ctrl+v
printf $cb | xsel -b -i
fi
fi
# this only works for (and is only useful for) static snips
# make dynamic snips manually
elif [ "$1" == 'save' ]
then
txt=$(xsel)
if [ x"$txt" == 'x' ]
then
txt=$(zenity --title "Save snip" --entry --text "Text to put in snip")
fi
name=$(zenity --title "Save snip" --entry --text "Filename to save snip as")
echo "$txt" > $snips/$name
else
echo "No valid arguments passed" >&2
exit 2
fi
edit01: Um, well, it seems to be broken in some cases anyway, please ignore everything I've posted...
edit02: Ok, it kinda works, but the snippet files have to end with a linebreak and it doesn't perform in a shell.
edit03: update - now can't handle multiple lines. but works better
]]>Oh, and I've removed the check for executability because I thought it wouldn't be necessary this way. Maybe I broke something...
well, it was there for a reason. see http://github.com/Dieterbe/snip/blob/master/README for more info
]]>#!/bin/sh
if [ x"$XDG_DATA_HOME" != "x" ]
then
snips=$XDG_DATA_HOME/snip/snips
else
snips=$HOME/.local/share/snip/snips
fi
[ -d $snips ] || mkdir -p $snips
if [ x"$DMENU_ARGS" == "x" ]
then
DMENU_ARGS='-p snip: '
fi
if [ "$1" == 'load' ]
then
#if [ "$2" == 'name' ]
snip="$snips/$(ls $snips | dmenu $DMENU_ARGS)"
if [ -f $snip ]
then
cb=`xclip -o -selection clipboard`
xclip -i -selection clipboard $snip
xdotool key ctrl+v
xdotool key BackSpace
printf $cb | xclip -selection clipboard
fi
# this only works for (and is only useful for) static snips
# make dynamic snips manually
elif [ "$1" == 'save' ]
then
txt=$(xsel)
if [ x"$txt" == 'x' ]
then
txt=$(zenity --title "Save snip" --entry --text "Text to put in snip")
fi
name=$(zenity --title "Save snip" --entry --text "Filename to save snip as")
echo "$txt" > $snips/$name
else
echo "No valid arguments passed" >&2
exit 2
fi
@Dieter@be -- awesome! works great so far. I'm gonna integrate the little caching script that brisbin33 wrote for dmenu, so the most commonly used snips appear first.
Scott
what about `ls -tu` ? this sorts files by access time. no need for extra caching scripts or files.
that said, I think the default behavior of ls (sort by name) is also fine. maybe i should introduce a config file for snip where you can set DMENU_ARGS and LS_ARGS
With regard to original post:
cat "Hello World!" > ~/.snippy/hw
Shouldn't this be `echo`?
indeed
]]>cat "Hello World!" > ~/.snippy/hw
Shouldn't this be `echo`?
]]>Scott
]]>