How to run whichever function is bound to a certain key












3















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)










share|improve this question




















  • 1





    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
    2 days ago
















3















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)










share|improve this question




















  • 1





    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
    2 days ago














3












3








3








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)










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Drew

48.6k463107




48.6k463107










asked 2 days ago









funklutefunklute

233




233








  • 1





    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
    2 days ago














  • 1





    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
    2 days ago








1




1





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
2 days 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
2 days ago










2 Answers
2






active

oldest

votes


















3














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)





share|improve this answer































    2














    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)





    share|improve this answer



















    • 1





      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
      2 days ago











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    3














    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)





    share|improve this answer




























      3














      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)





      share|improve this answer


























        3












        3








        3







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        philsphils

        27.6k23769




        27.6k23769























            2














            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)





            share|improve this answer



















            • 1





              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
              2 days ago
















            2














            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)





            share|improve this answer



















            • 1





              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
              2 days ago














            2












            2








            2







            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)





            share|improve this answer













            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)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 days ago









            xuchunyangxuchunyang

            8,8041926




            8,8041926








            • 1





              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
              2 days ago














            • 1





              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
              2 days ago








            1




            1





            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
            2 days 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
            2 days ago


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana