3

After running cat -t, I tested the following cases:

  • ctrl + tab added only a tab space

  • ctrl + / added ^_

  • ctrl + ` added ^@

  • ctrl + shift + backspace deleted to the left (same behaviour as backspace).

On the other hand, the following key bindings remain consistent:

  • ctrl + n adds ^N

  • ctrl + y adds ^Y

  • All alphanumeric characters [a-zA-Z0-9] add the corresponding alphanumeric character

Is there a way to remap the key combinations to a preferred output? For example, in my first four examples above, I would like to remap them to the following:

  • ctrl + tab to <C-tab> (instead of TAB)

  • ctrl + / to ^/ (instead of ^_)

  • ctrl + ` to ^` (instead of ^@)

  • ctrl + shift + backspace to <C-S-backspace> (instead of DEL)

Background (TLDR)

I would like to run emacs -nw when editing on a remote machine, but I notice that several key combinations are not inconsistently detected between running emacs and emacs -nw. Here are a few of the inconsistent key mappings, which I have verified using C-h k <key-binding> in Emacs:

  • ctrl + tab is detected as <C-tab> in emacs, but detected as TAB in emacs -nw.

  • ctrl + / is detected as C-/ in emacs, but detected as C-_ in emacs -nw.

  • ctrl + ` is detected as C-` in emacs, but detected as C-@ in emacs -nw.

  • ctrl + shift + backspace is detected as <C-S-backspace> in emacs, but detected as DEL in emacs -nw.

Any ideas about how to send the proper key combinations to my window-less emacs? All of the inconsistently detected key combinations involve the use of ctrl, but not all key bindings with control are inconsistently detected. For example, C-n is consistently detected.

I am using Ubuntu 14.04, but this problem persists with Debian Wheezy as well. For terminal info, I have the following on all my servers and local machines:

[lucas@server]/home/lucas/bin$ echo $TERM
screen

[lucas@server]/home/lucas/bin$ echo $SHELL
/bin/bash

I usually run my sessions in tmux, but this problem persists regardless of whether I am in a tmux session.

Kenster
  • 23,465
  • 21
  • 80
  • 106
modulitos
  • 14,737
  • 16
  • 67
  • 110
  • What terminal do you use? It can translate the keys, too. – choroba Jun 10 '14 at 08:21
  • I use the default terminal on my Debian and Ubuntu machines, which is Bash (I added this info above). I also like to run everything through the `tmux` multiplexer. Let me know if you need more info. Any suggestions on where to start using the terminal to translate the keys? – modulitos Jun 10 '14 at 09:06
  • 3
    Emacs can handle whatever it receives, but most terminal emulators are heavily restricted in what they can send (faithfully emulating the limitations of a specific terminal). So it's not that Emacs detects the wrong key sequence; the problem is that the terminal never sent the desired sequence in the first place. FWIW [Thomas Dickey's XTerm](http://invisible-island.net/xterm/xterm.html) is the most capable terminal I'm aware of, when it comes to sending modified key sequences. – phils Jun 10 '14 at 11:08
  • 1
    Running `cat -t` in your terminal and then trying various key sequences will likely be illuminating. (Prefixing a given key with `C-v` may also be necessary. Perhaps there's a simpler way to get this effect for everything typed?) – phils Jun 10 '14 at 11:17
  • 2
    There are some links to similar questions (though for other keys) in [this answer](http://stackoverflow.com/a/11118511/113848). – legoscia Jun 10 '14 at 11:22
  • @phils thanks for the help. So, I can close this question and re-open a new question specific to Ubuntu terminal. Would you recommend installing xterm? – modulitos Jun 10 '14 at 11:37
  • I think just edit this question, so that it's Ubuntu-specific (no need to create another one). I don't know if there'll be a solution, mind, but it's useful to have the targeted question on the site -- there are plenty of Ubuntu users out there. – phils Jun 10 '14 at 11:39
  • 1
    I'd say give XTerm a try, for sure; but configuration may be the problem. Configuring it so that terminal Emacs and GUI Emacs are entirely equivalent is something that's languished on my to-do list for a long time, so I don't have a handy pointer for that. (I believe it's possible, but last time I looked I couldn't find a configuration that worked for me, and I've never done the necessary work to figure it out. http://www.emacswiki.org/emacs/XtermExtras always sounded extremely promising, and presumably worked for the author (so YMMV), but breaks lots of bindings for me, unfortunately.) – phils Jun 10 '14 at 11:59

1 Answers1

2

I suspect your encountering issues at more than just one level. This means that you need to tweak configurations at different points/levels.

One of the first things I need to do when setting up a new machine is go through and remove or change key bindings all over the place. The main reason is because I make extensive use of emacs and these days, lots of things 'steal' they key strokes before they get delivered to emacs.

Note that I rarely run emacs in the terminal. I almost exclusively run emacs in its own window and have my environment variables, such as EDITOR etc set to emacsclient. This means I can open an emacs frame faster than opening a terminal and running emacs. Currently, I'm running Ubuntu Gnome , but am seriously considering switching back to either Sawfish or Stumpwm.

If you are going to run emacs within a terminal under gnome, there are three 'layers' you need to look at with respect to key bindings.

layer 1: The session (gnome) layer. Many 'standard' emacs key bindings can be stolen by the wm. I usually go into the keyboard settings option and either disable shortcuts which conflict with bindings I want to use in emacs or re-bind them to another key.

layer 2: The terminal emulator. This can vary a bit depending on the terminal you are using. In general, things are a little more restricted at this level due to the way terminal emulators translate the key press events and send them to the application. The terminal can also steal some key events. There is also the problem that some terminal emulators will comply more with the traditional 'console' interface than others, in which case, some key combinations possible at the xsession layer are just not possible at the terminal layer. For the gnome terminal, you need to look at the settings under 'edit->keyborad shortcuts' and and the compatibility section under 'edit->profile preferences'.

layer 3: In some cases, you may need to tweak the key translation table in emacs so that it understands the key translations sent by the terminal.

Is there a particular reason you want to run emacs within a terminal rather than as a native X11 client? There are sometimes reasons to do this (such as collaboration with remote co-worker using tmux). However, if there is no strong case to run under the terminal, I would strongly recommend running in GUI mode. This will eliminate at least 1 layer. I also find that face settings seem much more consistent when running native than when running under a terminal and you avoid some of the problems you can experience with emacs in a terminal and running external interfacing features, such as with ssh, tramp, etc.

Tim X
  • 4,158
  • 1
  • 20
  • 26