Feed on
Posts
Comments

it boggles the mind that emacs – famous for having a bazillion keyboard shortcuts – doesn’t have one for quickly finding a line.

so, here’s how to make one.

go to your home directory in the terminal:

>> cd

and open up the .emacs file that lives there.

add the following line of code to it:

(global-set-key “\C-l” ‘goto-line)

restart emacs and you’ll find that whenever you hit “ctrl-l” (lowercase L), you’ll be presented with a prompt asking which line of the text you’d like to be moved to.


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

11 Responses to “add a goto-line command to emacs”

  1. on 01 Feb 2006 at 4:12 am self

    That’s what you get for running Emacs! XEmacs has had M-g for ‘goto-line’ for years.

  2. on 03 Jul 2006 at 12:58 pm Anonymous

    heh, 7 months later…

    in vi or vim, simply type [line number]G when not in insert mode.

    hey, it’s a slow day at work today :)

    -Andrew

  3. on 03 Jul 2006 at 1:27 pm Lawrence David

    you vim lovers make me sick :)

    insert mode? it’s a freakin’ text editor – why do you need extra commands just to edit text?!

  4. on 05 Oct 2006 at 1:22 am johngalt

    what about M-x goto-line?

    just hit enter and then type the line number.

  5. on 05 Oct 2006 at 1:28 am Lawrence David

    yea, but M-x goto-line gets really tired really fast.

  6. on 30 Jan 2007 at 12:58 am mike

    Try M-G G type the line number and hit enter. The cursor instantly moves to the line. Emacs is so eady…

  7. on 30 Sep 2009 at 5:49 am Alex Ati

    In fact, Lawrence, if you type M-x goto-line a couple of times, Emacs automatically suggests you M-G G!

  8. on 22 Feb 2010 at 7:16 am Pieta

    Thanks, little tips presented clearly like this are what make googling for help so useful!!

  9. on 08 Mar 2010 at 10:01 am Will

    If ever you think an emacs function doesn’t have an associated key chord the following will help. And will keep you from overriding other useful commands like you’ve shown above.

    M-x describe-function

    OR

    C-h f [ENTER]

    which would expand to:

    C-h f goto-line [ENTER]

    This will show you all sorts of information related to the function, including what key combinations are bound to it.

  10. on 12 Jul 2010 at 11:57 am meepo

    does anyone know how to open a file from command promt with emacs (or better, emacsclient) and directly go to line N? with vim you can open a file like this and the cursor will be set at the beginning of line 127.

    % vim somefile +127

    any help appreciated

  11. on 19 Oct 2010 at 6:12 am Gandalf

    @meepo:
    yes, this is a very important feature – open file at line N.

    While in vim you type

    $ vim somefile +27

    in emacs it must be[1]:

    $ emacs +27 somefile

    (in case of more files it should go BEFORE every filename you want to have opened at a specific line.

    [1] (source: man emacs)

Did I get this wrong? Let me know!

Trackback URI | Comments RSS