Vim and the X clipboard

Usually when I needed to paste stuff from a text file into a GUI program (most commonly, the browser), I resorted to opening the text file in gedit and copying/pasting from there. Using the X clipboard by selecting text with the mouse kinda worked, but it’s subject to Vim’s visual representation of the text, which may include unwanted display-related breaks. So using gedit was the easiest, but also awfully kludgy solution.

I did some research and learned that vim does have direct access to the X clipboard. I tried the commands they mention (basically “+y to yank selected text, then I tried to paste in a GUI application; or “+p to paste from the current X clipboard). They didn’t work. My installed version of Vim in Ubuntu lacked the xterm_clipboard setting. I was in despair!

Then I came across this bug report in Launchpad. Upon reading it I realized that it was as simple as installing vim-gtk. I had never considered this, as it includes a graphical Vim version which I have absolutely no use for. However the bug report mentions that it also includes a text version of vim compiled with X clipboard support. So I installed, fired up Vim, and the feature works well!

I can now have a buffer with long lines, with :set wrap and :set linebreak, which would be afwul if I cut/pasted it with the mouse. I can select text using vim commands and just yank it into the + register, and it’s instantly available in the X clipboard. Bliss!


El gabinete de los viejitos

Cuando sea hora de votar, acuérdense de esto:

Josefina Vázquez Mota, afirmó que cuando ve a otro aspirante que nombra a su gabinete, pienso que suman como mil 500 años de edad, y que al observar cómo en otro partido se integra a su consejo político a ex gobernadores, algunos con historias terribles, pienso que suman como mil 500 años de prisión.

Acuérdense de estos comentarios a la hora de ir a votar, acuérdense de cómo desprecia a la gente simplemente por su edad, cómo para ella la experiencia no vale nada, y piensen si así descalifica y menosprecia a sus colegas políticos, ¿qué hará con los planes de apoyo a la tercera edad?.

Elecciones – Desconocidos vs. Superestrellas

¿Por quién prefieres votar? ¿Por Vázquez Mota y su equipo de “desconocidos” que van a seguir hundiendo al país tratándolo como si fuera una empresa?

¿O por este equipo de superestrellas de la ciencia, el arte, la política y la economía?

Y la neta, si no los conoces, ve investigando quienes son. La decisión es tuya.

Ubuntu and Juju with local providers

Want to play with Ubuntu’s awesome Juju but don’t want to get into the hassle of getting EC2 configured?

It’s actually pretty easy to set up a local provider to experiment with this.

You need to be running Ubuntu 12.04 (yes, it’s not released yet but you can use the beta version or daily  images). Oh, and you can install this on a virtual machine if you really don’t want Juju to mess with your actual system.

Make sure to have a valid SSH key, if you don’t have one, create it with



apt-get install juju llibvirt-bin lxc apt-cacher-ng zookeeper

Once the packages are installed, run

newgrp libvirtd

this adds you to the libvirtd group at runtime.

Then run juju bootstrap. Juju will complain about a config file or something. Ignore it! and then edit .juju/environments.yaml and replace everything on that file with this:

    type: local
    data-dir: /tmp/local-dev
    admin-secret: b3a5dee4fb8c4fc9a4db04751e5936f4
    default-series: oneiric

The admin-secret is a MD5 random key, you should probably generate your own with something like this:

head -c 10 /dev/urandom |md5sum

Then finally it’s time to bootstrap things:

juju bootstrap

This will exit pretty quickly, but things are not ready yet. Note that it will take a few minutes to get packages and actually prepare the nodes.

Once your juju is bootstrapped you can follow the rest of the steps here:

Find and -exec and why doesn’t basename work in there?

Ever wonder why this doesn’t work to rename all your isos to .nrg?

find ./ -iname *iso -exec rename {} `basename {} .iso`.nrg \;

The thing is, the backticks are a bash construct, and whatever you specify to -exec does *not* get shell-expanded. So it’s being passed verbatim, with the only substitution being the {} for the found filenames.

But this works! (though quoting may pose some challenges). This works because it *is* being processed through bash, so all the normal expansion and shell tricks will work.

find ./ -iname *iso -exec bash -c "rename {} `basename {} .iso`.nrg"  \;

Notifications – during and after launching

When I launch a long-running process I like to forget about it, but how do I know when it’s finished?

You can of course have it send an email after it finishes:

long_process; echo "finished" |mail -s "finished"

For this to work, it’s very useful to have ssmtp configured, so you have a sane, working local SMTP agent.

You can also send the notification only if the command succeeds:

long_process && echo "finished" |mail -s "finished"

OK, so you forgot to add the notification to your initial command line. You can use a loop to monitor a particular process and notify you when it’s done.

In this case I’ll be monitoring an instance of netcat. Determining the process name is up to you 🙂 The delimiters $ and ^ look for the executable names only.

The while loop will run while the process exists; once the process disappears the loop continues with the next instruction in the line, which is popping up an alert on the desktop and then sending an email. So if I’m not glued to the desktop, I’ll still get an email when this is done.

