unsetting regular key on emacs - emacs

This is a little embarrassing, but recently I happen to bind regular keys on the fly using global-set-key every week or two.
e.g.
I bound the q key to quoted-insert and toggle-read-only today (not on purpose)
I tried global-unset-key, but now pressing the q key gives me q is undefined message (and writes nothing).
Since it isn't the first time this happens to me recently and since I hate losing all the buffers opened in my emacs every time it happens to me, I'm looking for a way to unset a char key without losing the ability to write this char OR to rebind the char to itself (global-set-key not allowing it). Trying to bind to insert-char 71 failed as well

(global-set-key (kbd "q") #'self-insert-command)
This is the default binding for all alphanumeric keys (which of course can be overridden in some major or minor mode).
There isn't a way to reset a key to its default that I'm aware of. If you want to play around with key bindings in a reversible way, probably the best thing to do is make your own minor mode and only bind keys to it (see http://shallowsky.com/blog/linux/editors/emacs-global-key-bindings.html). Then you can easily toggle the mode off, or undefine a key in its map, to get the global binding.
Finally, I use save-visited-files (in melpa) to remember (most of) my buffers. There are more complete solutions like desktop to save and restore state as well.

Related

Emacs: relation between keystrokes and keys

I am slightly confused by the difference between keys and key-bindings in emacs after reading the first chapter of "Writing GNU Emacs Extensions" (O'Reilly book).
The chapter starts by noting that in some emacs version the keys backspace and DEL, invoke the help menu instead of deleting. In other words, they invoke what is invoked by C-h.
The chapter then goes on to show how to change this behaviour using Lisp code. This is done by binding the keystroke C-h to the delete command.
This is a bit confusing to me. In my mind DEL, Backspace and C-h are three different keystrokes (the first two consisting of a single key).
Why does remapping C-h effect what DEL and Backspace does?
I would have thought to change what backspace does for example, you would remap backspace to another command, not the keystroke C-h.
Unless remapped by a low-level keybord driver, the effect of the Backspace key is to send the character with numeric code 8, which, in certain operating systems, is exactly the same code generated by pressing Control-h. You can verify this fact by simply writing anything on a unix (or linux) shell and then pressing Backspace and Control-h: both of them have the effect of erasing the previous character, since the character with numeric code 8 is interpreted by the operating system as the control character BS, backspace, used either to erase the last character printed or to overprint it (see wikipedia). Analogously, Control-J is equivalent to the RETURN key, etc.
The same Wikipedia page describe DEL as “originally intended to be an ignored character, but now used in some systems to erase a character”.
So, when you assign a command to a keystroke you are actually assigning a command to a character code, and if two or more keys generate the same code, by pressing them you are invoking the same command.
#Renzo answered your question about how these keys are related and how binding one can seem to affect another. Here is some more information about what's going on in this particular case.
See the Emacs manual, node DEL Does Not Delete.
There you will see this, following an explanation of the problem/confusion:
To fix the problem in every Emacs session, put one of the following
lines into your initialization file (*note Init File::). For the first
case above, where BACKSPACE deletes forwards instead of backwards, use
this line to make BACKSPACE act as DEL:
(normal-erase-is-backspace-mode 0)
For the other two cases, use this line:
(normal-erase-is-backspace-mode 1)
Another way to fix the problem for every Emacs session is to
customize the variable normal-erase-is-backspace: the value t
specifies the mode where BS or BACKSPACE is DEL, and nil
specifies the other mode. *Note Easy Customization::.
See also the GNU Emacs FAQ question about Backspace invoking help. There you will see, in addition to information similar to that above, information about how to remap DEL on UNIX - use this:
stty erase '^?'
Wrt C-j and RET (not mentioned in the question, but mentioned in #Renzo's answer): The default behavior of Emacs changed in most programming modes, in Emacs 24.4.
Starting with that release, electric--indent-mode is turned on by default, which means that RET inserts a newline character and indents, whereas C-j just inserts a newline character. Prior to that release, these keys had the opposite behaviors. If you prefer the old behavior then do this, to turn off electric-indent-mode:
(when (fboundp 'electric-indent-mode)
(electric-indent-mode -1)) ; Use classic `C-j' and `RET'.

User super key for meta commands in emacs

I'm trying to learn emacs and would like to use the super key on my keyboard for any meta commands in emacs, because I use dwm as my window manager and it uses the meta key extensively. I have read this page, using it to try
(setq x-meta-keysym 'super)
(setq x-super-keysym 'meta)
with no luck. Ideally I'd like to simply flip them, but only in emacs. when I inspect the variables x-meta-keysym and x-super-keysym with C-h v the commands do seem to be evaluated correctly. When I check the super key with the linux utility xev it confirms that the key I'm pressing is mapped to Super_L.
I agree with #Tyler
In my case I use dwm as window manager, so meta (ALT) key is used to control almost all its features. When using emacs, meta key conflicts with some key bindings, eg: when I'm moving around words M-f or Mb (that's why I came to this thread ;)), so I found three choices to get rid with this issue:
1) change emacs key binding and use super key as meta key
(setq x-meta-keysym 'super
x-super-keysym 'meta)
Both symbols (variables) are required, because if you just use only x-super-keysym 'meta, it enables both keys as meta key. If you just use only x-meta-keysym 'super, it disables both and de command buffer will return s-x is undefined.
2) change dwm key binding for meta to super key in config.h file. take a look in http://dwm.suckless.org/customisation/windows_key
/* key definitions */
-#define MODKEY Mod1Mask /* meta (ALT) key */
+#define MODKEY Mod4Mask /* super (windows or cmd) key */
3) override xmodmap keymap bindings swapping mod1 to super and mod4 to meta (be sure to check what xmodmap returns at first glance, and not affect keybindings of other programs)
Finally, I recommend not to change default emacs meta key because is less comfortable to your fingers and may cause you to respond slowly to keystrokes. Try options 2 or 3 depending your environment.
So, it turns out that the above commands work when emacs is not used in terminal mode. I had been trying it in the terminal exclusively which is why it wasn't working. I'm not sure how to fix it in the terminal, but it's good enough I suppose.
edit: it was the terminal emulator that was gobbling the keypresses before they got to emacs, so the original commands do work, so long as the keypress actually makes it to emacs.

Overriding keyboard input in emacs

Is it possible to override/catch all keydown and keyup events during a minor mode?
I would like to do such a thing for several reasons, but mainly to try an alternative input method which I have not found any reasonable way of doing, in which key combinations would produce input, but in which it would be bothersome to re-press keys (it would be simpler to just hold them down if they are used in several combinations in a row).
If your minor mode is at the top of the list of all active minor-modes, its keymap will be queried before all other modes are queried (unless some even higher precedence cases fall into place, cf. "Searching Keymaps" in the Emacs Lisp documentation).
This means that you can at least handle all key events before other modes do, by binding them in your modes' keymap. You can also define a "catch all" key binding that matches every key event that is not otherwise bound in your map:
(define-key my-minor-mode-map [t] 'my-catch-all-command)
However, this will not help you getting information about key releases. Perhaps you'll find it worthwhile checking out Key chord mode for that.

emacs key "deletechar"?

which is emacs key <deletechar>?
Is there any emacs command to check specified character name?
There are two keys mapped to delete-char functions: Delete, and <deletechar>.
Delete key I have remapped, so <deletechar> left, but I can't figure out which one it is.
In Emacs, "delete" and "deletechar" are not specific keyboard keys, they are mapped to specific "key sequences". If you look at the content of the local-function-key-map variable, you may see that "deletechar" is mapped to other "key sequences". Looking at the content of the function-key-map variable will show you what terminal device mapping is in place. The variables key-translation-map and input-decode-map may also have an entry for "deletechar".
If you use both terminal and windowed versions of emacs or you use different terminal types or if you use emacs on different hardware platforms or if you use different versions of emacs, you may find that pressing a keyboard key sometimes generates a "delete" key sequence and sometimes it will generate a "deletechar" key sequence depending on what the specific key sequence mappings are. Therefore, if you have set "delete" to execute one function and "deletechar" to execute another function, the actual function that is executed may vary depending on whether you are running on terminal or windowed emacs, the terminal type, and the version of emacs.
For more info, have a look at the Keymaps for Translating Sequences of Events section of the Emacs manual and the links off the Keymaps section of the Emacs manual.

Emacs locks hard over PuTTY when £ is entered

I'm using emacs (21.4.1) via PuTTY (0.60) connected to a CentOS5.3 box with a UK keyboard. Whenever I enter the £ symbol emacs locks hard, making the whole putty window unresponsive and loosing all changes.
Edit:
Futher to pajato0's suggestion, I get the following message:
à (translated from £) runs the command self-insert-command
which is an interactive built-in function in `C source code'.
which is an interactive built-in function in `C source code'.
It is bound to many ordinary text characters.
So it looks like I need to rebind the key. How would one do this?
To determine what role, if any, Emacs is playing in your scenario, I would suggest that you try C-h k # where # is your pound key. If it hangs, then Emacs is not getting the key at all and you have a Putty/Windows issue most likely. If Emacs tells you the function binding then you have some hope of fixing it by changing the key binding. My best guess is that Putty is capturing the key and sending it to Windows which is dropping the ball.
You could try another ssh client to see if it is putty-specific. I tend to just run
startxwin.sh # from a MinGW rxvt terminal
after which an xterm pops up in which I do
ssh -X some.unix.box # from the new xterm
after which I fire up a tabbed terminal emulator in which I keep several sessions 'forever'. And my US keyboard has no Pound sign so no way for me to test your issue...
I would look at the character set translation that putty is performing (window->translation in settings). It could be that the character code that is being sent for a pound symbol (don't have a UK keyboard...) locks up the terminal somehow, or that the character that is being returned in response to the keypress is causing some similar behavior (e.g. waiting for the completion of a code point).
You might want to try it with everything set to UTF-8 (charset/locale on linux side, and on Putty translation), to minimize the chances of confusion...
In response to your question "how does one rebind a key": use the define-key function, e.g.
(define-key global-map [S-Home] 'beginning-of-buffer)
The hard part is figuring out how to represent your key and then choosing a suitable function. For example, you might do something like:
(defun make-euro ()
"Euro")
(define-key global-map # 'make-euro)
But I would also suggest that you file a bug since if trying to self-insert your # key causes Emacs to hang, that is clearly Emacs breakage that should get fixed.