jasonwryan.com

Miscellaneous ephemera…

Tarsnap

image

I have been using Tarsnap for a couple of months now to backup my EeePC. Tarsnap’s (superb) tagline is: online backups for the truly paranoid.

Essentially, Tarsnap stores your encrypted backups on Amazon’s S3 servers, recognizes duplicate data and avoids storing it again, and only deletes data once no remaining archives require it. I can recommend it as a terrific service for a number of reasons.

It is secure. Developed by the FreeBSD Security Officer, there is a bug bounty for anyone who does discover bugs or security holes. It is very reasonably priced. It is reliable. And most importantly, Dr Colin Percival, erstwhile child prodigy and developer, is extremely helpful either on the mailing list or IRC if you have any questions or need help.

If, however, you have a quick look at the man page, you will see that it is not exactly a simple application. After a couple of months of trying to remember all of the relevant flags, I searched for a script that would simplify the process for me. There are a few online, but they are all intended to fully automate the process (which makes sense for something like backups)…

What I was looking for, however, was something more interactive: more along the lines of a prompt. So, realizing I would have to scratch my own itch, I wrote a wrapper script to assist my failing memory.

The script prints out a menu of the available options and, once you have selected the activity that you would like (creating a new snapshot, deleting an old one etc.,), then the script checks if any further input is required, for example, the name of the snapshot to delete, and then executes it. It is nothing flash, but it works…

image

You can grab the script from my mercurial repo. It’s written in bash, but if you require portability, it shouldn’t be too hard to tweak.

Updated 3/10/11

I have rewritten the script to be POSIX compliant and also to better conform to basic UNIX behaviours, in terms of the options etc. It should now be good to run in most shells.

Notes

Creative Commons licensed image by Declan TM

Script to Edit Config Files

image

Even with bash completion, endlessly typing and tabbing through the directory tree to make a simple change to a config file quickly becomes tedious, so I concocted this script to make it somewhat more straightforward.

xdg.sh
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
dir=$XDG_CONFIG_HOME/$1
if [ -d "$dir" ]; then 
    for f in $dir/* ; do
    file=${f##*/}
        case $file in
        conf | config | *.cfg | *rc)  $EDITOR "$f" ;;
        *)  :  ;;
        esac
    done
fi

A relatively simple script, with a nice feature or two, it does a couple of things. First, it tests that the directory actually exists:

1
    if [ -d "$dir" ]

and then loops through all of the files in the directory using a glob and checks for a pattern match.

In order for the case statement to work, however, the directory path has to be stripped from the file name.

Rather than run another process with basename, I used parameter expansion to remove the substring:

1
    file=${f##*/}

which turns, for example, $XDG_CONGIG_HOME/newsbeuter/conf into a simple conf. This can then be evaluated for a match with the file names in the case statement and passed to the $EDITOR.

If no match is found, nothing (:) is done.

Now, it is simply a matter of entering xdg vimprobable and $XDG_CONFIG_HOME/vimprobable/vimprobablerc is opened in Vim.

Updated 19/10/11

After using this script for a while it became clear to me that it has a significant drawback: too many applications place their config files in directories other than $XDG_CONFIG_HOME. So, with some help, I updated the script. It now covers all of the directories where you are likely to find dotfiles.

xdg.sh v2
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
dirs=($HOME/.$1* $HOME/.$1/ $XDG_CONFIG_HOME/$1/)
IFS=$'\n'
read -r -d '' -a files < \
    <(find "${dirs[@]}" -type f \( \
           -name "*.conf" \
        -o -name "conf" \
        -o -name "config" \
        -o -name "*rc" \
        -o -name "*.cfg" \) 2>/dev/null)

(( ${#files[*]} )) && "$EDITOR" "${files}"

If you would prefer a more portable version—which still relies on GNU find—then you could simplify it like so:

xdg.sh v3
1
2
3
4
5
6
7
8
9
10
#!/bin/sh
dirs=($HOME/.$1* $HOME/.$1/ $XDG_CONFIG_HOME/$1/)

find "${dirs[@]}" -type f \( \
       -name "*.conf" \
    -o -name "conf" \
    -o -name "config" \
    -o -name "*rc" \
    -o -name "*.cfg" \) \
    -exec "$EDITOR" {} +

Using Vimprobable

image

I posted last year about Vimprobable a webkit browser that uses Vim-like keybinds and can be run completely without the rodent.

Over the last couple of months I have become increasingly comfortable with it, and with the addition of some great recent functionality, it has become my default browser.

The project has good documentation, in the form of two man pages: vimprobable and vimprobablerc, but I thought I’d share some of my customizations and working methods.

Copy & Paste

The only time I have been tempted to take my hand off the keyboard was to insert the cursor at the correct character to begin a selection to copy and paste. This is unnecessary. With the right options in $XDG_CONFIG_HOME/vimprobable/vimprobablerc, you can do it all from the keyboard.

Map the keys you would like to use to enter and exit caret mode. I use Ctrlc to enter and Shiftc to return to normal mode:

1
2
map <C-c>=:set caret=true
map <S-c>=:set caret=false

Then, it is just a matter of identifying the text that you wish to copy (in the photo above I was copying a script to launch tabbed), and highlighting it via search. So the sequence is:

1.  / - to enter search mode
2.  #! Enter - the shebang and then enter to highlight the correct
    sequence
3.  Ctrlc to enter caret mode
4.  and then ShiftUp or ShiftDown to highlight the following or
    preceding lines
5.  yanking the selected text is Shifty, and the text is now stored in
    your clipboard.

Opening new instances in tabbed

I highlighted the section on tabbed from the Vimprobable site for a reason. The second tip is how to set up tabbed to work well with other applications.

By default, any new windows (ie., instances of Vimprobable) opened from within Vimprobable are “caught”—and managed— by tabbed. However, if you open a link from, say newsbeuter or Tyrs, this will open an instance outside tabbed which, if you are using a tiling window manager like dwm, means this window gets pushed to master in your stack. This is not entirely satisfactory.

Thanks to Connor Lane Smith on the suckless ML, I have a solution to have all new instances of Vimprobable sent to tabbed, whether opened in another application, or if I click on a link in my terminal, Urxvtc.

CLS’s solution is elegantly simple. Launch tabbed with an .xid file in /tmp:

1
"$(tabbed -d >/tmp/tabbed.xid); vimprobable2 -e $(</tmp/tabbed.xid)"

Then it is just a matter of setting up other applications to pass links to the tabbed tmp file. For example, in my $XDG_CONFIG_HOME/tyrs/tyrs.cfg, I have:

1
openurl_command = vimprobable2 -e $(</tmp/tabbed.xid) %s

The workaround for Urxvtc was a little trickier. I finally got there with some help from Riccardo Murri on Unix & Linux SE. In my ~/.Xdefaults I have:

1
2
3
4
! -- Clickable urls -- !
URxvt.perl-ext-common: default,matcher
URxvt.urlLauncher: /home/jason/Scripts/vimprobtab.sh
URxvt.matcher.button: 1

…and the wrapper script vimprobtab.sh looks like this:

1
2
3
#!/bin/bash
# Script to open Vimprobable instances in tabbed
exec vimprobable2 -e $(</tmp/tabbed.xid) "$1"

Now whenever I open a link in an application or by clicking a link in the terminal (in irssi, for example) it is passed to Vimprobable in tabbed.