while pgrep $nc^; do sleep 5; done; alert; (echo “finished” |mail -s “finished” you

find’s printf action

If you use find, it outputs full paths, which may not always be desirable. Turns out find has a -printf action with which you can do  niceties such as outputting plain filenames (as if you’d used basename on them, but this means one less command on your pipeline):

find data/{audio,documents,images,video,websites}/ -type f -printf "%f\n"

The -printf command has a lot of formatting variables and possibilites! give it a try, look at the man page for more information.

ASCII video rendering

If you’re a CLI jockey you may enjoy looking at a nice ASCII rendering of your face via your webcam:

mplayer -vo caca tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0

Or to watch your favorite video in ASCII rendering:

vlc –vout caca some-file.avi



Random picking without repetition

So the problem was to draw people at random from a list. The list is contained in a leads.txt text file, one per line.

This nifty one-liner will output a randomly-picked person from that file every time it’s invoked. it’ll then remove the name from the file so it doesn’t get repeated again.

export i=`sort leads.txt |shuf  |head -1` ;  echo $i; sed -i "s/^$i$//;/^$/d" leads.txt

It can be shortened by changing shuf |head 1 to shuf -h 1.

If you’d rather avoid deleting already-chosen entries from the file, this version just comments the names it picks:

export i=`sort leads.txt |grep -v \# |shuf |head -1` ;echo $i;  sed -i "s/^$i$/#$i/" leads.txt

Building Debian/Ubuntu packages with sbuild

Many of the on-line instructions and tutorials are quite complicated. Why? It was easy for me:

sudo apt-get install sbuild

To build a virtual machine:

mk-sbuild --distro=ubuntu --arch=i386 precise

this will create a schroot in /var/lib/schroots/precise-i386. Note how it appends the architecture to the schroot name. Also note that the first time you run mk-sbuild, it’ll show you a configuration file and configure your environment. I didn’t change anything in the config file, I used it “as it was”. When it prompts you to log out, do it, otherwise things won’t work.

OK now you want to build a package using your chroot with sbuild:

sbuild -A -d precise package.dsc

This will build the package on precise for ALL available architectures. Note that -d is just “precise”; the -A flag will tell sbuild to build architecture: any packages for all available architectures (so if you have amd64 and i386 chroots, it’ll do the right thing and build two packages).

If you want to build arch-specific packages:

sbuild  -d precise-i386 package.dsc

This will magically build for the given architecture (i386). Note that arch: any packages will also be built.

You can also specify the arch as a parameter (but then you have to leave it out of the -d name):

sbuild  -d precise --arch=i386 package.dsc

This will not work:

sbuild  -d precise-i386 --arch=i386 package.dsc

Using diff on the output of two commands – named pipe and bash magic

Ever wanted to diff the output of two commands? Usually it’s done by first piping each command to a temporary file and then diffing them.

The following syntax creates a named pipe for the command and uses the pipe’s name instead of a filename. Bash takes care of everything automagically so all you have to do is:

sort <(cat /etc/passwd)

That’s a dumb example, but how about this?

diff <(command1) <(command2)

The commands can be as complicated as you need them to be!

Countdown bash function

To be put in your .bashrc. Combines a waiting period with a (simple) progress report.

countdown () { [ "$1" != "" ] || return && i=$1; while [ $i -gt 0 ]; do i=$(($i-1)); echo -n "$i " ; sleep 1; done; echo "LIFTOFF"; }

The perfect keyboard layout?

I remember an easier time when all keyboards had the same layout (C-64, anyone?) and if you wanted to type special characters you had to resort to arcane command sequences, if they were at all possible.

My, how times have changed.

My first PC compatible had a spanish keyboard, and you could very simplistically tell the OS (MS-DOS) about your keyboard layout. For a while this worked pretty well. Then someone decided that Latin America was so different from Spain, that we needed our very own keyboard layout; this layout just moves stuff around needlessly, destroying many years of experience for those of us who were accustomed to the spanish keyboard. I understand removing the ç as it’s not used in Latin America, but why move all the rest of the stuff around?

Latin American Keyboard

So basically I got used to the spanish keyboard which has worked well in all kinds of OSes, from MS-DOS to Windows, OS/2 and yes, Linux.
While the Latin American layout was such a pariah that, at some point, it got overwritten by the Latvian keyboard (la), so when doing a system upgrade, all of a sudden your keyboard was in latvian, and you had to select “latam” for Latin America.

French Canadian Keyboard

Eventually I happened to get a laptop with a Canadian French keyboard. Luckily, this is not the dreaded french AZERTY keyboard, but basically an english keyboard layout with most symbol keys mapped very strangely. So if you want to type the basic alphabet you’re OK, like you’d be with an english keyboard, but things start getting weird when you need to create special characters or compose accents, cedillas and stuff like that. This was so different from any other layout I’ve used, that I was basically freaking out. I could just ignore the red characters on my keyboard, and/or use it as just an english keyboard, but I routinely need to compose text in spanish and in french, so how would I go about doing this?

And no, the ages-old trick of memorizing ASCII codes for special characters doesn’t cut it: for one, it’s unreliable on Linux (especially on graphical mode), and for another, it’s just primitive! I used to chuckle at all the people I’ve seen through the years who had a nice “cheat sheet” glued to their desktop with ASCII codes for frequently-used accented characters, as opposed to taking 15 minutes to correctly configure their keyboards to do this natively.

So anyway, what I came across while checking out the available keyboard maps under Linux and trying to figure out how to type stuff on the Canadian keyboard, was this wonder of wonders, the US International with AltGr Dead Keys layout.

Basically, it takes the right Alt key (labeled AltGr on my keyboard, a monstrosity I was already used to from the LatinAmerican and spanish keyboards) and uses it to “compose” or “deadkey” stuff (dead keys are like accents, for instance, where you press the accent key and then the next letter you type will be accented). In combination with ~, “, ‘ and `, this enables me to type nearly all accented characters with relative ease.

Also, I can use AltGr+vowel to type acute-accented vowels (áéíóú), and AltGr+n for ñ.

Grave accents (è) and tilded letters (ã) can be composed by AltGr+accent (use ` for grave, ~ for tilde), and then the letter you want to type.

