Gilles Ruppert

a.k.a. elduderino78

Disabling the GUI From Loading in Ubuntu

A quick reminder to myself.

To disable the GUI in Ubuntu (i.e. to run it as a headless VM), do the following:

sudo vim /etc/default/grub

Change: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" to GRUB_CMDLINE_LINUX_DEFAULT="text"

save, then run: sudo update-grub and restart.

To enable gui again, just change from text to quiet splash.

Batch Rename Files on OSX/Unix

I had to rename a bunch of files in a directory by just changing a prefix. Perfect for the command line, no? After a bit of googling (that exists as a verb now, doesn’t it? :-)) I found this article:Batch File Rename By File Extension in Unix

All you need to do is iterate over all of the files in directory, and then rename by echoing the name & running sed on it, i.e.

for i in *; do mv $i `echo $i | sed 's/search/replace/'`; done

If you need to iterate over a tree of directories or only rename certain files, you can run this command

for f in `find . -type f -name "filename"`; do mv $f `echo $f | sed "s/search/replace/"`; done

If you are using git, you can also exchange mv with git mv.

As always: backups and/or version control are your friends!

Find & Sed: Find and Replace on the Command Line

Since using vim, I sometimes miss the great find and replace that TextMate offers, but fear not: find and sed fill the gap quite nicely:

find . -type f -exec sed -i 's/search/replace/g' '{}' \;

To make sure your find and search are looking for the right things, you can do the following 1st:

find . -type f -exec grep 'search' '{}' \;

This command tells find to search all files recursively and pass the to the sed stream editor, which in turn search and replaces all the occurrences (see the g flag at the end, which stands for global).

To avoid certain files, you can exclude them from the find command, i.e.

find . -type f -not \( -name '*.json' \) -exec grep 'search' '{}' \;

This will recursively search all the files, but exclude files with the .json extension.

You can also make this more complex by excluding json and JavaScript files, i.e.

find . -type f -not \( -name '*.json' -o -name '*.js' \) -exec grep 'search' '{}' \;

The -o is the logical or operator.

N.B: the -i argument for the sed command means ‘edit-in-place’. You can pass a value with it to create a backup. This might not be a bad idea. As with most (all?) Linux commands, you fly without a net & there is no undo. Version control is the net I use.

I sometimes get confused about the exact syntax of the command, hence the quick post.

Creating Patch Files With Git

Git has a built in way to create patches and Github has a great write up on this. The git format-patch command is useful if all your changes are committed and you want to create a patch of certain commits.

Sometimes however you have changes that you can’t or don’t want to commit for whatever reason. Git lets you easily create a standard patch file that you can apply on another clone or branch.

1
2
3
4
5
# To create the patch
git diff --no-prefix > path-to-patch/name-of-the-patch-file

# To apply the patch:
patch -p0 < path-to-patch/name-of-the-patch-file

If you have an existing git diff output that didn’t use the --no-prefix argument, you can just apply the patch like this:

1
patch -p1 < path-to-patch/name-of-the-patch-file

Voila.

Integrating JSLint for Vim

A couple of months ago I moved away from TextMate and started using vim for most of my text editing needs. I love TextMate, but I needed a powerful text editor that is also available on other operating systems.

Overall I love vim: it has a steep learning curve, but it’s very powerful.If you are touch-typing and loath the mouse as much as I do, it’s fantastic. But I found myself missing out on some of TextMate’s great bundles, and JSLint integration is one of them. After some googling around and looking at different solutions, this is the one that was easiest to install and I like the functionality.

First download & install JavaScript Lint. On OSX & Linux, you can do so by copying the resulting folder into your PATH. I put it in /usr/local/bin/.

Then get the JavaScriptLint vim plugin by Joe Stelmach and restart vim.

Off you go. When you save a JavaScript file in vim, you will get a window with JSLint warnings. If you want to configure JSLint to show/hide certain errors, you can edit the config (jsl.default.conf) in the folder. You might want to backup the file before editing it.

Happy linting!

Upgrading the Subversion Command Line Client on Mac OSX Leopard

UPDATE: Since I wrote this post, I found a couple of other ways of upgrading the Subversion command line client.

One option is to install the package via MacPorts or Homebrew. If you use Homebrew, you probably don’t have to do anything else, as Homebrew sets up the symlinks automatically. If you install via MacPorts, you have the same issue as you do with the Collabnet installer.

Instead of creating symlinks though, you can also change the PATH environment variable. To do this, open ~/.bash_profile or ~/.bashrc in your favourite editor (vim) and make sure that you have a line similar to this:

1
export PATH=/opt/local/bin:/opt/local/sbin:$PATH

This will make sure that /opt/local/bin/, which contains the MacPorts installs, will be checked before any other locations. MacPorts should add this by itself. This makes sure that svn as installed by MacPorts will be used.

As before: you can check which svn binary you are using by typing which svn and svn --version in the terminal.

Hope this helps!

Original post

