Use semicolon in global-set-key for function in .emacs - emacs

I am trying to set [Ctrl]-[;] as a keybinding in my .emacs like this
(global-set-key "\C-;" 'comment-or-uncomment-region)
however it doesn't work when I try (i don't get any error messages, it just has no effect). It will work though if i try a normal character (such as setting it to "\C-p").
I have also tried
(global-set-key (kbd "C-;") 'comment-or-uncomment-region)
but I don't like this option because for me it doesn't work when i run "emacs -nw". Any thoughts on how I can do this?
EDIT:
When I run C-hcC-; in emacs -nw I get the output:
; runs the command self-insert-command
which is exactly the same as when I run C-hc; in emacs -nw
So I believe phils is right, that it is a terminal problem, because emacs never actually sees C-;, it only sees ;

Indeed C-; is typically not something your terminal is able to send to an underlying application like Emacs (so it works under a GUI but not in a terminal). But I wonder: why do you need such a binding anyway, given that M-; is already bound to comment-dwim which does comment/uncomment the region when the region is selected, so it provides a superset of comment-or-uncomment-region.

Using (kbd "C-;") is absolutely fine and correct.
I suspect when you type C-; when running emacs -nw, your terminal is not actually sending anything to Emacs.
So your problem is more likely to be a question of how to get your terminal to send C-; to Emacs (or alternatively how to get Emacs to recognise the sequence which is sent).
If you run emacs -Q -nw and type C-hcC-;, do you get a "C-; is undefined" message?
Assuming that it is a terminal issue, here are some related Q&As which may point you in the right direction, but it's going to depend upon the particular terminal you are using.
Binding M-<up> / M-<down> in Emacs 23.1.1
Send "C-(" to Emacs in VT100/xterm terminal (Mac OS X's Terminal)?
How does one send S-RET to Emacs in a terminal?
emacs -nw issues with cscope and terminals

Related

How to use terminal keyboard shortcuts inside emacs multi-term

I know there's some questions that are kind of related to this already, and I know you can do stuff like
(defun term-send-esc ()
"Send ESC in term mode."
(interactive)
(term-send-raw-string "\e"))
but it would be very convenient if there was kind of a univerisal override keybinding. For example, I just ssh:ed into a remote server and tried to nano a file and couldn't figure out how to exit because Ctl-x listens for emacs bindings. Is there such a thing?
There are two term sub modes, term-char-mode (C-x C-k) and term-line-mode (C-x C-j). Char mode is closer to a terminal, line mode is closer to a regular buffer. IIRC, multiterm starts in char mode, but it rebinds some keys to make it more Emacs-like. Look at the keys that multiterm rebinds; it has some alists that are used to modify the existing term-mode keymaps instead of using its own keymap. Or use sane-term, which is not much more than some commands to create new term buffers (no mucking with key bindings).
Ctl-x listens for emacs bindings.
Not in the default term-char-mode it doesn't, so your problem is most likely with your own config.
Run a terminal in emacs -Q to confirm the standard behaviour.

I'm having issues setting up ansi-term in emacs [duplicate]

I use ansi-term for my normal terminal sessions. I tend to use unicode characters in my prompt to do things like set the trailing character based on the type of source control I'm using.
I use the character "±" as my prompt for git repositories.
In Emacs' ansi-term, my prompt isn't rendered as unicode, and shows as "\302\261". Displaying the current coding system shows that it defaults to utf-8-unix for input to the process, but I get raw binary as the decoding output. I can hit C-c RET p to change the encoding and decoding coding systems. I'm drawing a blank as to how to set this automatically when I start a terminal? I've tried adding to term-mode-hook to set the buffer's coding system to no avail. I think I've found what I'm looking for in term.el, but I don't care to tweak the distribution elisp, and it appears the raw binary was added to fix a bug somewhere else.
EDIT: This was unclear originally. I'm having issues setting the default process coding system for ansi-term running under Cocoa-ized Emacs 23.3 on MacOS. Emacs itself isn't running in a terminal, my terminal is running in Emacs.
The following worked for me:
(add-hook 'term-exec-hook
(function
(lambda ()
(set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))))
ansi-term seems to ignore the default-process-coding-system variable, so I had to set it buffer-locally after it executes my shell.
After getting a better understanding of term.el, the following works:
(defadvice ansi-term (after advise-ansi-term-coding-system)
(set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))
(ad-activate 'ansi-term)
Trying this with term-mode-hook is broken because in term.el, term-mode-hook is called before switching to the terminal buffer, so set-buffer-process-coding-system breaks due to the lack of a process associated with the buffer.
Try
(set-terminal-coding-system 'utf-8-unix)
That's C-x RET t not C-x RET p.
So C-x RET p helps?
Unless C-h v default-process-coding-system is (utf-8-unix . utf-8-unix) try
(setq default-process-coding-system '(utf-8-unix . utf-8-unix))

Unicode characters in emacs term-mode

I use ansi-term for my normal terminal sessions. I tend to use unicode characters in my prompt to do things like set the trailing character based on the type of source control I'm using.
I use the character "±" as my prompt for git repositories.
In Emacs' ansi-term, my prompt isn't rendered as unicode, and shows as "\302\261". Displaying the current coding system shows that it defaults to utf-8-unix for input to the process, but I get raw binary as the decoding output. I can hit C-c RET p to change the encoding and decoding coding systems. I'm drawing a blank as to how to set this automatically when I start a terminal? I've tried adding to term-mode-hook to set the buffer's coding system to no avail. I think I've found what I'm looking for in term.el, but I don't care to tweak the distribution elisp, and it appears the raw binary was added to fix a bug somewhere else.
EDIT: This was unclear originally. I'm having issues setting the default process coding system for ansi-term running under Cocoa-ized Emacs 23.3 on MacOS. Emacs itself isn't running in a terminal, my terminal is running in Emacs.
The following worked for me:
(add-hook 'term-exec-hook
(function
(lambda ()
(set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))))
ansi-term seems to ignore the default-process-coding-system variable, so I had to set it buffer-locally after it executes my shell.
After getting a better understanding of term.el, the following works:
(defadvice ansi-term (after advise-ansi-term-coding-system)
(set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))
(ad-activate 'ansi-term)
Trying this with term-mode-hook is broken because in term.el, term-mode-hook is called before switching to the terminal buffer, so set-buffer-process-coding-system breaks due to the lack of a process associated with the buffer.
Try
(set-terminal-coding-system 'utf-8-unix)
That's C-x RET t not C-x RET p.
So C-x RET p helps?
Unless C-h v default-process-coding-system is (utf-8-unix . utf-8-unix) try
(setq default-process-coding-system '(utf-8-unix . utf-8-unix))

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

Certain keybindings don't work when using emacs in a terminal

I load GNU emacs in it's own window by typing emacs in the terminal.
I like to use the keybindings from pc-selection-mode, which allows you to highlight characters using shift-right or shift-left, or entire lines by pressing shift-up or shift-down.
The problem is that when I run emacs in the terminal by typing emacs -nw, the latter 2 keybindings don't work. I can highlight characters using shift-left and shift-right, but pressing shift-up and shift-down doesn't do anything. The cursor stays where it is.
How do I fix this problem? Why is it even occurring? I'm using GNU Emacs 23.1.1, and I've confirmed that the same version is being used both when emacs is in it's own window and when emacs is running inside the terminal.
Thanks for any help
A lot of times the bindings just aren't listed, or are mapped wrong. You can try M-x show-lossage (or C-h l) to see if the escape sequences reach emacs or not. If they do, you might want to try xterm-extras -- it's always worked even as I migrate between different versions of linux and solaris, and as I ssh between them (which is often a source of problems).