2

Using PuTTY to connect to Linux from Windows and running Emacs: How do I bind CTRL-<home> and CTRL-<end> to beginning-of-buffer and end-of-buffer respectively?

I've been searching the web for a while on this one. I've seen various suggestions but none of them seem to work. I understand that PuTTY can be configured to send different character codes for the HOME and END keys based on a setting and I can see what those codes are in the bash shell (via C-v ) and in Emacs (via C-q). I've read that I might need to "bind" those codes someone in my .bash_profile file and/or I then might need map those codes via a keymap in my .emacs file.

Ultimately I want to be able to add something like this to my .emacs file ...

(global-set-key (kbd "C-<home>") 'beginning-of-buffer)
(global-set-key (kbd "C-<end>") 'end-of-buffer)

... and make CTRL-<home> and CTRL-<end> behave as they do in most native Windows text editors.

Has anybody been able to make this work? Please share your wisdom.

  • Is this a question about PuTTY or about Emacs? – Dan Mar 20 '15 at 14:22
  • Mr Dan, your comment is very apropos. I was just doing some more testing on this today and I have realized that this is basically a PuTTY issue. It turns out that PuTTY does not send any character codes for CTRL-<home> or CTRL-<end> (or CTRL-<left-arrow>, CTRL-<right-arrow>, CTRL-<up-arrow> or CTRL-<right-arrow> it would appear too). If I use an xterm to connect to my Linux host instead of PuTTY then the codes for those key sequences are sent and I can configure (via .emacs) and use Emacs as I want. – Alan Davies Mar 20 '15 at 14:25
  • I was wrong about CTRL with the arrow keys. PuTTY does send control codes for those. Naked <left-arrow> sends ^[[D CTRL-<left-arrow> sends ^[OD – Alan Davies Mar 20 '15 at 14:48
1

Although this question being about Putty makes the actual issue off topic, the question is applicable (since troubleshooting is required to identify the cause).

As a possible solution with Putty:

set PuTTY > Terminal > Features > Disable Application cursor keys mode to checked

Source Using ctrl-arrow keys with putty and screen

I cannot guarantee that it will resolve all the issues but it appeared to be enough to allow CTRL+arrow to send the correct escape codes.

1

It turns out that this is a PuTTY issue as opposed to an Emacs issue, so I apologize for abusing the Emacs forum. Since the frustration started with trying to configure Emacs though I'm guessing that others may run in to the issue too and so I'll post my own answer to hopefully provide those poor souls with some solace.

It seems that PuTTY does not send any keyboard codes for CTRL combinations with certain keys (including [home] and [end]). So, when using PuTTY the CTRL-[home] that I enter on the keyboard doesn't even get to bash or Emacs and so there's no way to bind that to anything. This is unfortunate.

Other terminal software may do this though. I know now that my xterm (I'm using Cygwin with XFree86 from Windows) does. So when using xterm, and launching character mode Emacs from there (emacs -nw), I can get the behavior I want. I have also realized that I can get that behavior if I use an XServer and launch the X version Emacs from PuTTY (emacs) too. So I don't have to completely abandon PuTTY.

Well, at least I learnt something today. I hope this answer helps someone else one day.

Here's the relevant section of my .emacs file ...

;; Notes:
;; * PuTTY doesn't send control characters for CTRL-<home> or CTRL-<end> so there's
;;   no way to make those key combinations work in character mode Emacs launched from
;;   a PuTTY terminal.  Character mode Emacs will work if launched from an xterm
;;   though and X mode Emacs launched from PuTTY will work too.
;; * Also, PuTTY mis-maps the <end> key to <select>

;; Make CTRL-<home>/<end> work like in Windows.
;; X mode Emacs or character mode Emacs launched from xterm only.
(global-set-key (kbd "C-<home>") 'beginning-of-buffer)
(global-set-key (kbd "C-<end>") 'end-of-buffer)     ;; Works in X, not in PuTTY 
(global-set-key (kbd "C-<select>") 'end-of-buffer)  ;; For PuTTY

;; Make <home>/<end> work like in Windows
(global-set-key (kbd "<home>") 'beginning-of-line)
(global-set-key (kbd "<end>") 'end-of-line)         ;; Works in X, not in PuTTY
(global-set-key (kbd "<select>") 'end-of-line)      ;; For PuTTY
  • It's a border case rather than abuse of the forum, so don't worry too much about it. Also have a look at Problems with keybindings when using terminal to see if there's anything there that helps. – Dan Mar 20 '15 at 14:46
  • As I'd said in my answer: Technically the problem is off-topic, but the question is not since it was showing up in Emacs (which led to determining it was a non-Emacs issue). – Jonathan Leech-Pepin Mar 20 '15 at 14:50
  • Thanks Dan. That question (and others that it links too) further reinforce what I've learnt today. – Alan Davies Mar 20 '15 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.