Friday, June 19, 2015

Links and quotes, April 2015

From The New York Times comes this explainer on the mathematics of fair division. Highlights include an interactive visualisation of Sperner's lemma and a shout-out to the not-for-profit app Spliddit.

In an observational study, The University of Texas at Austin gave a group piano majors a Shostakovich passage to learn and perform a day later. They found that the amount of time spent practicing the passage didn't have much bearing on mastery. What did distinguish the top performers was how they handled their mistakes. The best ones took pains to individually locate and correct errors, addressed them immediately when they arose, and strategically slowed the piece down to address problem areas.

On the pros and cons of trigger warnings as standard classroom practice: "oh god oh god I want to be dead I want to be dead is just not a good mindset to be in when you’re trying to grasp the nuances of Derrida."

Still image of a cresting wave, backlit against the sun, from Ray Collins's "Sea Stills".
Sea Stills: Photographer Ray Collins captures giant waves in otherworldly moments.

Tuesday, June 16, 2015

Bash completion of aliased commands, revisited

When you alias a command in Bash, tab completion no longer works. If you regularly take advantage of tab completion, this undoes most of the convenience of aliasing.

A quick web search throws up an old script by Ole Jorgen that solves this problem by "wrapping" the original completion command with some code that modifies variables to make the complete believe you'd typed out the aliased command in full and are tab completing as per usual.

Unfortunately, running that circa-'08 script on Ubuntu 14.04 LTS (using bash 4.3.11) causes it to choke on most attempted tab completions, with error messages like:

$COMP_POINT: substring expression < 0

The problem: the script modifies two of the variables made available to Bash completion (COMP_CWORD and COMP_WORDS), but misses others. With a little hackery we can modify that script to alter the other variables, COMP_LINE and COMP_POINT.

# Author.: Ole J, Chris C
# Date...: 14.06.2015
# License: Whatever

# Wraps a completion function
# make-completion-wrapper <actual completion function> <name of new func.>
#                         <command name> <list supplied arguments>
# eg.
#     alias agi='apt-get install'
#     make-completion-wrapper _apt_get _apt_get_install apt-get install
# defines a function called _apt_get_install (that's $2) that will complete
# the 'agi' alias. (complete -F _apt_get_install agi)
function make-completion-wrapper () {
    local function_name="$2"
    local arg_count=$(($#-3))
    local comp_function_name="$1"
    shift 2 # For convenience, drop the extracted arguments
    local arg=${@:1}
    local function="
function $function_name {

    local cmdlength


    COMP_WORDS=( "$@" \${COMP_WORDS[@]:1} )

    return 0
    eval "$function"

Then usage proceeds as before:

alias sdr='screen -d -r'
make-completion-wrapper _screen _sdr screen -d -r
complete -F _sdr sdr