How to fix “delete” and other keys (3~ 4~ 5~, etc) when using Emacs in a Putty session

This has literally been the bane of my existence for several years now. Remote SSH is great, but sometimes the way the interaction between your terminal, shell, and program can drive you mad.

When I log into my Ubuntu-based servers from Putty and fire up Emacs for editing, everything works flawlessly until I mistakenly hit the delete key. Whereupon I get a little gem like this:

Man, I need to delete the last word of this 3~3~3~3~3~3~sentancesersf

Which isn’t very great at all. Same result for Page Up, Page Down, Home, End, and ALL the F1-F12 keys.

After scouring the internets, I found lots of helpful but ultimately useless suggestions. The closest I got was to try and get either the terminal or emacs to tell me which key it thought I was pressing, and forcefully remap that key in my .emacs to what I wanted it to do:

http://www.emacswiki.org/emacs/PuTTY

;; PuTTY fix. Ugly. Bad. But it works. (Good) (it doesn't, actually. not good.) ;; (define-key global-map "M-[1~" 'beginning-of-line) ;; (define-key global-map [select] 'end-of-line)

Lots of variations on that theme brought me some interesting insights, such as

Ctrl-v

shows you which keycode the terminal thinks you pressed:

david@latitude:~$ ^[[3~

And emacs will helpfully tell you the same by using

Ctrl-h l

to tell you the last key you pressed:

-UU-:----F1 *scratch* All L5 (Lisp Interaction)----4:03PM 0.11------------ ESC [ > 0 ; 1 3 6 ; 0 c ESC [ 3 ~ C-h l -UU-:%%--F1 *Help* All L1 (Help View)----4:03PM 0.11----------------------- Type C-x 1 to delete the help window.

So, emacs and bash think I'm typing

ESC [ 3 ~

. Theoretically, I should be able to remap that somehow, right? Well...no.

Emacs seemed to choke on every variation I tried to get that in there, including:

;;(define-key global-map [M-[3~] 'forward-delete-char) ;;(define-key global-map [M-[3~] 'forward-delete-char) ;;(define-key global-map [?M-[3~] 'forward-delete-char) ;;(define-key global-map [e[3~] 'forward-delete-char) ;;(define-key global-map [ESC[3~] 'forward-delete-char) ;;(define-key global-map [^[[3~] 'forward-delete-char) error: Key sequence M-[ 1 ~ starts with non-prefix key M-[

Thanks emacs. Thanks internet.

WHY doesn't this work!?
http://www.cs.utah.edu/dept/old/texinfo/emacs18/emacs_32.html

Rebind the key C-x l to run the function make-symbolic-link. (global-set-key "C-xl" 'make-symbolic-link) or (define-key global-map "C-xl" 'make-symbolic-link)

http://www.linux-france.org/article/appli/emacs/manuel/html/key-bindings.html

You can mix function keys and characters in a key sequence. This example binds C-x NEXT to the command forward-page. (global-set-key [?C-x next] 'forward-page)

NO, YOU CAN’T!!!

This was incredibly frustrating, because http://www.emacswiki.org/emacs/PuTTY#toc10 seemed to have a gigantic table (“normal” column) that DIRECTLY addressed my problem!

Hey look, there are other people with this problem:
http://gnu.emacs.help.narkive.com/6dl5FQYb/function-keys-in-putty-and-emacs

BARGHHH!!!!!
https://skatgame.net/mburo/courses/350/emacs-file

(defvar cursor-map-2 (make-keymap) "for ESC-[") (fset 'Cursor-Map-2 cursor-map-2) (define-key esc-map "[" 'Cursor-Map-2)

!?

Fine. Whatever. That seemed to magically fix it somehow. *SIGH* I hate “magic” fixen.

Remote: emacs 23.1 bash 4.1 Ubuntu 10.10 Maverick Local: Windows 8 Putty .62

(Yes, I realize that my Ubuntu release is as old as the hills. I don’t care. I hate Unity and that release works just fine.)

This entry was posted in troubleshooting and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>