3

In a regular terminal (e.g. iTerm2 on OS X) I can connect to a database, e.g.

> psql ....

and then, as I type my command in psql, e.g.

$ select foo from bar

I can move around (as I type psql commands) using the standard Alt+b and Alt+f.

However, if I try to do this in a shell within Emacs (ansi-term), it doesn't work.

More specifically, if I start a shell (e.g. Bash) within ansi-term, the keystrokes Alt+b and Alt+f work fine in the Unix shell (in this case, Bash), BUT if I then drop into psql from within the shell, the keystrokes Alt+b nor Alt+f stop working (the keystrokes won't move the cursor, and I can't keep typing commands properly anymore)

Why is this? And how can fix this behavior?

Update 1:

I narrowed the problem and the line in my .zshrc that causes this behavior is the following one:

TERM=xterm-256color

I have this line because its's the best solution that I found to fix the problem that I report in this thread: Emacs multi-term not displaying special characters correctly

Update 2 (solution, but why?):

I found the solution in this thread: Weird character zsh in emacs terminal. As the top answer says, I had to create eterm-color terminfo by using following command: (note that the terminfo path may be different from your system)

# If you use Cocoa Emacs or Carbon Emacs
tic -o ~/.terminfo /Applications/Emacs.app/Contents/Resources/etc/e/eterm-color.ti
 
Amelio Vazquez-Reina
  • 40,169
  • 77
  • 197
  • 294
  • Do you have anything relevant in your `~/.inputrc`? I just tried with `ansi-term`, `mysql` and `bash` (and `zsh`) and it works fine. I don't have access to a psql database, perhaps something specific to its shell? – terdon Jan 29 '14 at 21:49
  • Thanks @terdon, but I don't have an `.inputrc` file in my home directory. Also - I am using Zsh. Actually, I **don't** have this problem in bash! Hmmm – Amelio Vazquez-Reina Jan 29 '14 at 21:55
  • Strange, I tested with both bash and zsh and in both cases it worked fine on a VM with Ubuntu server. I'd never used `zsh` o that machine before, I installed it just to try this out, so everything would be in its default state. Perhaps you are remapping these commands to something in your `~/.zshrc`? – terdon Jan 29 '14 at 22:00
  • Thanks @terdon - What commands would be equivalent to the changes you mentioned in `.inputrc` for Zsh? I am afraid that my `.zshrc` (and Zsh init files) are quite large at this point. – Amelio Vazquez-Reina Jan 29 '14 at 22:06
  • I have absolutely no idea :) I just thought that .inputrc would be a place to check. Try explicitly setting those shortcuts in `.inputrc`. For example, this sets Ctrl+Left to backward-word (same as Alt-B): `"\e[1;5D": backward-word` – terdon Jan 29 '14 at 22:15
  • Your question is not clear: where are you using bash and where are you using zsh? What happens if you tell ansi-term to use `psql` itself as a shell? What happens if you move your `.bashrc`, `.zshrc` and `.inputrc` out of the way? – Gilles 'SO- stop being evil' Jan 30 '14 at 00:06
  • Thanks @Gilles I edited the OP - hopefully the question is clearer now. I will experiment with moving `.zshrc` out of the way next. – Amelio Vazquez-Reina Jan 30 '14 at 01:08
  • @Gilles - I narrowed the problem to this statement `TERM=xterm-256color` in my `.zshrc`. See my update in the OP for the details. – Amelio Vazquez-Reina Jan 30 '14 at 02:51
  • I'd say that `TERM=xterm-256color` is just wrong — you're lying to psql and thus getting what you deserve — but I don't understand why it fixes your other problem. – Gilles 'SO- stop being evil' Jan 30 '14 at 10:10
  • Thanks @Gilles - I have updated the OP with a second update that solved my problem using the answer to another thread. I am not quite sure why it fixes my problem, so I didn't provide it as an answer. – Amelio Vazquez-Reina Jan 30 '14 at 18:12

1 Answers1

2

According to the Emacs wiki, the recommended TERM setting for ansi-term is "eterm-color". That terminal description is provided by ncurses; you probably would have to use the package with the complete terminfo database for platforms which make a distinction, e.g., Debian with ncurses-base and ncurses-term.

The eterm-color description provides 16 colors, which still appears to be the current state of the Emacs code. Aside from that, the description for xterm-256color includes escape sequences which Emacs is unlikely to handle properly. Using infocmp, there are several capabilities which differ and could produce a problem in (a) cursor movement and (b) scrolling. Here are the interesting lines:

    ech: NULL, '\E[%p1%dX'.
    flash: NULL, '\E[?5h$<100/>\E[?5l'.
    hpa: NULL, '\E[%i%p1%dG'.
    hts: NULL, '\EH'.
    indn: NULL, '\E[%p1%dS'
    rin: NULL, '\E[%p1%dT'.
    rmam: NULL, '\E[?7l'.
    rmcup: NULL, '\E[?1049l'.
    rmkx: NULL, '\E[?1l\E>'.
    rmm: NULL, '\E[?1034l'
    sitm: NULL, '\E[3m'.
    smacs: NULL, '\E(0'.
    smam: NULL, '\E[?7h'.
    smcup: NULL, '\E[?1049h'.
    smkx: NULL, '\E[?1h\E='.
    smm: NULL, '\E[?1034h'.
    tbc: NULL, '\E[3g'.
    vpa: NULL, '\E[%i%p1%dd'.

For instance:

  • hpa for instance works in xterm to move the cursor horizontally.
  • smm and rmm control meta mode (which is relevant to the mention of "alt" keys: see the description of meta in terminfo(5)).
  • indn and rin are used for vertical scrolling

When the terminal capabilities and terminal description do not agree, you can see odd things happening. In a quick test of Emacs 24.5, ansi-term implements none of those features; all are likely to be used by full-screen programs, and some (such as hpa) would be used by command-line programs.

Further reading:

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268