Skip to content

Variations on the snippets script

jasondunsmore edited this page Sep 13, 2010 · 4 revisions

KDE users

If you’re using KDE, where “Shift + Insert” is bound to the copy/paste buffer, the following script should be used:

#!/bin/bash
rs=~/rhel-snippets
exe=$( ls -B $rs | dmenu -b )
echo $exe >> $rs/.stats
cat $rs/$exe | sed -e '/^\#/d' -e '/^$/d' | perl -pe 'chop if eof' | xclip
xdotool mousedown 2 && xdotool mouseup 2

Displaying documentation when selecting a script

Using Xdialog

apt-get install xdialog

Put the following in ~/bin/snippets:

#!/bin/bash
exe=$( ls -B ~/rhel-snippets | dmenu -b )
cat ~/rhel-snippets/$exe | sed -e '/^\#/d' -e '/^$/d' | perl -pe 'chop if eof' | xclip
xdotool key "shift+Insert"
tmp=`mktemp`
cat ~/rhel-snippets/$exe | sed -n '/^#/p'|sed 's/^# *//' > $tmp
if [ -s $tmp ]; then
    Xdialog --fixed-font --textbox $tmp 15 60
fi

Using Emacs.

Put the following in ~/bin/snippets:

#!/bin/bash
rs=~/rhel-snippets
exe=$( ls -B $rs | dmenu -b )
echo $exe >> $rs/.stats
cat $rs/$exe | sed -e '/^\#/d' -e '/^$/d' | perl -pe 'chop if eof' | xclip
xdotool key "shift+Insert"
doc=`cat $rs/$exe | sed -n '/^#/p'|sed 's/^# *//'`
if [ -n "$doc" ]; then
    emacsclient -e "(snippets-doc \"$doc\")"
fi

…and put the following function in ~/.emacs:

(defun snippets-doc (doc)
  (get-buffer-create "*snippets-doc*")
  (set-buffer "*snippets-doc*")
  (erase-buffer)
  (insert doc)
  (display-buffer "*snippets-doc*"))

Emacs-like snippets keybindings for Sawfish

Put the following script in ~/bin/sawppets

#!/bin/bash
rs=~/rhel-snippets
echo $1 >> $rs/.stats
cat $rs/$1 | sed -e '/^\#/d' -e '/^$/d' | perl -pe 'chop if eof' | xclip
xdotool key "shift+Insert"
doc=`cat $rs/$1 | sed -n '/^#/p'|sed 's/^# *//'`
if [ -n "$doc" ]; then
    emacsclient -e "(snippets-doc \"$doc\")"
fi

And then add the following configuration to .sawfishrc

(define sawppets-keymap (make-keymap))
(bind-keys global-keymap "C-z" sawppets-keymap)

(bind-keys sawppets-keymap "o" (lambda () (system "sawppets server-overview")))
(bind-keys sawppets-keymap "u" (lambda () (system "sawppets get-public-ip")))
(bind-keys sawppets-keymap "z" '(synthesize-event "C-z" (input-focus)))

(define sawppets-f-keymap (make-keymap))
(bind-keys sawppets-keymap "f" sawppets-f-keymap)
(bind-keys sawppets-f-keymap "b" (lambda () (system "sawppets file-backup")))

(define sawppets-a-keymap (make-keymap))
(bind-keys sawppets-keymap "a" sawppets-a-keymap)
(bind-keys sawppets-a-keymap "b" (lambda () (system "sawppets apache-buddy")))
(bind-keys sawppets-a-keymap "g" (lambda () (system "sawppets apache-log-growers")))

Restart Sawfish, and the keybindings will be in effect.