Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

emacs

elisp

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 ;

like image 485
rottweiler Avatar asked Mar 24 '12 23:03

rottweiler


2 Answers

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.

like image 55
Stefan Avatar answered Nov 15 '22 05:11

Stefan


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
like image 35
phils Avatar answered Nov 15 '22 04:11

phils