Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a key behave as another key

I want certain keys and key combinations to behave as other keys or key combinations in Emacs. For example, I want F5 to behave as a substitute for C-c for every possible combination involving it, or C-S- as C-. Is it possible to do that without manually rebinding all such key combinations?

like image 962
Mirzhan Irkegulov Avatar asked May 04 '12 16:05

Mirzhan Irkegulov


People also ask

How do I reassign my keyboard keys?

To reassign a keyConnect the keyboard that you want to configure. Select the Start button, and then select Microsoft Mouse and Keyboard Center. From the displayed list of key names, select the key that you want to reassign. In the command list of the key that you want to reassign, select a command.

How do I remap a key with KeyTweak?

Find the key you want to be the new input and select it.Once you've chosen your key input and output, click on "Remap Key". If you want to change several keys, do this for the rest of the keys you want to change before moving onto the next step.

How do you reassign keys on a Mac keyboard?

You can change the action that modifier keys, such as Control or Option, perform when you press them. On your Mac, choose Apple menu > System Settings, then click Keyboard in the sidebar. (You may need to scroll down.) Click Keyboard Shortcuts on the right, then select Modifier Keys in the list on the left.


2 Answers

The keys you are referring to are known as 'prefix keys'. A prefix key has its own keymap, so to make another key behave the same, you need to assign it to the same keymap. For control-c, you use the mode-specific-map:

(global-set-key (kbd "<f5>") mode-specific-map)

Control on its own isn't a prefix key, or really a key at all, since it doesn't send a keypress to Emacs[1] until you hit another key. I'm not sure how to remap C-S- to C- within Emacs. You could do it system-wide with xmodmap, but that's probably not what you want.

[1] the control key (and shift, alt) do send a keypress to the operating system, but Emacs doesn't 'see' this unless there's another key pressed at the same time

like image 130
Tyler Avatar answered Sep 24 '22 05:09

Tyler


I prefer

(define-key key-translation-map [f5] (kbd "\C-c"))

Here is a good resource.

To summarize the link given above: The disadvantage of global-set-key is that, when you define a key combination to enter a symbol, it doesn't work in isearch.

key-translation-map also has a problem. Imagine you defined a symbol | to execute a command and C-| to enter the symbol |, pressing C-| will execute the command.

like image 24
pmr Avatar answered Sep 21 '22 05:09

pmr