How to run whichever function is bound to a certain key
There are a lot of occasions where, by default, the key-binding C-c C-c
does something useful - e.g. in org-mode or magit. I would like to take whatever function C-c C-c
runs, and bind that function to something like F8
, without having to know what specific function is being called under the hood.
Thus: how can I take an existing key-binding, obtain the function which that key-binding would run, and then simply run that function?
(I'm aware it would take me 5 min to just make a list of functions that I'm interested in, and bind them appropriately in the respective modes - but I would nevertheless like to know how to achieve the above)
key-bindings
add a comment |
There are a lot of occasions where, by default, the key-binding C-c C-c
does something useful - e.g. in org-mode or magit. I would like to take whatever function C-c C-c
runs, and bind that function to something like F8
, without having to know what specific function is being called under the hood.
Thus: how can I take an existing key-binding, obtain the function which that key-binding would run, and then simply run that function?
(I'm aware it would take me 5 min to just make a list of functions that I'm interested in, and bind them appropriately in the respective modes - but I would nevertheless like to know how to achieve the above)
key-bindings
Note thatC-c C-c
in org-mode runs a generic command (org-ctrl-c-ctrl-c
) that then dispatches to different functions, depending on context. The solutions below will get you to the top-level command dispatcher, but if you want to get to the lower level dispatched function, you will have to read theorg-ctrl-c-ctrl-c
code.
– NickD
13 hours ago
add a comment |
There are a lot of occasions where, by default, the key-binding C-c C-c
does something useful - e.g. in org-mode or magit. I would like to take whatever function C-c C-c
runs, and bind that function to something like F8
, without having to know what specific function is being called under the hood.
Thus: how can I take an existing key-binding, obtain the function which that key-binding would run, and then simply run that function?
(I'm aware it would take me 5 min to just make a list of functions that I'm interested in, and bind them appropriately in the respective modes - but I would nevertheless like to know how to achieve the above)
key-bindings
There are a lot of occasions where, by default, the key-binding C-c C-c
does something useful - e.g. in org-mode or magit. I would like to take whatever function C-c C-c
runs, and bind that function to something like F8
, without having to know what specific function is being called under the hood.
Thus: how can I take an existing key-binding, obtain the function which that key-binding would run, and then simply run that function?
(I'm aware it would take me 5 min to just make a list of functions that I'm interested in, and bind them appropriately in the respective modes - but I would nevertheless like to know how to achieve the above)
key-bindings
key-bindings
edited 10 hours ago
Drew
48.6k463107
48.6k463107
asked 16 hours ago
funklutefunklute
183
183
Note thatC-c C-c
in org-mode runs a generic command (org-ctrl-c-ctrl-c
) that then dispatches to different functions, depending on context. The solutions below will get you to the top-level command dispatcher, but if you want to get to the lower level dispatched function, you will have to read theorg-ctrl-c-ctrl-c
code.
– NickD
13 hours ago
add a comment |
Note thatC-c C-c
in org-mode runs a generic command (org-ctrl-c-ctrl-c
) that then dispatches to different functions, depending on context. The solutions below will get you to the top-level command dispatcher, but if you want to get to the lower level dispatched function, you will have to read theorg-ctrl-c-ctrl-c
code.
– NickD
13 hours ago
Note that
C-c C-c
in org-mode runs a generic command (org-ctrl-c-ctrl-c
) that then dispatches to different functions, depending on context. The solutions below will get you to the top-level command dispatcher, but if you want to get to the lower level dispatched function, you will have to read the org-ctrl-c-ctrl-c
code.– NickD
13 hours ago
Note that
C-c C-c
in org-mode runs a generic command (org-ctrl-c-ctrl-c
) that then dispatches to different functions, depending on context. The solutions below will get you to the top-level command dispatcher, but if you want to get to the lower level dispatched function, you will have to read the org-ctrl-c-ctrl-c
code.– NickD
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
Trivially, with a keyboard macro:
(global-set-key (kbd "<f8>") (kbd "C-c C-c"))
n.b. That's not binding to a function; it's making <f8> issue the key sequence C-cC-c, which will in turn call whatever command it is bound to.
However as the C-cC-c sequence is reserved for major modes (refer to C-hig (elisp)Key Binding Conventions
), you can trust that the local keymap will contain the binding of interest, and so you could use that to bind <f8> to the same command.
Here's an example I use to make RET do whatever M-j is bound to in any programming major mode.
(defun my-coding-config ()
"Common behaviours for programming."
(local-set-key (kbd "RET") (key-binding (kbd "M-j"))))
(add-hook 'prog-mode-hook 'my-coding-config)
add a comment |
To find which command C-c C-c
runs, you can use key-binding
, e.g.,
(key-binding (kbd "C-x C-f"))
;; => find-file
To run an interactive command from Lisp, you can use call-interactively
, e.g.,
(call-interactively #'find-file)
(funcall
won't work sometimes since it doesn't take care of the interactive spec.)
So maybe the following does what you want
(defun your-ctrl-c-ctrl-c ()
(interactive)
(let ((command (key-binding (kbd "C-c C-c"))))
(when command
(call-interactively command))))
(global-set-key (kbd "<f8>") #'your-ctrl-c-ctrl-c)
Nitpick:call-interactively
only works on commands which are also functions. So in general you should rather useexecute-command
which should work for any command, whether it's a function or a key-sequence.
– Stefan
7 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "583"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f48238%2fhow-to-run-whichever-function-is-bound-to-a-certain-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Trivially, with a keyboard macro:
(global-set-key (kbd "<f8>") (kbd "C-c C-c"))
n.b. That's not binding to a function; it's making <f8> issue the key sequence C-cC-c, which will in turn call whatever command it is bound to.
However as the C-cC-c sequence is reserved for major modes (refer to C-hig (elisp)Key Binding Conventions
), you can trust that the local keymap will contain the binding of interest, and so you could use that to bind <f8> to the same command.
Here's an example I use to make RET do whatever M-j is bound to in any programming major mode.
(defun my-coding-config ()
"Common behaviours for programming."
(local-set-key (kbd "RET") (key-binding (kbd "M-j"))))
(add-hook 'prog-mode-hook 'my-coding-config)
add a comment |
Trivially, with a keyboard macro:
(global-set-key (kbd "<f8>") (kbd "C-c C-c"))
n.b. That's not binding to a function; it's making <f8> issue the key sequence C-cC-c, which will in turn call whatever command it is bound to.
However as the C-cC-c sequence is reserved for major modes (refer to C-hig (elisp)Key Binding Conventions
), you can trust that the local keymap will contain the binding of interest, and so you could use that to bind <f8> to the same command.
Here's an example I use to make RET do whatever M-j is bound to in any programming major mode.
(defun my-coding-config ()
"Common behaviours for programming."
(local-set-key (kbd "RET") (key-binding (kbd "M-j"))))
(add-hook 'prog-mode-hook 'my-coding-config)
add a comment |
Trivially, with a keyboard macro:
(global-set-key (kbd "<f8>") (kbd "C-c C-c"))
n.b. That's not binding to a function; it's making <f8> issue the key sequence C-cC-c, which will in turn call whatever command it is bound to.
However as the C-cC-c sequence is reserved for major modes (refer to C-hig (elisp)Key Binding Conventions
), you can trust that the local keymap will contain the binding of interest, and so you could use that to bind <f8> to the same command.
Here's an example I use to make RET do whatever M-j is bound to in any programming major mode.
(defun my-coding-config ()
"Common behaviours for programming."
(local-set-key (kbd "RET") (key-binding (kbd "M-j"))))
(add-hook 'prog-mode-hook 'my-coding-config)
Trivially, with a keyboard macro:
(global-set-key (kbd "<f8>") (kbd "C-c C-c"))
n.b. That's not binding to a function; it's making <f8> issue the key sequence C-cC-c, which will in turn call whatever command it is bound to.
However as the C-cC-c sequence is reserved for major modes (refer to C-hig (elisp)Key Binding Conventions
), you can trust that the local keymap will contain the binding of interest, and so you could use that to bind <f8> to the same command.
Here's an example I use to make RET do whatever M-j is bound to in any programming major mode.
(defun my-coding-config ()
"Common behaviours for programming."
(local-set-key (kbd "RET") (key-binding (kbd "M-j"))))
(add-hook 'prog-mode-hook 'my-coding-config)
answered 15 hours ago
philsphils
27.5k23769
27.5k23769
add a comment |
add a comment |
To find which command C-c C-c
runs, you can use key-binding
, e.g.,
(key-binding (kbd "C-x C-f"))
;; => find-file
To run an interactive command from Lisp, you can use call-interactively
, e.g.,
(call-interactively #'find-file)
(funcall
won't work sometimes since it doesn't take care of the interactive spec.)
So maybe the following does what you want
(defun your-ctrl-c-ctrl-c ()
(interactive)
(let ((command (key-binding (kbd "C-c C-c"))))
(when command
(call-interactively command))))
(global-set-key (kbd "<f8>") #'your-ctrl-c-ctrl-c)
Nitpick:call-interactively
only works on commands which are also functions. So in general you should rather useexecute-command
which should work for any command, whether it's a function or a key-sequence.
– Stefan
7 hours ago
add a comment |
To find which command C-c C-c
runs, you can use key-binding
, e.g.,
(key-binding (kbd "C-x C-f"))
;; => find-file
To run an interactive command from Lisp, you can use call-interactively
, e.g.,
(call-interactively #'find-file)
(funcall
won't work sometimes since it doesn't take care of the interactive spec.)
So maybe the following does what you want
(defun your-ctrl-c-ctrl-c ()
(interactive)
(let ((command (key-binding (kbd "C-c C-c"))))
(when command
(call-interactively command))))
(global-set-key (kbd "<f8>") #'your-ctrl-c-ctrl-c)
Nitpick:call-interactively
only works on commands which are also functions. So in general you should rather useexecute-command
which should work for any command, whether it's a function or a key-sequence.
– Stefan
7 hours ago
add a comment |
To find which command C-c C-c
runs, you can use key-binding
, e.g.,
(key-binding (kbd "C-x C-f"))
;; => find-file
To run an interactive command from Lisp, you can use call-interactively
, e.g.,
(call-interactively #'find-file)
(funcall
won't work sometimes since it doesn't take care of the interactive spec.)
So maybe the following does what you want
(defun your-ctrl-c-ctrl-c ()
(interactive)
(let ((command (key-binding (kbd "C-c C-c"))))
(when command
(call-interactively command))))
(global-set-key (kbd "<f8>") #'your-ctrl-c-ctrl-c)
To find which command C-c C-c
runs, you can use key-binding
, e.g.,
(key-binding (kbd "C-x C-f"))
;; => find-file
To run an interactive command from Lisp, you can use call-interactively
, e.g.,
(call-interactively #'find-file)
(funcall
won't work sometimes since it doesn't take care of the interactive spec.)
So maybe the following does what you want
(defun your-ctrl-c-ctrl-c ()
(interactive)
(let ((command (key-binding (kbd "C-c C-c"))))
(when command
(call-interactively command))))
(global-set-key (kbd "<f8>") #'your-ctrl-c-ctrl-c)
answered 15 hours ago
xuchunyangxuchunyang
8,7941926
8,7941926
Nitpick:call-interactively
only works on commands which are also functions. So in general you should rather useexecute-command
which should work for any command, whether it's a function or a key-sequence.
– Stefan
7 hours ago
add a comment |
Nitpick:call-interactively
only works on commands which are also functions. So in general you should rather useexecute-command
which should work for any command, whether it's a function or a key-sequence.
– Stefan
7 hours ago
Nitpick:
call-interactively
only works on commands which are also functions. So in general you should rather use execute-command
which should work for any command, whether it's a function or a key-sequence.– Stefan
7 hours ago
Nitpick:
call-interactively
only works on commands which are also functions. So in general you should rather use execute-command
which should work for any command, whether it's a function or a key-sequence.– Stefan
7 hours ago
add a comment |
Thanks for contributing an answer to Emacs Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f48238%2fhow-to-run-whichever-function-is-bound-to-a-certain-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Note that
C-c C-c
in org-mode runs a generic command (org-ctrl-c-ctrl-c
) that then dispatches to different functions, depending on context. The solutions below will get you to the top-level command dispatcher, but if you want to get to the lower level dispatched function, you will have to read theorg-ctrl-c-ctrl-c
code.– NickD
13 hours ago