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.
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.
Which is practically single line scrolling.
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
Bind the macros to key combinations
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).
You can use keyboard macros for that task. The link pretty much explains how to do it, for completeness here's what to do:
C-x (
to start recording a keyboard macro.C-x )
to stop recording the keyboard macro.M-x name-last-kbd-macro
to name the last-defined keyboard macro (i.e. the one you just defined).M-x insert-kbd-macro
to insert the code of the last defined macro at point, copy it into your init file.(global-set-key (kbd "M-n") 'my-macro)
into your init file (assuming you named the macro my-macro
).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)
.
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
.
And bind it to M-n
?
(global-set-key (kbd "M-n") 'my-scroll-up-1-line)
See the Emacs manual, node Rebinding
.
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
Bind the macros to key combinations
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).
(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.
(global-set-key (kbd "M-<up>") (kbd "C-u 1 <prior>"))
would be simpler -- but not necessarily better. – Skitter