Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move to the beginning of the file in Emacs

Tags:

emacs

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?

like image 830
npit Avatar asked Aug 22 '17 23:08

npit


People also ask

How do you go to the beginning of a file in Emacs?

Emacs prompts you for a filename; respond by typing the filename, followed by RETURN. Emacs starts another buffer with the new file in it. To type C-x C-f, hold down CTRL, press x and then press f.

How do you get to the top of a file in Emacs?

Ctrl-O is its complement, moving forward through the jumplist. gg jumps to the top of the file, so Ctrl-I then jumps back. The equivalent to Ctrl-I in Emacs would be C-u C-space .

How do you go to the bottom of a file in Emacs?

Type C-M-v to scroll the bottom window. (If you don't have a real Meta key, type ESC C-v.) >> Type C-x o ("o" for "other") to move the cursor to the bottom window.

How do I move in Emacs?

The keybindings for movement by word in Emacs is almost the same as that of movement by character, but instead of the prefix C- it is M- . To move forward one word use M-f ; and to move backward one word use M-b . Movement by word will make up the bulk of your intra-line movement.


2 Answers

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

like image 93
Punnerud Avatar answered Sep 16 '22 12:09

Punnerud


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
This shows me the actual string for the key sequence, as well as the mysterious 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.

like image 42
Trey Jackson Avatar answered Sep 18 '22 12:09

Trey Jackson