views:

450

answers:

5

i'm wondering if emacs has these cursor movement commands built in or if i'm going to have to write them or find a snippet somewhere. i find them pretty spiffy and use them in vim regularly. i haven't found them in emacs' documentation so far.

in vim, they look like this: shift-h -> move cursor to the top of the screen shift-m -> move cursor to the middle of the screen shift-l -> move cursor to the bottom of the screen

just to clarify, i'm not looking to move the cursor to the top/bottom of the document, just to the top/bottom of the currently visible part of the document, i.e. the part that's currently being displayed on screen.

i found one of them so far. alt-r seems to be the equivalent of vim's shift-m. it moves the cursor to the first column of the middle line.

+10  A: 

Use:

  • Alt+0 Alt+r - top of Window
  • Alt+- Alt+r - bottom of Window

Strictly, these should be written as M-0 M-r for the top of the screen and M-- M-r for the bottom of the screen. Where M means the Meta key which is usually mapped to Alt.

I worked out these keystrokes as follows:

M-r runs the command move-to-window-line. I found this out with C-h k M-r, ie. Ctrl+h, k, Alt+r. The key sequence C-h k means tell me what the next key sequence does. It told me the command name and also that you can pass numeric arguments to the command to pick the line you want to go to. If you don't pass any it moves the point to the middle of the window as you have seen.

You pass numeric arguments to commands by typing a number while holding down Meta. A minus sign on its own is taken to mean -1. Now, to move to the top of the screen we want to pass line 0 and for the bottom of the screen line -1. This gives us the key sequences above.

If you want to bind move-to-window-line to a different key look at Joe's answer to this question.

Dave Webb
+2  A: 

The function you wish to use is move-to-window-line, whose definition is:

move-to-window-line is an interactive built-in function in `C source
code'.

It is bound to M-r.
(move-to-window-line arg)

Position point relative to window.
With no argument, position point at center of window.
An argument specifies vertical position within the window;
zero means top of window, negative means relative to bottom of window.

You would call it with a 0 to go to the top of the page and a -1 to go to the bottom of the page. These can be bound to a key with an anonymous function or a named function. Examples of both are given.

Anonymous Functions

(global-set-key [(f4)] (function
      (lambda ()
        "Go to top of page."
        (interactive)
        (move-to-window-line 0))))

(global-set-key [(f4)] (function
      (lambda ()
        "Go to bottom of page."
        (interactive)
        (move-to-window-line -1))))

Named Functions

(defun my-top-of-page ()
  "Go to top of page."
  (interactive)
  (move-to-window-line 0))

(defun my-bottom-of-page ()
  "Go to bottom of page."
  (interactive)
  (move-to-window-line -1))

(global-set-key [(f4)] 'my-top-of-page)
(global-set-key [(shift f4)] 'my-bottom-of-page)
Joe Casadonte
A: 

If you are using Emacs 23, it's simply C-l. The first time it will go to the center, the second time it will go to the top, the third time it will go to the bottom.

EDIT:

Oops, my bad, that puts the current line in the center/top/bottom of the window. Still useful though :)

scottfrazer
+1  A: 

to add to Joe and Dave's answers, you can get middle with:

(defun bp-goto-center() "move cursor to middle line" (interactive) (move-to-window-line (/ (window-height) 2)))

(I add "bp" to the front of all my functions to distinguish them from built-ins, or other people's... feel free to remove that.)

Brian Postow
+1  A: 

In Emacs 23.2, M-r does exactly what you want.

The first invocation of this command moves the point to center of the currently visible window, the next successive invocations move to top and bottom.

No additional configuration or custom functions needed.

Anupam