Archive for the ‘Open Source’ Category

Save multiple images to a PDF in Linux

Friday, December 4th, 2009

Place all the images you want in the PDF into a new directory and in the console run the following from within that directory:

convert * mynewfile.pdf

The powerful convert command uses the ImageMagick library which can be installed (in Ubuntu) with the following console command:

sudo apt-get install imagemagick

How to delete all .svn directories

Thursday, July 30th, 2009

In Linux, if you want to remove all .svn files and folders within a directory, navigate to the directory in your terminal and use:

find . -name ".svn" -type d -exec rm -rf {} \;

Credit

Programming with VIM

Tuesday, July 28th, 2009

vim

For years now I’ve used IDEs like Visual Studio and Eclipse occasionally falling back to plain text editors like GEdit or Notepad2 when making quick changes. I’ve been running Linux for almost three years now and, as you do when dealing with remote servers, have sometimes had to edit text files via the console using VIM.

VIM has been around for 18 years and is an extended version of VI which has in turn been around for 33 years. VIM = VImproved. It’s a console-based text editor designed on the assumption that you will only ever be using your keyboard (kiss your mouse goodbye) which means that much of the most common functions are based around the home keys (‘asdf’ and ‘jkl;’) and it’s packed with great programming features.

But the learning curve is about the steepest I’ve ever come across.

I decided a couple of months ago to make an effort to get to grips with it and it took a good four weeks before I was matching the programming speed I was used to with Eclipse. But now I’m finding that my speed is continuing to increase and there is no way I can go back now. It’s not the be-all-and-end-all though; if you are writing a document from scratch (like this blog entry) then VIM doesn’t really have much to offer but if you are editing an existing document (as you often are when programming) it’s streets ahead of IDEs and text editors.

One of the hardest things to get your head around is the fact that VIM is modal which means that you switch between typing stuff and doing stuff to existing text. By default you are not in ‘typing stuff’ mode and so when you type the letter w it’ll skip to the next word. If you want to add text you have to press i and then press Esc after you’ve finished to go back to ‘doing stuff’ mode. For example, to copy an entire line, paste it below, move to the new line, skip three words along, delete the remaining text on the line and start typing, in VIM you would type yy, p, 3w, c$ but the equivalent in a text editor would be to press Shift-End, Ctrl-C, End, Return, Ctrl-V, Home, Ctrl-Right, Ctrl-Right, Ctrl-Right, Shift-End, Delete and then start typing. Those key combinations may seem strange but, once you are used to them, they make a lot more sense than having to constantly move your hands away from the home keys. Especially on a laptop!

VIM is free, open source and is available for AmigaOS, Atari MiNT, BeOS, DOS, MacOS, NextStep, OS/2, OSF, RiscOS, SGI, UNIX, VMS, Windows, FreeBSD and Linux. If you are a programmer or edit plain text on a regular basis I recommend you give it a go but be aware that the curve is about as steep as curves get. If you decide to try it out I can also recommend this website to get you on your feet.

The Ultimate Device

Tuesday, June 10th, 2008

I see that Apple are deigning to release the new iPhone in New Zealand soon. I’ve had an iMate Jam for almost three years now and only use it as a phone and an MP3 player (since moving to Linux I’ve been unable to sync my calendar and emails). I’m not going to be rushing out to get the new iPhone either. Why? Because I already know what I want in a device and I just know I’m going to be deeply dissatisfied with anything for the next ten or twenty years.

Here’s my specs for the ultimate device:

  • Small enough to be worn on the wrist (perhaps twice the size of a watch but more fitted)
  • An in-built or pull-out screen that suffices and has a minimum 800×600 resolution
  • A universal docking port.
  • Open source software AND hardware
  • Phone
  • Good quality camera (~4 megapixels + optical zoom)
  • Audio and Video playback
  • Desktop-equivalent processor + 4gb ram (to replace current PC but can be hotplugged into screen, keyboard, mouse and other devices wherever I happen to be)
  • At least 160GB storage.
  • Bottle opener
  • Assorted knives, saws, scissors and picky things (air travel issues here)
  • GPS
  • Heart rate and other physiological monitors
  • IR and radio remote control and key control for car, TV and house
  • Credit card built in
  • Solar panel on the back in case I can’t get near a power point
  • Emergency beacon
  • Tape measure
  • Minesweeper and solitaire (isn’t that obligatory?)
  • Total lockdown of sensitive information