OSX Leopard ships with Subversion 1.4.x by default. Since then, SVN has had 2 major upgrades (merge tracking anyone?). To make things easy, there is a binary available at Collab.net. But unfortunately you need to do some work yourself.

The path of the built in Subversion command line client is different to the one that the Collab.net Community binary is installed to. The following steps will fix this.

Disclaimer - Be careful: when you use the Terminal you are flying without a safety net! No undo & no restore from trash. Do this at your own risk and don’t forget to have an up-to-date backup!!!

  1. install the binary downloaded to Collab.net
  2. open your Terminal and check which version of SVN you are using by typing svn --version. This should say 1.4.x
  3. check which installed instance of SVN the OS is using by typing which svn. This will return the path to the SVN version you are using, which probably is /usr/local/bin/svn
  4. check whether the Collab.net version is installed by going to it: cd /opt/subversion/bin. Doing an ls should show you the subversion files
  5. Backup the current svn location: by going to it (cd /usr/local/bin) and copying it to your desktop or wherever you fancy (mv svn* ~/Desktop/)
  6. now you need to create symlinks to the Collab.net binaries:
    • ln -s /opt/subversion/bin/svn svn
    • ln -s /opt/subversion/bin/svnadmin svnadmin
    • ln -s /opt/subversion/bin/svndumpfilter svndumpfilter
    • ln -s /opt/subversion/bin/svnlook svnlook
    • ln -s /opt/subversion/bin/svnserve svnserve
    • ln -s /opt/subversion/bin/svnsync svnsync
    • ln -s /opt/subversion/bin/svnversion svnversion
  7. That should be it

The beauty is that you only need to do this once. The next time you upgrade your Subversion client, the symlinks will just point to the updated files and everything should be fine. As I said earlier though: only do this if you feel comfortable on the command line.

UPDATE: I have been told that some people seem to have SVN installed in /usr/bin/. Just use the path that is returned by the which svn command. You might also need to use sudo to run some of the command (i.e. move).

jQuery List Paginator Plugin - Elpaginator

I know it’s been a while. I started about 5 posts, but haven’t finished any of them… 

Hopefully this makes up for it: as a treat for the new year, I thought a little jQuery plugin that I wrote would be handy for some people: please welcome Elpaginator!

What it does:

It looks for a ul or ol within a wrapper & adds pagination depending on the maximum number of items specified.

Features:

  • paginates if there are more items than the user specified
  • displays number of items left or pages, i.e. 1-10, 11-20, etc or 1, 2, etc
  • html of pagination is fully configurable (wrapper need to contain class of page-list!)
  • the number of the list item if ordered list is used is correct (uses start attribute that is deprecated, but there is no real alternative yet as IE6 & 7 don’t support CSS for this yet)
  • support for ul & ol
  • very lightweight (less than 200 lines WITH lots of comments)  

You can find the elpaginator script & a demo page in my svn repository.

Options

This is how you initialise the pagination:

1
$('#wrapper').elpaginator();

Possible Options are:

1
2
3
4
5
6
7
$('#wrapper').elpaginator({
  MAX: 10,  // maximum number of items per pagination
  list: 'ol',   // whether it's an ul or ol. Default: ul
  style: 'items', // other value is pages -> show 1-5 6-10, etc, or 1, 2,
  paginationHTML: '<div class="pagination"><ul class="page-list"></ul></div>', // HTML for the pagination. Is appended to the wrapper
  paginationListItemHTML: '<li><a href="#"></a></li>' // HTML for the pagination items
});

A live example of it working can also be seen on the new Top Gear site (look at the sidebar for the lap times table).

Splitting a Long JavaScript String Across Multiple Lines

Whilst reading a comment by Mike DeBoer on Ajaxian about delimiting JavaScript strings with backslashes, I thought it’s worth mentioning it here as well, if only to remind myself.

String concatenation always creates a new string.

Thus, writing:

1
2
3
var str = "This is a long" +
  " string. That's why we want to " +
  " split it over several lines";

is slower & more memory intensive than writing:

1
2
3
var str = "This is a long\
 string. That's why we want to\
 split it over several lines";

Just be careful you don’t have trailing spaces as that will mess it up.

JavaScript Cookies Made Easy

Here is another little script that makes my life a bit easier. It consists of 3 methods that makes working with cookies very straight forward. This script is built on the shoulder of giants: PPK from Quircksmode was the man who did the hard work.

However, rather than creating 3 methods that sit in the global namespace, I wanted to use a singleton object that would only add 1 global variable. Since there is no need for private methods, the object literal pattern is appropriate. In my opinion the names of the methods make it very straight forward to use. Good names mean less looking up, mean less mistakes, mean faster development. Your Technical Project Manager will love you for it :-)

1
2
3
4
5
6
7
8
// To create/set a cookie:
cookie.set('name', 'value, 234); // last argument is number of days until expiry. If none is provided, it will get deleted when the browser is closed

// To get the cookie value:
cookie.get('name');

// To delete the cookie:
cookie.erase('name');

As always, you can download the latest version from my svn repository