What I like about Linux’s keyboard selection thingy is that you can see an actual layout map. Thus, even if my keyboard doesn’t have the characters stenciled in, I can take a quick peek and see where stuff I need might be.

Thus I can do things like use ç or €, all with a minimum of fuss. Also more complicated stuff like ï œ ø is still just one AltGr+key away. All this while preserving a layout that’s very familiar to everyone (english), and where most strange characters using while programming {}][\|~ are also much easier to use than on the spanish keyboard I was used to (it needs AltGr for all sorts of braces and piping, which makes it very painful on my hands).

The actual US International with AltGr deadkeys layout as shown by the Gnome keyboard selection applet.

So there you have it, if you see yourself wrestling with choosing a good physical keyboard layout *and* making it work on your OS, stop pulling your hair out, get an english-layout keyboard and use US International with AltGr Dead Keys!

Can Ubuntu succeed? comparing to iOS and Android

Last week during Ubuntu Developer Summit, head honcho Mark Shuttleworth said something to the effect of “iOS and Android have managed to succeed despite Microsoft’s monopoly,  and we haven’t” (see the keynote here). As a few days passed I thought about it a bit and here’s what resulted.

I think it’s not quite as clear-cut as “they have done it and we haven’t”. Microsoft’s monopoly is on the desktop, and it is there that Ubuntu is going directly against Microsoft and perhaps, yes, failing to capture a percentually significant chunk of the market. And I won’t go into the whole “Linux is better than Windows” debate.

Rather, let me point out a key fact about Android’s and iOS’s success: they both did so in a market where Microsoft wasn’t a dominant player. Before Apple unleashed the iPhone on the world, the smartphone market was very fragmented, with Microsoft a relevant player (Windows Mobile), but nowhere near the dominance it has in the desktop. Nokia (Symbian) and RIM (Blackberry OS) were two big players, but they have both been relegated, one to irrelevance (Nokia – plus the deal with Microsoft), the other (RIM) to a frankly defensive posture where they lack a strategy and are scrambling just to stop the exodus of users.

Now, even while Apple and Google are the two strongest players in the smartphone market, things are pretty much in a state of flux, and no platform can claim the stranglehold that Microsoft has on the desktop. So those companies are forced to innovate and stay on their toes. But the fact is that, even with a product that is clearly superior to previous offerings, any one of these companies would have had a hell of a time dethroning a hugely dominant player from the field.

Ubuntu’s challenge is greater as it’s going head-on on Microsoft’s cash cow, and there’s no real competition for the desktop. The only other mainstream operating system with any success is Mac OS X. Apple is content with whatever niche they’ve carved for themselves, and it’s clear to anyone that the strides they’ve made in the past decade are more due to the halo effect of the iPod and iPhone than because of OS X’s (admittedly great) merits. So yes, they have a superior product, but that still hasn’t propelled them beyond a 10% market share.  While I’m at it, let me comment: it’s easy to forget that the first versions of OSX were kludgy, slow and difficult to use, and had a myriad usability problems. It was the iPod and then the iPhone that propelled Apple from a fringe player into the powerhouse they are today. In the end, Apple realizes that promoting Mac OS X is not worth a big effort, and that the momentum from the iPod and iPhone are enough to keep OS X alive.

So what does Ubuntu need to succeed on the desktop? I have no insight in this topic, but let’s just realize that it’s not as clear-cut as looking at, and imitating, Android’s and Apple’s successes, because as I’ve said, their playing field was a vastly different one. Would a “halo-effect device” help Ubuntu the way the iPhone helped Mac sales? maybe. Maybe all Ubuntu needs is endurance, as even hugely dominant players (Ford, IBM, WordPerfect, Netscape) can be surpassed under the right circumstances.