Tagged as programming

30 Jul 2011

Toggling relative line-numbering in Vim

With the recent release of Lion, OS X users get access to the latest version of Vim. That includes the new relative line-numbering feature which looks particularly useful given Vim's many line-wise operations (see :help relativenumber for details).

However, it might not be something you want on all the time. Absolute line-numbering is useful in debugging and for providing context, and relative line-numbering could be slightly distracting given that it changes as you move around. So, you might want to toggle between the two modes.

Toggling between absolute and relative isn't as simple as :set relativenumber! though, because norelativenumber just turns off line-numbering rather than falling back to number.

So here's one way of handling things. First set your default numbering:

if exists('+relativenumber')
    set relativenumber
else
    set number
endif

Add a function to toggle between the two modes:

function! ToggleNumbering()
    if exists("+relativenumber")
        if &relativenumber
            set number
        else
            set relativenumber
        endif
    else
        set number!
    endif
endfunc

If the option isn't available, e.g. you're on a version earlier than 7.3, it falls back to toggling line-numbering on or off. If you were so inclined, you could do a version that cycled between the 3 possible states (off, absolute and relative), but I prefer it like this.

Finally, map a keyboard shortcut to trigger the function:

noremap <leader>n :call ToggleNumbering()<cr>

I use <leader> mappings a lot for my own personal shortcuts, but a lot of people seem to prefer the function keys. You can replace <leader>n with <f12> or whatever if that includes you.

Paste that lot into your .vimrc and continue on your merry way.

0 comments

22 Feb 2011
3 Nov 2010

...why does Python have an adjective, when other languages do not? We don't hear about code being Javanese, or Pearly, or Rubinesque. Why don't we speak of code that is C-plush-plush, or PHPleasing?Ned Batchelder

0 comments

25 Apr 2010

Ode to a Bygone Era

In the pub on friday the subject of geek pilgrimages cropped up. Trips to MIT, One Infinite Loop, the Googleplex, that sort of thing. I've yet to partake myself, but I can definitely see the appeal.

In a similar vein, I mentioned Code Rush, only to be met by blank looks all round.

A documentary from the close of the nineties, about crunch time at Netscape in the run-up to the release of Mozilla. If you haven't seen it and those words evoke anything for you, then you should. Jamie Zawinski, Brendan Eich, history, pathos, what more do you need?

(Credit due to Andy Baio who was fairly instrumental a year or two ago in re-kindling interest in the film and inspiring the film-makers to re-release it under a Creative Commons license.)

0 comments

23 Mar 2010
16 Jan 2010
25 Nov 2009
17 Nov 2009
21 Oct 2009
20 Aug 2009
22 Jul 2009
24 Jun 2009
23 Mar 2009
12 Mar 2009
12 Feb 2009
6 Dec 2008
16 Oct 2008
11 Oct 2008
9 Oct 2008
7 Oct 2008
26 Sep 2008
24 Sep 2008
26 Aug 2008
8 Jul 2008

A minor morsel

I haven't posted in ages, so I might as well use this to break radio silence. It's a solution to this little programming brain teaser from Dustin Diaz. I don't think it's as much of a puzzle as he seemed think, given by the responses in the comments and how long it took me, but regardless:

var listA = ['a', 'b', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e', 'f', 'e', 'f', 'e', 'f', 'a', 'a', 'a', 'f', 'f', 'f'];
var listB = [];

var count = 0;

listA.forEach(function(val,key,arr){
    var matchPrev = (val == arr[key - 1]);
    var matchNext = (val == arr[key + 1]);

    count = matchPrev ? count+1 : 0;

    if (count == 2) {
        val = '<span>' + val;
    }
    if (count >= 2 && !matchNext) {
        val += '</span>';
    }

    listB.push(val);
});

console.log(listB.join(' '));

As some of the commenters' solutions show, it can be done in a one-line regular expression, but I was sticking to the "rules" implicit in the original post.

0 comments

5 Apr 2008
19 Mar 2008
17 Jan 2008
13 Jan 2008
9 Jan 2008
6 Jan 2008
29 Dec 2007
28 Aug 2007
26 Jun 2007
15 Jun 2007
14 Jun 2007
12 Jun 2007
24 Jan 2007
15 Jan 2007
12 Dec 2006
8 Dec 2006
6 Dec 2006
3 Dec 2006
23 Nov 2006
22 Nov 2006
12 Nov 2006
9 Nov 2006
28 Sep 2006
5 Sep 2006
8 Mar 2006
15 Nov 2005
8 Sep 2005
29 Aug 2005
25 Aug 2005
22 Aug 2005

Where am I?

This is the personal site of Matthew Tarbit, a seasoned web developer holed-up in a hideaway somewhere in the depths of Leeds, England.

Should the mood arise, you might add my outpourings to the deluge that is your already overflowing info drip feed.

Alternately, rest a while and dig through my entries like a corpulent pig in search of all that is truffle-icious.