Archives: 'tech'

Week 3: Vim tricks - Indenting Code

Wednesday, August 6th, 2008

For a long time now I have known how to indent using Vim’s ‘>’ command to indent a block of text. I have learnt a few new tricks that might speed me up a bit. (more…)

Week 2: Vim tricks - Automatic code completion

Tuesday, July 29th, 2008

You may have used an IDE before that allows you to automatically complete parts of your code. In Microsoft’s Visual Studio this is called “intellisense”. It’s very handy for speeding up development and for exploring objects that you can’t quite remember the attributes of. This week I found out how to add it to Vim. (more…)

New Vim tricks

Monday, 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

Friday, 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.

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

Thursday, 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.

Last Exit: Ubuntu Package

Thursday, May 11th, 2006

I have packaged last exit with my modification as described in yesterday’s post.

You can just download the package and install it by typing

sudo dpkg -i last-exit_0.2-3_i386.deb or you can add my repository to your sources list and then you’ll automatically get updates when new versions are packaged.

If you want to add my repository open /etc/apt/sources.list in a text editor and add

deb http://www.newspeak.org.uk/repository/ binary/

(note: there is meant to be a space between “…repository/” and “binary/”)

Then type

sudo apt-get update
sudo apt-get install last-exit
last-exit

There are a number of bugs with last exit currently, such as the program segfaulting when you click on the information icon and when you close the program. If you have problems other than those feel free to leave a comment here but please bear in mind that I am not the original software author I have merely packaged it and made a minor alteration.

Last Exit: Gtk based Last.fm player

Wednesday, May 10th, 2006

Lastexit personal radio screenshot

More screenshots

Last Exit is a Gtk based player for Last.fm. If, like me, you use GNOME Desktop then you might prefer the way this player looks compared to the Qt based official Last.fm player. It is harder to install than the official player and as it’s still quite new there are still a few bugs that need to be ironed out. I have written some basic instructions for installing Last Exit on Ubuntu Linux. (more…)