emacs key binding command affects another - emacs

Previously I used C-SPC to activate/deactivate mark, now I use expand-region package and set key binding to C-# like this:
(global-set-key (kbd "C-#") 'er/expand-region)
But this affected the C-SPC keybinding also, so it is also bound to expand-region.
What I need is C-# bind to expand-region and C-SPC to bind to old activate/deactivate mark.

Suggest you refer to:
set-mark-command not working emacs with C-SPC
and
https://www.gnu.org/software/emacs/manual/html_node/emacs/Setting-Mark.html
Quoting from the latter:
"Footnotes [1] There is no C-<SPC> character in ASCII; usually, typing C-<SPC> on a text terminal gives the character C-#. This key is also bound to set-mark-command, so unless you are unlucky enough to have a text terminal that behaves differently, you might as well think of C-# as C-<SPC>."
I think you'll find that they are not separate keys; C-SPC sends a code that's the same as C-#. I think that means you'll have to find somewhere else to bind one of the functions, (even if you have to override expand-region)

Apologies for a second answer... I think the first was wrong because I have now been able to make separate definitions for C-SPC and C-#, as described below.
This works to define C-# and C-SPC separately:
(global-set-key [?\C-#] 'beginning-of-line)
(global-set-key (kbd "C-SPC") 'end-of-line)
To give credit, I derived the answer from here: Rebind C-space in Emacs
after googling "emacs control space"
(Regarding your question, "what key should I bind this expand-region to?", I ordinarily use C-h C-k and type some key I don't think I use. Then look at the function that that key is bound to by default. If it seems useful to me, I try another key and keep looking. If I feel like I will never use the default definition, I redefine it for my own purposes.)

Related

In Spacemacs (or Emacs), is it possible to bind keys to other keys?

Here is one thing that has been hard to find out. In Spacemacs (or Emacs), is it possible to have a given key combination set to execute whatever another key combination is supposed to do?
In other words, is it possible, for example, to have the F5 key always execute whatever the combination C-c C-c is supposed to do? Something like:
(global-set-key [remap (kbd "F5")] (kbd "C-c C-c"))
Let me give a concrete example for clarity. The key combination C-c C-c is often used to send the current buffer's entire code to interpreter/console, be it to send the buffer's code to the Python interpreter in case the code resides in a .pyfile, or to send it to a R console with package ESS in a .R file.
Now, suppose that one wants to have the F5 key set to do that for these two languages and maybe even more. One obvious solution would be to simply implement key bindings that are different for each package. However, it would be much easier if it was possible to simply say: whenever F5 is pressed, it should result in whatever command associated to `C-c C-c being called.
Is it possible to have such a thing? I am particularly interested in doing that for Spacemacs, but of course a more generic answer for Emacs is acceptable.
Try
(define-key key-translation-map [f5] (kbd "C-c C-c"))
It should solve your problem.

Emacs: How to bind key only in regular buffers and not in the minibuffer?

I have written a fancy function, which I would like to bind to TAB. The functionality is only meaningful in any non-read-only text buffer. Currently, I bind it either like that:
(global-set-key (kbd "<tab>") 'my-indent-region)
or
(define-key global-map (kbd "<tab>") 'my-indent-region)
The problem with this binding is that now tab-completion does no longer work in the minibuffer, which is an essential feature (e.g. for buffer/file names, or M-x).
Is it possible to bind TAB only for regular modes? I know that I can use define-key some-major-mode-map, but since I want it in all modes except for the minibuffer, this would be annoying to maintain. Thus, I'm probably looking for something like a define-key any-mode-except-minibuffer ....
If such a functionality does not exist: Is there a workaround to get the tab-completion working in the minibuffer again? Maybe I can re-set the original minibuffer tab binding after changing the global binding? I couldn't figure out though which function I actually have to bind to make it work.
After some more research I found a workaround/solution to the problem in this answer.
Apparently, my problem was that I was binding to (kbd "<tab>"). If I understand it correctly, my problem was in fact not that I overwrote the actual keymap of the minibuffer -- I guess they are correctly loaded when entering the minibuffer minor modes. However, there seems to be a precedence of a binding to (kbd "<tab>") over a binding to just "\t". According to the above answer, the minibuffer bindings just use "\t", so binding to (kbd "<tab>") shadows them. I'm now using the following binding instead:
(global-set-key "\t" 'my-indent-region)
Now everything seems to be working fine.
Do you see this behavior when you start Emacs without your init file (emacs -Q)? I doubt it. If not, then recursively bisect your init file to find out what is causing the problem.
The minibuffer uses its own keymaps, which are local and which therefore take precedence over global keymap bindings.
However, any minor-mode keymaps take precedence over local keymaps. So if, for example, you have a (global) minor mode turned on that binds <tab> then that will override any binding for that key in the minibuffer keymaps.
Another thing you can do is simply bind whatever command you want to <tab> in the minibuffer keymaps. But again, you should not need to do that, if you want the usual <tab> behavior for the minibuffer.
[Another possible confusion: Some things, such as Isearch, which you might think use the minibuffer do not use it. Isearch uses its own keymap, isearch-mode-map.]
UPDATE after your comment:
Assigning a key in the global map, as you have done, should not affect what that key does in the minibuffer, provided it has a different binding in the minibuffer keymaps. TAB is typically bound in all of the minibuffer completion keymaps (but not in the non-completion minibuffer keymaps).
See the Elisp manual, nodes Completion Commands and Text from Minibuffer for information about the minibuffer keymaps.
To see what the current bindings are for a keymap that is associated with a variable (such as minibuffer-local-completion-map), load library help-fns+.el and use C-h M-k followed by the keymap variable's name. (See Help+ for more information about the library.)
If you do not want TAB to use your global command binding in the non-completion minibuffer maps (minibuffer-local-map, minibuffer-local-ns-map), then just bind it in those maps to whatever command you like. But for the completion maps you should not need to do anything - TAB should already be bound there.
Did you try emacs -Q, to see if something in your init file is interfering? If not, do that first.

emacs get rid of C-x

Is there a way I can stop needing C-X before any shortcut and, for example, just use C-c to leave?
Also, if possible, what disadvantages could this have?
I'm looking for a simpler way of using GNU Emacs, but not sure if I can find one.
(this is too long for a comment)
Not really an answer but I know that some people hate the "C-x anything" in Emacs not just because you have to "type a lot of keys" but also because from a touch-typing point of view C-x doesn't make that much sense.
But of course in Emacs everything is configurable. I'm using "C-," instead of C-x and I honestly find it much easier this way.
I use my left pinky to hit CTRL, which is the key physically located at the left of 'a' on a QWERTY keyboard: i.e. the key that used to be CTRL but that now often is labelled CAPS-Lock [and you can remap it to CTRL].
I then touch-type ',' with my right hand.
To this end I added this to my config:
(define-key global-map [(control ,)] ctl-x-map)
You won't have less keys to type to do, say, a C-x C-c (which you can now do both by doing C-x C-c or by doing C-, C-c), but at least you won't be distorting your fingers as much ; )
Also note that as explained here (see user "scottfrazer"'s +40+ upvoted and accepted answer) it may be better to create a minor mode for all your key mappings instead of directly define a global mapping:
Globally override key binding in Emacs
My recommendation is to first learn the standard Emacs key bindings, then, after a while change whichever ones you think will do you the most good to change.
See also CUA mode, in the Emacs docs -- I don't use or recommend CUA mode, but a lot of people (esp. newbies?) use it to keep their cut, copy, paste key habits.
After looking at the source code of boon.el, I found out about ctl-x-map. C-x is a prefix key, prefix keys often have their own maps.
To use M-m for C-x shortcuts and unbind C-x, use this
(global-set-key (kbd "M-m") 'ctl-x-map)
(global-set-key (kbd "C-x") nil)
I think it is a very bad idea, because the C-x prefix can handle such situations like C-x s vs C-x C-s. And: you are really f*cked when you read emacs tutorials and have your own keybindings
If you want some other keybindings read ErgoEmacs Keybinding or How to Set Emacs's User Interface to Modern Conventions

emacs - [control shift up] doesn't work

I tried to define hotkey as following
(global-set-key [(control shift up)] 'other-window)
but it doesn't work (no error, just doesn't work), neither does
(global-set-key [(control shift down)] 'other-window)
But
(global-set-key [(control shift right)] 'other-window)
and
(global-set-key [(control shift left)] 'other-window)
work!
But because the last two key combinations are used by emacs (as default), I don't wanna change them for other functions.
So how could I make control-shift-up and control-shift-down work?
I have googled "(control shift up)", it seems that control-shift-up is used by other people, (but not very few results).
The reason for this is not an Emacs problem, but comes from the fact that your terminal cannot produce a key sequence for C-S-up.
You can verify this very easily. Open a terminal and then type:
Control-v Control-Shift-right
The Control-v part will make the control sequence for the next key be inserted verbatim into your shell. In our case, it will insert the sequence for Control-Shift right, and that'll look something like this:
^[[1;6C
Now, try the same thing for C-S-up:
Control-v Control-Shift-up
You'll see that no control sequence is entered, which hints at the fact that when you press C-S-up in Emacs, it will actually never receive anything, because the terminal is not able to produce anything to pass on to Emacs.
We can double-verify this if you just start a new emacs -nw and type C-h k to invoke Emacs' describe-key function. You'll get asked in the minibuffer to type a key to see what function it is bound to. If you now type C-S-up nothing happens - of course not, since the terminal in which your Emacs runs doesn't produce anything.
However, if you're just looking for an unused key-combination, how about just Shift-up (or even Shift-right) without Control? That one should work both in a terminal emacs and in the windowed version.
Finally, with the help from grawity on superuser.com, I got it working. Please this thread
https://superuser.com/questions/230852/get-ubuntu-terminal-to-send-an-escape-sequence-controlshiftup
This could well be a duplicate of:
Binding M-<up> / M-<down> in emacs 23.1.1
If this is the case, Gilles' answer should sort you out (undoubtedly with different escape sequences, though.)
edit (for better visibility -- see answer below):
It turned out that gnome terminal does not transmit these key combinations, so the solution relies upon the following: https://superuser.com/questions/230852/get-ubuntu-terminal-to-send-an-escape-sequence-controlshiftup

writing lisp emacs key binding and cannot specify the <delete> character

For some reason I got the default M-del key binding for backward-kill-word mapped to a scan for matching brackets and resetting is not working, so I am trying to set the global key binding in lisp. So I wrote in ~/.emacs.d/init.el the lisp commands:
(global-set-key (kbd "M-h") 'backward-kill-word)
(global-set-key (kbd "M-<\delete>") ‘backward-kill-word)
I tried them with C-x C-e and they both give the 'backward-kill-word output but only the first key-binding works "M-h", the other is ignored and M-del still trying the strange scanning action. The delete key works in emacs elsewhere, so it seems like "delete" is not being mapped to the physical key in lisp (and the backslash is there to show in this text only as the word was being commented out). Any idea what keyword to use or special character?
Best.
(I looked for libraries that may have overrided this command but I cannot find them)
On some systems, the delete key is defined as an alias to C-d. This is done through function-key-map on GNU Emacs <23 and local-function-key-map on GNU Emacs 23. (I've observed this behavior on Debian and Ubuntu 10.04 under X.) The purpose of such translations is to isolate people who code modes from the terminal intricacies: a mode that wants to shadow the delete command only needs to rebind C-d and not wonder if it should rebind delete (is that a delete left or delete right?) or deletechar or something else.
If there is a global or local binding for delete, it shadows this translation to C-d. However, if you press ESC delete, if there is no global or local binding for ESC delete, the second key is translated to C-d. This translation has precedence over the interpretation of ESC delete as M-delete. So ESC delete becomes equivalent to C-M-d.
This is arguably a bug in Emacs: the effect of ESC delete should be the same as M-delete, and there is no reason why ESC delete would run down-list which has nothing to do with deletion.
There are several possible fixes; I don't know which is best. One that should work with any version of Emacs is
(global-set-key [?\e delete] 'backward-kill-word)
The really nice thing about kbd is that what you type there is the same string that Emacs displays. So, try the following
C-h k M-<\delete> (to use your syntax)
or
M-x describe-key M-<\delete>
Emacs (for me) responds with:
M-DEL (translated from <M-delete>)
runs the command backward-kill-word,
which is an interactive compiled Lisp
function in `simple.el'.
It is bound to , M-DEL.
(backward-kill-word arg)
....
Which you can see shows that the representation for the key you want is M-DEL or M-delete.
Which is a long way of getting to the point that what you want is
(global-set-key (kbd "M-delete") 'backward-kill-word)
Of course, if you have something in your .emacs that overrides it, the above won't help. You'll need to find that included library and stop using it (or customize its behavior).
You might want to call global-set-key interactively to see how it interprets meta-delete. Also try local-set-key to ensure the strange binding is not mode-specific.
After not being able to find the library holding the conflict I found this webpage
http://www.cs.cmu.edu/cgi-bin/info2www?%28emacs%29Rebinding
Changing Key Bindings Interactively...
`M-x global-set-key KEY CMD '
Define KEY globally to run CMD....
Normally, C-z' is bound to the function
suspend-emacs' (when not using the X Window System), but you can
change C-z' to invoke an interactive subshell within Emacs, by binding
it toshell' as follows:
M-x global-set-key <RET> C-z shell <RET>
`global-set-key' reads the command name after the key. After you
press the key, a message like this appears so that you can confirm that
you are binding the key you want:
Set key C-z to command:...
And now the standard default is returned to by doing
M-x global-set-key M-del ...
backward-kill-word
But this is transient and must be done on each reload, any way to make this permanent?
Putting a command into the init.el is not overriding the other effect