15
votes

How do I move to the beginning of the file in emacs (i.e. the effect of Ctrl+Home in windowed text editors)?

Mashing pageup does not move the cursor to the beginning (nor does Ctrl+Home, ofc).

Here the shortcut for the desired result is described as:

M-< : Move to the top of the buffer (beginning-of-buffer). With numeric argument n, move to n/10 of the way from the top.

M-> : Move to the end of the buffer (end-of-buffer).

However Meta + < yields "No M-x tags-search or M-x tags-query-replace in progress" message.

I am typing the shortcut as Alt + Shift + , since to get the "<" I have to type "Shift + ,"

Am I doing something wrong?

Thank you.

Edit: Turns out this is an issue only when running emacs through screen, where the keyboard shortcuts are, for some reason, misinterpreted. For example, C-Home gives this error message:

M-[ 1 ; 5 h (translated from M-[ 1 ; 5 H) is undefined

Any way around it?

3
I mention the relevant shortcut in the quotation, which is the standard one as I can see (e.g. same one in this emacs cheatsheet). It does not work for me though, as I explain in my post.npit
Check that key sequence is bound to beginning-of-buffer with C-h k followed by M-<. You are entering it correctlyRorschach
Are you using Emacs inside a terminal, or are you using the GUI version of Emacs. In case it makes a difference, you might as well mention also the operating system and version of Emacs that you are presently using. M-x emacs-versionlawlist
Ok, I possibly feel like an idiot, because following @jenesaisquoi 's instructions I see that indeed it's mapped to the standard shortcuts, but Ctrl+Home does work now - no idea why it did not before. I do not know how to type M-< : Alt+Shift+, successfully. It results in M-, when I try. I am only using emacs in a terminal.npit
I am undeleting and updating the question.npit

3 Answers

11
votes

This works on newer Emacs

Esc followed by < #beginning of file
Esc followed by > #end of file

Works great in combination with ssh and Tmux

2
votes

I cannot reproduce the exact behavior as C-HOME that you experience. For me it translates to M-[ 1 ;, without the 5H (but that is actually inserted...).

But, given that, here's how I would set up the binding.

I'd go into the *scratch* buffer and type

(read-key-sequence "please type C-home ") C-j

Which will prompt you for a key sequence, so do C-HOME and Emacs should insert the following right after the read-key-sequence line:

    "^[[1;"
    5H
5H

Using the string, I'd set up the binding in my .emacs like so:

(global-set-key "^[[1;" 'beginning-of-buffer)

This will define the key binding appropriately, except that (for me) it now inserts 5H at the top of the buffer. I believe the 5H is a product of screen somehow...


Updated to add:

The 5H annoys me, but as far as I can tell, Emacs thinks we are literally typing it. So, I coded up two alternatives which result in the same behavior.

The first uses keyboard-quit to interrupt the key sequence after getting to the beginning of the buffer. This prevents the 5H from being inserted. But it has the downside of doing a keyboard-quit - which will flash/ding at you ever time. Kind of annoying.

(global-set-key "^[[1;" 'my-bob)

(defun my-bob ()
  "Go to beginning of buffer, then quit"
  (interactive)
  (beginning-of-buffer)
  (keyboard-quit))

To avoid the keyboard-quit, I wrote a different version which runs a little snippet of code which deletes the 5H if it exists. It's a little more involved, but does the job.

(global-set-key "^[[1;" 'my-bob)

(defun my-bob ()
  "Go to beginning of buffer, then delete any 5H inserted by the binding"
  (interactive)
  (beginning-of-buffer)
  (run-with-idle-timer 0.1 nil 'delete-inserted-chars "5H"))

(defun delete-inserted-chars (chars)
  (save-excursion
    (backward-char (length chars))
    (if (looking-at (regexp-quote chars))
        (delete-char (length chars)))))

The delete-inserted-chars can be reused for other bindings in screen which happen to insert characters as well.

-1
votes

One thing you could do it go to line one:

C-u 1 M-g M-g