❯~/pixelastic

Commandline escaping magic

Aren't your tired of having to wrap your url in quotes when using wget so the various & and ? do not mess up your terminal?

Well, I was, until I stumbled upon this god-send zsh magic: url-quote- magic.

Just initiate it in your .zshrc with the following lines:

autoload -U url-quote-magic
zle -N self-insert url-quote-magic

And now, everytime you type or paste a url in wget it will automatically escape the needed characters. It is quite clever and works with other commands that would otherwise trigger your terminal globbing feature.

Alternate cp and mv commands using rsync

Sometime you need to copy or move files, but preserve their ownership, timestamp or simply want to update the changing bits instead of blindly moving whole chunks of data.

I have two simple aliases to do just that. Meet rcp and rmv:

function rcp() { rsync -rahP --modify-window=1 "$@" }
function rmv() { rsync -rahP --modify-window=1 --prune-empty-dirs --remove-sent-files "$@" }
compdef _cp rcp rmv 

They are defined as functions and not aliases so I can define which completion functions zsh will use (that's the compdef part). In that case, I want them to use the same completion as for the basic cp command.

Converting html files to pdf from the commandline

When you need to convert an html file to a pdf, often the default pdf printer of your OS is enough. For those times when you need a much better rendering, you need a better tool.

I had to convert my html/css resume to a pdf file today. The default pdf printer on Ubuntu was discarding the background color and adding useless margins.

I finally resorted to using wkhtmltopdf. It does the job, but needs a few tweaks. First, you have to add -T 0 -R 0 -B 0 -L 0 to remove the margins. Then, you have to use the file:// notation to target a local file. Also, it does not understand the css properties page-break-after nor page- break-before. I had to manually add padding in my elements to stop them from being cut in between two pages. But worst of all, it chokes on UTF-8 characters in filenames.

In the end, I wrote a small ruby wrapper around wkhtmltopdf to work around all those issues for me and called it [html2pdf](https://github.com/pixelastic/oro shi/blob/master/scripts/bin/html2pdf).

Preventing the Meta key from opening the dash in Unity Ubuntu

Everytime I install Ubuntu on a fresh computer and try to define custom keybindings that use the meta (ie. Windows) key, I run into issues.

The Ubuntu UI uses the meta key to open what they call the "Dash". I never use it and want my meta key back to define my own keybindings.

There is no way to disable it from the default Ubuntu options. You have to install compizconfig-settings-manager and launch it using ccsm. There, go to the Ubuntu Unity Plugin and disable the "Key to show the launcher" binding.

You will now be free to use the meta key in your keybindings, again.

git resurrect to bring back deleted files from the dead

Ever deleted a file several commits away and then suddenly realizing that you actually need it? Well, here's an easy way to get your file back.

First of all, we'll get the commit in which the file has been deleted with: git log --diff-filter=D --format=format:%H -- {your_filename}

--diff-filter=D will only keep commits where files gets deleted, --format=format:%H will only display the commit hash and finally the -- is a separator between your options and the argument. Argument should be your filename, and it even accepts * as a wildcard.

Once you got the commit hash, it's just a matter of checking the file out using git checkout {hash}~1 {your_filename}. The ~1 targets the previous commit, right before the file gets deleted.

All wrapped up in a nice shell script, here is what it looks like:

#!/usr/bin/env bash
filename=$1
# We first find the commit where the file was deleted
hash=$(git log --diff-filter=D --format=format:%H -- $filename)
# We then bring it back from the dead
git checkout $hash~1 $filename

I've aliased mine to git resurrect.