You are not logged in.

#26 2010-11-13 22:50:05

mhwombat
Member
Registered: 2010-11-13
Posts: 1
Website

Re: Snippy - poor mans text snippet expander

I'd like to thank sessy for this. An application-agnostic snippet-handler was on my to-do list for quite a while, but now I'm sorted.

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

Offline

#27 2010-11-14 10:32:33

sessy
Member
Registered: 2006-01-20
Posts: 104

Re: Snippy - poor mans text snippet expander

I'm glad You find it useful!

Offline

#28 2013-01-03 15:43:45

Syntaxeus
Member
Registered: 2008-10-04
Posts: 24

Re: Snippy - poor mans text snippet expander

Hi!

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..?

Offline

#29 2013-02-14 09:16:12

chickenPie4tea
Member
Registered: 2012-08-21
Posts: 309

Re: Snippy - poor mans text snippet expander

@mhwombat
Thanks I a using your multi-line version
would it be possible with this script to input more than on line and not all at once?
at the moment if I had a paste file that contained 2 lines of text (say my email address on first line and pass on the 2nd) It would all get pasted at once.
What I would like is the first line to be pasted and then a tab command and then the 2nd line so that if I was loggging into hotmail It would paste my address then tab to the 2nd box and paste the 2nd bit

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"

Last edited by chickenPie4tea (2013-03-07 21:38:11)


You can like linux without becoming a fanatic!

Offline

#30 2013-09-19 16:24:01

vIiRuS
Member
Registered: 2010-06-23
Posts: 18

Re: Snippy - poor mans text snippet expander

Thanks to mhwombat for this script!

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.

Offline

#31 2015-02-16 08:56:05

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: Snippy - poor mans text snippet expander

Adapted Snippy to use the great copyq clipboard manager.

Source: https://github.com/orschiro/scriptlets/ … ter/Snippy

Offline

Board footer

Powered by FluxBB