All entries from Jul 2011

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

21 Jul 2011

@JoeTheDough A bold statement. I counter with Burts Bloody Mary, Brannigans Smoked Ham & Pickle, and Walkers Worcester Sauce.@twitter

0 comments

14 Jul 2011

@offmessage @mrben @natblundell That's what example.com's for.@twitter

0 comments

11 Jul 2011

@offmessage We got left a pigeon print some years back, complete with a perfect little bird turd at the bottom. It was practically art.@twitter

0 comments

10 Jul 2011

@winjer @bne @JoeTheDough I saw them at a christmas ATP in Camber Sands in 2005. Was worth it despite the cold.@twitter

0 comments

7 Jul 2011

@nofunnyname Is that the M&S snack pack thing? I always leave the beetroot one. Tastes like soil.@twitter

0 comments

4 Jul 2011

@mrben_ Worst. Shortcut. Ever.@twitter

0 comments

Other dates

Visit the archive if you wish to skip merrily through time in a devil may care fashion.