New Vim tricks

July 21st, 2008

I’ve been using vim as my main text editor for over 5 years now and because I’m now dependent on it and using it every day at work I’ve decided to challenge myself to learn a new trick every week. Last week’s trick was sorting lines.

Sorting Lines

I actually learnt two new tricks to be able to do this. The first is selecting lines using visual mode.

By placing the cursor on the first line you wish to select then pressing Shift+V and moving the cursor the last line to select you select the range of lines between the two and you can do any of your normal ex commands on just the selected text.

To sort the selected lines type :sort, this runs the sort command and replaces the selection with the result. You notice if you try to sort lines with numbers they will be sorted alphanumerically, ie. 700 would come before 8, if you want to sort numerically use :sort n. You can also remove duplicates from a list by using :sort u, to remove duplicates and sort numerically use :sort un.

Optional arguments in Python

March 7th, 2008

Optional arguments in python are obviously very useful allowing you to have default values so you don’t need to pass them every time. A simple hello world example always goes a long way…

def hello(to="world"):
    print "Hello,", to
>>> hello("Steve")
Hello, Steve
>>> hello()
Hello, world

Sometimes you’ll see people define an optional argument with a default value of None then override it in the function’s body. This might seem a strange thing to do at first, but there is a reason. Let’s say that we wanted to be able to say hello to a list of people and for it to automatically add “World” to the list.

def hello(to=[]):
    to.append("World")
    if len(to) > 1:
        greeting = "Hello, " + ", ".join(to[:-1]) + " and " + to[-1]
    else:
        greeting = "Hello, " + to[0]
    print greeting
>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World
>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World
>>> hello()
Hello, World
>>> hello()
Hello, World and World
>>> hello()
Hello, World, World and World

This happens because the value for the default argument is the same list every time, so when we modify it the function has the same modified list as the default argument when it is subsequently called. This is probably not the behaviour you would expect and you would rarely want to use this behaviour. The way around it is to define the default argument as None then override it.

def hello(to=None):
    to = to or []
    to.append("World")
    if len(to) > 1:
        greeting = "Hello, " + ", ".join(to[:-1]) + " and " + to[-1]
    else:
        greeting = "Hello, " + to[0]
    print greeting

>>> hello()
Hello, World

>>> hello()
Hello, World

>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World

>>> hello(["Steve", "Matt"])
Hello, Steve, Matt and World

Because None evaluates to false, setting the default value to None means it can be easily overridden to give us the default value we actually want if there wasn’t a passed value.

Dw i’n dysgu Cymraeg.

February 19th, 2008

S’mae!

Yesterday evening I attended my first Welsh lesson with the University of Wales, Bangor. We were given a sheet with resources online including a link for downloading a Welsh spell checker, nothing for Open Source users or Linux though. There are things out there for Welsh learners and speakers in the open source world though.

Firefox

If you’re not using firefox, you should be, I’m not going to go into why because plenty of people already have.

A Welsh dictionary for the spell checker to in Firefox can be installed from this page. There is also a setting that allows you to specify the languages that you want to read webpages in. To do this go to Edit -> Preferences and then click on advanced. You should see a section titled “Languages” with a button labelled “Choose”, click on that, select Welsh and click add.

If you are particularly brave you can have a go at setting firefox to be bilingual English/Welsh by following these instructions.

Online Dictionary

There is an open source Welsh-English dictionary developed by the Apertium Project that allows quick lookups saves a lot of time thumbing through pocket dictionaries.

Online Welsh-English Dictionary

Bilingual OpenOffice

Agored (A project from the University in Aberystwyth) have a bilingual version of OpenOffice unfortunately it seems that there is only a windows installer. The code may be Open Source though so versions for other platforms may be available in the future. I certainly hope so.

MI6 Google Ad Recruitment

December 18th, 2007

I wonder if this is targeted advertising and they actually want people like me.

Economists Say the American Dollar Still Has Sentimental Value.

November 27th, 2007

You need to a flashplayer enabled browser to view this YouTube video
You need to a flashplayer enabled browser to view this YouTube video
You need to a flashplayer enabled browser to view this YouTube video
You need to a flashplayer enabled browser to view this YouTube video

Thank you ulti for the title

OpenMoko Neo1973

November 16th, 2007

Today I got my grubby hands on an openmoko neo1973. It’s cool.

OpenMoko Screenshot

It is lacking wifi networking and there doesn’t seem to be an interface for reading SMS messages although there is a basic one for sending SMS, written in python, that you can install yourself. Also there is no Preferences interface, so you have to know a bit about linux to be able to do things like set the data and time. But the Neo1973 isn’t currently intended for use by the general public but by developers.

I’m hoping to be able to get a bluetooth networking working so I can control it and it can talk to my computer within a reasonable distance.

My steps for getting Ubuntu Feisty (7.04) the way I like it.

August 30th, 2007

Ubuntu is pretty good out of the box, but personally I need a few more things to make it feel like my computer. Before I start I install w32codecs and libdvdcss2 (on machines with a DVD drive) this allows me such wonders as DVD and MP3 playback.

Audio and Video

I remove

  • Rhythmbox,
  • Serpentine and
  • Totem

and install

  • Amarok,
  • Grip,
  • mplayer and
  • acidrip (on systems with a DVD drive)

sudo apt-get remove rhythmbox serpentine totem totem-gstreamer totem-xine totem-mozilla
sudo apt-get install grip lame amarok mplayer

Firefox

I install

  • Media Connectivity Plugin and point it to mplayer for playback and
  • Adblock Plus and subscribe it to EasyList in order to avoid having to waste my time and bandwidth on adverts.

Then I swap the Google search engine for the Google UK search engine by clicking on the search engine drop down and clicking “Manage Search Engines”. I do the same for the Ebay and Amazon search engines.

Vim

For some bizarre reason Ubuntu comes with a crippled version of Vim that it calls “vim-tiny”. I replace this with vim-full

sudo apt-get remove vim-tiny
sudo apt-get install vim-full

and put the following in my .vimrc

syntax on
set expandtab
set autoindent
set tabstop=4
set shiftwidth=4
set textwidth=78

That way vim works in a way suitable for pretty much everything I use it for as soon as I open it.

Remote Access

If I want to be able access the machine remotely I install sshd then if it’s open to the internet I use iptables to block bruteforce attacks.

If I think of anything else to add here I’ll update this post.