I wish to customize my f6key so it corresponds to a fast way of doing M-x imenu, then Pr RET. I've added the following to my ~/.emacs:
(global-set-key [f6] 'imenu)

Then I've tried to add different stuff after 'imenu, but all has failed and I've tried to google Emacs forums and google, but didn't find an obvious solution.

When M-x imenu is started it gives the user 4 possibilities:
2: Rescan
3: Procedures
4: Types
5: Modules

I always want to go into Procedures so it's enough to type Pr
followed by RET. And that explains the full key-sequence: M-x imenu, then Pr RET.

How can I proceed from the (global-set-key [f6] 'imenu) step and make the F6 key work as described above...

UPDATE: Thanks a lot so far (also for "pretty"-fying the question), however something new happened: Based on the answer from RichieHH I tried adding the following to my ~/.emacs file (although on a different system/computer, please bear over with that):

(fset 'lastMacro
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([120 31 escape 120 105 109 101 110 117] 0 "%d")) arg)))
(global-set-key [f5] 'lastMacro)

(fset 'yep
   (lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([escape 120 105 109 101 110 117 return 109 97 105 110 return] 0 "%d")) arg)))
(global-set-key [f6] 'yep)

Thing thing is that now the F5-key works - but not the F6-key. If you want to use the same "test file" as I'm using, grab the example at https://raw.githubusercontent.com/orlp/pdqsort/master/bench/bench.cpp and then M-x imenu should allow to quickly jump to different sections in that file. The problem seems to be: I'm running the X11 version of emacs (didn't try terminal-only) and if I type M-x imenu a "text-menu" pops up. If I instead use the F5-key, a GUI/X11-menu pops up, at the mouse cursor location... And that's probably why the F6-key doesn't work... I appreciate if anyone could please explain that behaviour and if I'm doing something wrong (reading the answer incorrect)?

1 Answer 1


Look up emacs keyboard macros

You will record a sequence, save it resulting in a function, then bind that function to your f6 key.

  • Please elaborate. Otherwise this is essentially a link-only answer and so risks being deleted. Please summarize the info at the link that you think answers the question.
    – Drew
    Feb 21, 2020 at 16:58
  • This is a link to official documents that explains all of the points. While I am not disagreeing, surely the refer to docs, and providing the link, is better here than cutting and pasting information that is up to date and properly maintained in the link I provided? Had I linked a random blog then yes, 100% with you. I have outlined the three steps and referenced the supporting information. I kind of felt the pointer to "using macros" is the answer the OP is looking for rather than someone writing the code for him.
    – RichieHH
    Feb 21, 2020 at 20:26
  • Yes, refer to the doc. No, do not ONLY refer to the doc. You have at least said to record a sequence, save it in a function, and bind it to a key, so your answer is not 100% link only. But please summarize what you think the doc says that specifically answers the question.
    – Drew
    Feb 22, 2020 at 0:31
  • @RichieHH : Thanks a lot for leading me in the right direction, I'm grateful for that! However, maybe I'm misunderstanding something... Please see the updated question and I hope that behaviour can easily be reproduced on other systems.
    – Okay Dokey
    Feb 22, 2020 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.