Manoj Rao bio photo

Manoj Rao

Your Average Common Man

Email Twitter Github

I use both vim and Emacs because I grew up using a lot of vim then decided to switch over to Emacs one day. The obvious choice was to switch to using Evil Mode. This post is not about that, at least not entirely. One of the things I missed dearly about vim was the ease with which you could use line numbers to move around and edit. I always found C-u preface very crufty. The implicit numeric argument that precedes a command is what makes you super fast with navigation in vim. However, I had come to like love Emacs to go back. So I copied took inspiration and wrote some ELisp code from the Internet and modified it to achieve the exact result I wanted. This is something that works out of the box on my Emacs - 26.1

Demo

Relative Line Numbers

Code

Throw this in your .emacs and watch the magic!

(defvar my-linum-current-line-number 0)

(setq linum-format 'my-linum-relative-line-numbers)

(defun my-linum-relative-line-numbers (line-number)
  (let ((test2 (- line-number my-linum-current-line-number)))
    (propertize
     (number-to-string (cond ((<= test2 0) (* -1 test2))
                             ((> test2 0) test2)))
     'face 'linum)))

(defadvice linum-update (around my-linum-update)
  (let ((my-linum-current-line-number (line-number-at-pos)))
    ad-do-it))
(ad-activate 'linum-update)

(global-linum-mode t)

My Podcast!

If you like topics such as this then please consider subscribing to my podcast. I talk to some of the stalwarts in tech and ask them what their favorite productivity hacks are:

Available on iTunes Podcast

Visit Void Star Podcast’s page on iTunes Podcast Portal. Please Click ‘Subscribe’, leave a comment.

Get it iTunes

Available on Google Play Music

Visit Void Star Podcast’s page on Google Play Music. Please Click ‘Subscribe’ and leave a comment.

Listen on Google Play Music
Available on Stitcher

Visit Void Star Podcast’s page on Sticher. Please Click ‘Subscribe’ and leave a comment.

Listen to Stitcher

Your app not listed here? Not an issue..

You should be able to search for ‘VoidStar Podcast’ on your favorite app. Most apps use one of the above sources for listing podcasts. This was tested on Podcast Addict (where you can also specify the search engine) and RatPoison on Android.