-   Linux - Software (
-   -   emacs in a terminal : problems with ctrl (

Willard 06-23-2010 02:11 PM

emacs in a terminal : problems with ctrl

I am one of those weirdos that likes running emacs from a terminal. I always do this because I sometimes must (when editing remotely through SSH), so I won't have to switch between a graphical editor and a terminal editor.

Usually, things work just fine. However, problems arise when I must send a "control-something" (like "C-RET" and "C-,") command to my editor. In graphical mode, this works fine. In a terminal, it does not.

In the current terminal that I am using (gnome-terminal), typing "C-RET" will send "C-j" to my editor. Furthermore, typing "C-," will send "," to my editor.

A non-solution would be to "not use these commands". However, I am using some major emacs modes (proofgeneral/coq-mode and agda2-mode), which have keyboard shortcuts like this:

"C-c C-RET"

"C-c C-,"

These are very useful shortcuts, and I am crippled without them.

So far, I am getting around this issue by adding to my .emacs configuration file an entry which makes emacs behave as expected when emacs receives this incorrect command:

(global-set-key (kbd "C-c C-j") 'proof-goto-point)

I would, however, prefer not to have to do this for every major mode I use. Furthermore, if a mode happens to use "C-c C-j", then this re-set keybinding will override it.

My question is the following:

PROBLEM: How do I ensure that emacs correctly interprets a "C-X" command, where X is *any* character?

A noteworthy detail here is that when I run emacs in an xterm, typing "C-," will in fact send "C-," to emacs. However, typing "C-RET" will send "C-<return>" to emacs. So the problem seems to be how the terminal translates a CTRL-something to a key, and sends it to emacs. So one solution could be to

SOL.1: Make sure your terminal sends the right signals to emacs.

However, this may not be possible (I am not about to dig into the belly of a terminal implementation, or to implement my own terminal emulator). Alternatively, we could accept that a terminal sends CTRL-something incorrectly to emacs / emacs interprets CTRL-something incorrectly. Accepting this, the solution to this problem would be to

SOL.2: Make sure emacs interprets the incorrect signals correctly.

A third option, which is probably the easiest one, is to realize that this is too challenging, and

SOL.3: Make sure that emacs reacts correctly when seeing "C-<return>", when emacs is run in an xterm.

(this seems to be the only problem with running emacs in an xterm.)

I am writing this here because I do not know how to proceed with any of these solutions, and am hoping someone here does.

Kind regards,

rweaver 06-23-2010 05:22 PM

Typically this is related to your termcap, what terminal program are you using and what term are you telling the system that you're using? Do you have any additional keymaps?

Willard 06-23-2010 07:50 PM


Originally Posted by rweaver (Post 4013138)
Typically this is related to your termcap, what terminal program are you using and what term are you telling the system that you're using?

I am (was) using gnome-terminal. I followed this guide to make any application running in my terminal aware that I am using a terminal that can display more than 8 colours.

> env | grep TERM


Originally Posted by rweaver (Post 4013138)
Do you have any additional keymaps?

I changed the way X treats "Caps Lock".

> cat .Xmodmap
remove lock = Caps_Lock
add control = Caps_Lock

I tried working more with xterm to have it understand all the CTRL-X I need, and realised that there is in fact no problem with "C-RET". However, "C-SPC" (control space) was a problem; emacs received "C-@". This seems to be the only issue with running emacs in an xterm.

Just in case this can cause problems, here are the configurations I made to xterm:

> cat .Xresources
xterm*foreground:      gray
xterm*background:      black
XTerm*metaSendsEscape: true
XTerm*font:            9x15
Xft.antialias:        1
Xft.hinting:          1
Xft.hintstyle:        hintfull
Xft.dpi:              96

The xterm*metaSendsEscape: true turned out to be important, for else emacs would interpret "M-x" (alt x) as "" (a random forum somewhere suggested that this was an issue with using the UTF8 locale as opposed to the ISO8859-1 locale).

In case it is relevant, here is my (ugly) .emacs file.

> cat .emacs
;; Set default character encoding to UTF-8.
(set-language-environment "UTF-8")
(set-input-mode (car (current-input-mode))
  (nth 1 (current-input-mode))
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)

;; Replace tabs with spaces, etc.
(setq-default tab-width 8)
(setq-default c-basic-offset 4)
(setq-default indent-tabs-mode nil)

;; Have emacs store temporary files in ~/.emacs-backup.
(defvar user-temporary-file-directory "~/.emacs-backup")
(make-directory user-temporary-file-directory t)
(setq backup-by-copying t)
(setq backup-directory-alist
      `(("." . ,user-temporary-file-directory)
        (,tramp-file-name-regexp nil)))
(setq auto-save-list-file-prefix
      (concat user-temporary-file-directory ".auto-saves-"))
(setq auto-save-file-name-transforms
      `((".*" ,user-temporary-file-directory t)))

;; remove toolbar in graphical emacs
(tool-bar-mode -1)

;; remove scrollbar in graphical emacs
(scroll-bar-mode -1)

(if (null window-system)
    (defvar config-is-graphical-window-system nil)
    (defvar config-is-graphical-window-system t))

(setq auto-mode-alist (cons '("\\.jr$" . java-mode) auto-mode-alist))

(autoload 'flyspell-mode "flyspell" "On-the-fly spelling checker." t)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)

(autoload 'coq-mode "coq" "Major mode for editing Coq vernacular." t)
(autoload 'proofgeneral "proofgeneral" "Generic front-end for proof assistants." t)

;;(global-set-key (kbd "C-c C-j") 'proof-goto-point)
;;(global-set-key (kbd "C-c ,") 'agda2-goal-and-context-and-inferred)

;; proofgeneral customization
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(agda2-include-dirs (quote ("." "/home/willard/Programs/Installed/lib-0.3/src"))))

;; abda setup
(load-file (let ((coding-system-for-read 'utf-8))
                (shell-command-to-string "agda-mode locate")))

Currently, I have switched from gnome-terminal to using xterm, and am doing alright so far. However, if I would be able to use gnome-terminal instead without trouble, I would be much happier. Any suggestions on how to do this are much appreciated.


rweaver 06-25-2010 01:31 PM

Try switching to TERM=linux in gnome-terminal and see if things work correctly (modify the other relevent bits).

Willard 06-26-2010 02:20 AM


Originally Posted by rweaver (Post 4015020)
Try switching to TERM=linux in gnome-terminal and see if things work correctly (modify the other relevent bits).

Setting TERM=linux changed nothing; emacs in gnome-terminal will still interpret "C-c C-RET" as "C-c C-j", and similar badness for ".", ",", "SPC", and other characters.

vmurali 09-13-2011 04:18 PM

I am in a similar boat (a vim user, forced to use emacs because of Proof General). I mostly ssh (using putty) to my workstation from a windows laptop, which doesn't run the entire X server stack. I was wondering if you found a solution to make gnome-terminal (and putty, which is more relevant for me) send the right keys? I read somewhere that C-j is "return" for emacs and so it seems to me that the terminals are simply omitting the Ctrl key.

All times are GMT -5. The time now is 03:55 PM.