emacs-nox 'C-,' & 'C-.' keybindings not working - emacs

I've got a couple emacs keybindings as follows:
(global-set-key (kbd "C-,") 'beginning-of-buffer)
(global-set-key (kbd "C-.") 'end-of-buffer)
Recently, I realized these don't work in emacs-nox. Is it is possible to get them working, or is this just a limitation of emacs-nox?

By popular demand:
It's probably a limitation of your terminal.
Most terminals I've used don't send any keycodes at all for C-, and C-..
You can check this by executing M-x describe-key (usually bound to C-h k), and then typing C-, and/or C-.. If Emacs does nothing when you hit the keys, it's your terminal.

On my emacs(21), \C-. and \C-, don't register as keyed. I use terminal through PuTTY. The default keybindings \M-< and \M-> works fine for me for beginning-of-buffer and end-of-buffer respectively.
So either use the default keys or set some other keys if your emacs-nox does not register them as keyed in.

Related

Bind C-, and C-

Is there anyway that we can bind C-, and C-. in emacs ? I have tried this (define-key global-map (kbd "C-,") 'action) but it doesn't seem to work.
I use Emacs on Ubuntu with US keyboard layout.
Thanks
I'm pretty your define-key does work.
But most likely when you hit C-, Emacs doesn't actually receive this.
What does C-h k C-, tell you? What about C-, C-h l ?
My crystal ball tells me you're running in a text-terminal of some sort. In those beasts many key combos don't actually get through correctly to the running application.
You could try some key binding commands, e.g.
(global-set-key [(control ?,)] 'some-action)
in your ~/.emacs file (to make your key binding permanent, and available after restart). BTW, you could evaluate that first to test it. Then restart your emacs and use C-h k C-, to check if it is bound.

How to set new keyboard shortcuts in emacs?

I am new to emacs.
Is there a way set new key binding permanent for future use. That is I have a set of key bindings I would like to use in all of my future emacs sessions.
The following command is getting deleted (or forgotten) every time I quit emacs.
M-x global-set-key new key-binding command
How to save this key binding for future use?
Save the definitions into your .emacs file located in your home directory.
For example, I have the following there:
(global-set-key "\M-m" blink-matching-open)
This is covered comprehensively in the manual.
A number of answers above (and elsewhere on SO) didn't work for me. First, the meta key is referred to by something else in my emacs 24.3.1 with GTK+ version 3.10.7 (on Ubuntu 14.04).
A number of options pre-existing on my init.el didn't work.
There options on my init.el didn't work (and they include the one in here):
(a) (global-set-key (kbd "M-<up>") 'comment-region)
(b) (global-set-key (kbd "<M-up>") 'comment-region)
(c) (global-set-key [(meta up)] 'comment-region)
Then, I discovered that when I do a M-x describe-key and pressed metakey + up cursor arrow, the description said the key was <s-up>. I tried this:
(a) (global-set-key '<s-up>' 'comment-region)
Which didn't work either.
I then tried:
(a) (global-set-key [s-up] 'comment-region)
Which is the only one that worked after I exited the session.
I think a number of things need to be improved for both the emacs manual and the in-editor help with respect to keyboard shortcuts. As a nearly 20-year user of emacs, I can vouch for this. The OP's confusion is legitimate.

Emacs/elisp: global-set-key bindings not taking effect for Meta-<down> or -<up>?

I am trying to bind M-<up> and M-<down> to scroll-down-line and scroll-up-line respectively as indicated here: https://stackoverflow.com/a/16229080/562139.
This is what I have in my .emacs:
;; Key bindings
(global-set-key (kbd "M-g") 'goto-line)
;; Scroll line by line
(global-set-key (kbd "M-<down>") 'scroll-up-line)
(global-set-key (kbd "M-<up>") 'scroll-down-line)
Problem:
The scroll key bindings are not taking effect, while the one for goto-line does.
When I run M-x scroll-down-line however, emacs prompts me and says
"you can run the command with <M-down>"
Note:
When I run global-set-key (kbd "M-<down>") 'scroll-up-line) or (global-set-key (kbd "M-<up>") 'scroll-down-line) directly in the mini-buffer, the bindings take effect! However, I seem to have noticed through the corner of my eye when I do the latter, that pressing M-<up> actually sends something like ESC ESC-<up>.
I'm foxed. What gives?
Note: I am running emacs 24.3 in a terminal (via iTerm on OSX with Option key mapped to ESC+) over SSH to a RHEL5 virtual machine.)
Update
I followed the suggestion in this answer and found that pressing M-<up> results in something completely different:
ESC <up> (translated from ESC M-[ A) runs the command
scroll-down-line, which is an interactive compiled Lisp function.
It is bound to <M-up>, ESC <up>.
(scroll-down-line &optional ARG)
I'm going to try binding that key sequence to the function and check the result.
Try starting Emacs without your init file: emacs -Q, and see if you can reproduce the problem.
I do not see the problem, with Emacs 24.3 in terminal mode.
What you saw briefly was probably ESC <up>, which is equivalent to M-<up>.
Did you perhaps mean to type "When I run M-x scroll-up-line (instead of down)?
I suspect that you are in some mode that gives a local binding or a minor-mode binding to these keys, which overrides the global binding. To test that, try in a buffer that is in fundamental mode. If that is the case, then to override that overriding you will need to also bind the keys in that mode's keymap.
If you cannot repro the problem starting from emacs -Q then bisect your init file (~/.emacs) recursively until you find the culprit code.
Seems key got lost in translation.
Planted a forward-paragraph at openSuse that way:
(global-set-key [(meta down)] 'forward-paragraph)

Bind command to C-RET in Emacs

Say I have some interactive function in Emacs my-function, how can I bind it to Ctrl + RET?
I have tried with:
(global-set-key (kbd "C-RET") 'my-function)
and
(global-set-key (kbd "C-return") 'my-function)
but none of them seem to work. is this at all possible?
Always remember that kbd very conveniently accepts the exact same syntax that Emacs gives you when you ask it about a key sequence, so you never ever have to guess.
C-hkC-RET tells me:
<C-return>
therefore I would use (kbd "<C-return>")
OTOH, when running Emacs in my terminal, C-hkC-RET tells me:
C-j
because C-RET isn't a valid control character in a terminal, and therefore Emacs isn't receiving the same input that it gets in GUI mode (so I wouldn't be able to use that binding in my terminal).
This should work:
(global-set-key [(control return)] 'my-function)
It works for me, but may not in a terminal as per #phils's answer.

emacs -- keybind questions

I have successfully used Ctrl+Shift+Up ' Ctrl+Shift+down '
Ctrl+Shift+left' Ctrl+Shift+Right to different commands. But when I
tried to use Ctrl+s to the command save-buffer and Ctrl+Shift+s, which
is equivalent to Ctrl+S, to another command, it has some problem.
save-buffer works fine, but when I type Ctrl+Shift+s, it excute
the command save-buffer. I used Ctrl+q to find the control sequences of
Ctrl+s and Ctrl+Shift+S, I get the same result, which is ^S.
I expect that I will get ^s for Ctrl+s, but it doesn't.
Anyone knows the reason?
Another queston is: I use Ctrl+c for the command killing-ring-save. In this
case, all commands (which are of large number) begin with Ctrl+c don't work now.
Is there a way to replace the prefix Ctrl+c by another customized prefix?
I may pose my question in the wrong direction. I use ctrl+c as
killing-ring-save. It works fine in emacs (no mode). But if I open a .c file (C-mode), then
when I type Ctrl+c, it waits me to type another key. I think in this case,
ctrl+c is regarded as a prefix. In this case, I need the following modifications:
Using a custom defined prefix, say Ctrl+a, as Ctrl+c ; Remove the
prefix Ctrl+c ; Using Ctrl+c as killing-ring-save.
I add the following to my ~/.emacs :
(global-set-key (kbd "C-a") mode-specific-map)
(global-set-key (kbd "C-c") 'kill-ring-save)
(global-set-key (kbd "C-f") 'isearch-forward)
(global-set-key (kbd "C-v") 'yank)
(global-set-key (kbd "C-s") 'save-buffer)
(defun my-c-initialization-hook ()
(define-key c-mode-base-map (kbd "C-a") mode-specific-map)
(define-key c-mode-base-map (kbd "C-c") 'kill-ring-save))
(add-hook 'c-initialization-hook 'my-c-initialization-hook)
But this doesn't work. Ctrl+c is still regarded as a prefix, so I can't use it
as kill-ring-save. Furthermore, if I type Ctrl+a Ctrl+c, it said it's not
defined. (I thought it will have the same result as I type Ctrl+c Ctrl+c)
The C-c binding is tricky, CUA mode solves it well, by only making it do kill-ring-save when you have a region marked.
First, Control-S is an ASCII control character -- ^s and ^S are the same character.
Keys are something different from characters, however, and if you are using Emacs with a window manager then you can distinguish the keys C-s and C-S-s. The latter is Control-Shift-s.
The problem you are hitting is that if you do not explicitly bind the shifted version of a letter key, then the shifted letter key uses the binding of the unshifted key. This is a "feature".
So you need to bind both C-s and C-S-s.
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'another-command)
If you're running emacs in a terminal, then the reason for the shift-ctl-c issue could be the terminal driver. In that case, give the command stty stop undef, then run emacs again, and see if it affects the problem. Also, see if you get same problem with shift-ctl-other letters