And that’s all I can think of right now. Anything less is just not going to blow my socks off.

When my iMate finally dies I’m probably going to go back to my trusty Nokia 8210 which pretty much does everything I currently need a phone to do.

Preload to make Linux faster

Wednesday, February 27th, 2008

If you’re running Linux you’ll be aware that it’s crazyfastâ„¢ already. Want even more speed? Get Preload. If you’re running Ubuntu you can get it by opening your console and typing sudo apt-get install preload.

EOL is live!

Wednesday, February 27th, 2008

I’ve just received the following email announcing that the Encyclopedia Of Life has finally gone live with the first 30,000 pages:

The new Encyclopedia of Life portal has gone live with more than one million species pages!
In celebration of this big event, our first EOL newsletter is available at:

http://www.eol.org/content/page/newsletter.

You can see the new pages at http://www.eol.org. We also invite you to take the survey at the site so you can help us improve.

Unfortunately I’ve been unable to get on it due to the sheer volume of people who must be hitting it right now.

The EOL aims to catalogue the 1.8 million known species and is an open collaboration that’s expected to take 10 years or so. Take a look at EO Wilson’s talk that started it all off:

Some Handy Regular Expressions

Saturday, February 9th, 2008

At least something
/.+/

A positive integer
/^\d*$/

A decimal number
/^\d*\.?\d/

A valid email address
/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/

A valid web address
/^(http:\/\/)?\w+([\.-]?\w+)*\w+([\.-]?\w+)*(\.\w+)+(\/)?(\w+)?$/

A hexadecimal colour (6 chars)
/^#?[0-9A-Fa-f]{6}$/

How to use in Javascript:
var testString = "ff0000";
var pattern = /^#?[0-9A-Fa-f]{6}$/;
if (testString.match(pattern)) DoSomething();

How to use in PHP:
$testString = 'ff0000';
$pattern = '/^#?[0-9A-Fa-f]{6}$/';
if (preg_match($pattern, $testString) DoSomething();

How to use in C#:
string testString = "ff0000";
string pattern = @"^#?[0-9A-Fa-f]{6}$";
Match m = Regex.Match(testString, pattern);
if (m.Success) DoSomething();

Time For a Cool Change

Friday, January 25th, 2008

Tonight I set myself a challenge to redesign the look and feel of this blog without using any images. Lots of grey, lots of white space and all typography. And if you are viewing this on Linux it’s likely you’ll be treated to an eyeful of Deja Vu Sans+Serif Condensed – the sweetest open source font IMHO. Windows and Mac users get Helvetica, Arial and Trebuchet.

How to increase the number of recent comments in WordPress

Monday, January 21st, 2008

If you have direct access to your database you can change the number of recent comments that are displayed on your homepage in WordPress. I use phpMyAdmin to manage my database. Here’s what you need to do:

  1. Go to the wp_options table in your database
  2. Browse the data in this table and find the option with the name ‘widget_recent_comments’
  3. Edit this option and change the last number (it should be ‘5′ by default) to the number of recent comments you would like to display: a:2:{s:5:"title";s:0:"";s:6:"number";i:5;}

Of course, it’s quite likely that there is a setting for this in the admin panel that I’ve overlooked – I was unable to find it.

Search and Replace text in files with Python

Thursday, November 29th, 2007

#!  /usr/bin/python2.5
import os

mydir = "/path/to/directory"
mysearch = "text to find"
myreplace = "Text to replace"

def doReplace(filePath):
    fin = open(filePath, "r")
    s = fin.read()
    fin.flush()
    fin.close()
    fout = open(filePath, "w")
    s = s.replace(mysearch, myreplace)
    fout.write(s)
    fout.close()

for root, dirs, files in os.walk(mydir):
    for f in files:
        name, ext = os.path.splitext(f)
        if ext == '.html':
            doReplace(root + '/' + f)

Explanation: This will find all files ending in .html in the directory specified in mydir along with all matching files in any subfolders and will replace the text specified in mysearch with the text in myreplace. It’s only been tested on Linux but with a bit of tweaking will run on Windows and Mac.