How to set a single key binding for a sequence of commands in Emacs
Asked Answered
E

3

5

The Emacs manual only has an example to set a binding for a single command. How to do so for a sequence of commands. Specifically the following.

  • [M-down] to [C-u 1 C-v]
  • [M-up] to [C-u 1 M-v]

Which is practically single line scrolling.

Euphemize answered 29/4, 2015 at 9:40 Comment(0)
E
0

A solution for defining key bindings for a sequence of command is provided here below. Here's the summary:

  • Create keyboard macros with names assigned

    • scroll-up-step: C-u 1 prior
    • scroll-up-step: C-u 1 next
  • Bind the macros to key combinations

    • M- <- scroll-up-step
    • M- <- scroll-down-step

You can include this code to your init.el file

(fset 'scroll-up-step
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([21 49 prior] 0 "%d")) arg)))
(fset 'scroll-down-step
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([21 49 next] 0 "%d")) arg)))
(global-set-key (kbd "M-<up>") 'scroll-up-step)
(global-set-key (kbd "M-<down>") 'scroll-down-step)
(global-set-key (kbd "\e <up>") 'scroll-up-step)
(global-set-key (kbd "\e <down>") 'scroll-down-step)

Detailed Steps:

Start a Macro definition

<F3> Start KBD macro definition

C-u 1 prior The sequece of commands you wish to bind

<F4> End KBD macro definition

Give the recent macro a custom name (no conflicts!)

C-x C-k n scroll-up-step

Do the same for the other custom command(s)

<F3>
C-u 1 next
<F4>
C-x C-k n scroll-down-step

Note:

prior = PgUp / fn+up (Macbook)

next = PgDown / fn+down (Macbook)

Open the init.el in Emacs

C-x C-f </path/to/init.el> (usually "~/.emacs.d/init.el")

Append the macro lisp definition to the init.el file

M-x insert-kbd-macro RET scroll-up-step RET
M-x insert-kbd-macro RET scroll-down-step RET

Append the key binding for the custom commands to the init.el file

(global-set-key (kbd "M-<up>") 'scroll-up-step)
(global-set-key (kbd "M-<down>") 'scroll-down-step)
(global-set-key (kbd "\e <up>") 'scroll-up-step)
(global-set-key (kbd "\e <down>") 'scroll-down-step)

You would notice that I have included the key bindings definition twice for a single binding. This is because the binding is related to the Meta key. The meta key is described as M in the GUI mode, but the terminal still considers it as ESC (even if the alt/option key is pressed). Thus I defined it twice so that it would work in both GUI and terminal modes of Emacs.

This might not be the fastest method for defining key bindings for a sequence of commands. But it certainly works. I wonder if anyone has a more simpler solution (if any).

Euphemize answered 30/4, 2015 at 15:31 Comment(1)
(global-set-key (kbd "M-<up>") (kbd "C-u 1 <prior>")) would be simpler -- but not necessarily better.Skitter
C
13

You can use keyboard macros for that task. The link pretty much explains how to do it, for completeness here's what to do:

  1. Hit C-x ( to start recording a keyboard macro.
  2. Do what the command is supposed to do (i.e. execute commands, hit keys, ...)
  3. Hit C-x ) to stop recording the keyboard macro.
  4. Execute M-x name-last-kbd-macro to name the last-defined keyboard macro (i.e. the one you just defined).
  5. Execute M-x insert-kbd-macro to insert the code of the last defined macro at point, copy it into your init file.
  6. Put (global-set-key (kbd "M-n") 'my-macro) into your init file (assuming you named the macro my-macro).
  7. Profit!
Chalmers answered 29/4, 2015 at 10:9 Comment(0)
D
3
  1. Find out what command (function) C-v is bound to: C-h k C-v. Answer:
    C-v runs the command scroll-up-command, which is an interactive
    compiled Lisp function in `window.el'.

    It is bound to C-v, <next>.

    (scroll-up-command &optional ARG)

    Scroll text of selected window upward ARG lines

What does C-u 1 do here? Tells C-v to scroll upward 1 line.

So you want to call scroll-up-command, passing it an ARG of 1: (scroll-up-command 1).

  1. How do you define your own command that does that?

    (defun my-scroll-up-1-line ()
      "Scroll up one line."
      (interactive)
      (scroll-up-command 1))
    

    See the Elisp manual, node Defining Commands.

  2. And bind it to M-n?

    (global-set-key (kbd "M-n") 'my-scroll-up-1-line)
    

    See the Emacs manual, node Rebinding.

Druci answered 29/4, 2015 at 14:8 Comment(0)
E
0

A solution for defining key bindings for a sequence of command is provided here below. Here's the summary:

  • Create keyboard macros with names assigned

    • scroll-up-step: C-u 1 prior
    • scroll-up-step: C-u 1 next
  • Bind the macros to key combinations

    • M- <- scroll-up-step
    • M- <- scroll-down-step

You can include this code to your init.el file

(fset 'scroll-up-step
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([21 49 prior] 0 "%d")) arg)))
(fset 'scroll-down-step
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([21 49 next] 0 "%d")) arg)))
(global-set-key (kbd "M-<up>") 'scroll-up-step)
(global-set-key (kbd "M-<down>") 'scroll-down-step)
(global-set-key (kbd "\e <up>") 'scroll-up-step)
(global-set-key (kbd "\e <down>") 'scroll-down-step)

Detailed Steps:

Start a Macro definition

<F3> Start KBD macro definition

C-u 1 prior The sequece of commands you wish to bind

<F4> End KBD macro definition

Give the recent macro a custom name (no conflicts!)

C-x C-k n scroll-up-step

Do the same for the other custom command(s)

<F3>
C-u 1 next
<F4>
C-x C-k n scroll-down-step

Note:

prior = PgUp / fn+up (Macbook)

next = PgDown / fn+down (Macbook)

Open the init.el in Emacs

C-x C-f </path/to/init.el> (usually "~/.emacs.d/init.el")

Append the macro lisp definition to the init.el file

M-x insert-kbd-macro RET scroll-up-step RET
M-x insert-kbd-macro RET scroll-down-step RET

Append the key binding for the custom commands to the init.el file

(global-set-key (kbd "M-<up>") 'scroll-up-step)
(global-set-key (kbd "M-<down>") 'scroll-down-step)
(global-set-key (kbd "\e <up>") 'scroll-up-step)
(global-set-key (kbd "\e <down>") 'scroll-down-step)

You would notice that I have included the key bindings definition twice for a single binding. This is because the binding is related to the Meta key. The meta key is described as M in the GUI mode, but the terminal still considers it as ESC (even if the alt/option key is pressed). Thus I defined it twice so that it would work in both GUI and terminal modes of Emacs.

This might not be the fastest method for defining key bindings for a sequence of commands. But it certainly works. I wonder if anyone has a more simpler solution (if any).

Euphemize answered 30/4, 2015 at 15:31 Comment(1)
(global-set-key (kbd "M-<up>") (kbd "C-u 1 <prior>")) would be simpler -- but not necessarily better.Skitter

© 2022 - 2024 — McMap. All rights reserved.