Mac – Emacs key for Home & End

emacskeyboard shortcutsurxvtzsh

I'm running Emacs in urxvt with zsh as shell:

~ % echo $TERM && echo $SHELL
rxvt-256color
/bin/zsh
~ %

My current problem is that I can't get Home key & End key working properly in Emacs.

Here is my .Xresources sample for keysbind:

 URxvt.keysym.Home:            \033[1~
 URxvt.keysym.End:             \033[4~

 URxvt.keysym.C-Up:            \033[1;5A
 URxvt.keysym.C-Down:          \033[1;5B
 URxvt.keysym.C-Right:         \033[1;5C
 URxvt.keysym.C-Left:          \033[1;5D

 URxvt.keysym.Meta-Up:         \033[1;3A
 URxvt.keysym.Meta-Down:           \033[1;3B
 URxvt.keysym.Meta-Right:          \033[1;3C
 URxvt.keysym.Meta-Left:           \033[1;3D

 URxvt.keysym.S-Up:            \033[1;2A
 URxvt.keysym.S-Down:          \033[1;2B
 URxvt.keysym.S-Right:         \033[1;2C
 URxvt.keysym.S-Left:          \033[1;2D

I could setup Ctrl+arrows and Shift+arrows thanks to this rxvt.el file found on the web (thanks to its author).

~ % cat ~/.emacs.d/rxvt.el
[...]
(define-key function-key-map "\033[1;5A" [(control up)])
(define-key function-key-map "\033[1;5B" [(control down)])
(define-key function-key-map "\033[1;5D" [(control left)])
(define-key function-key-map "\033[1;5C" [(control right)])
(define-key function-key-map "\033[1;2A" [(shift up)])
(define-key function-key-map "\033[1;2B" [(shift down)])
(define-key function-key-map "\033[1;2D" [(shift left)])
(define-key function-key-map "\033[1;2C" [(shift right)])
[...]

Here are the lines concerning Home & End:

~ % cat ~/.emacs.d/rxvt.el
[...]
(define-key function-key-map "\033[1~" [home])
(define-key function-key-map "\033[4~" [end])                                 
(define-key function-key-map "\033[7~" [find])
(define-key function-key-map "\033[2~" [insert])
(define-key function-key-map "\033[8~" [select]) 
(define-key function-key-map "\033[5~" [prior])
(define-key function-key-map "\033[6~" [next])
[...]

And I added these lines into my .emacs, file but without any effect:

~ % cat ~/.emacs
 [...]
 ;;default configuration for home & end key
 (global-set-key [home] 'beginning-of-line)
 (global-set-key [end] 'end-of-line)

Here is what the sudo showkey command tells me about Home & End:

 <user>@localhost ~ % sudo showkey
 [sudo] password for <user>: 
 kb mode was ?UNKNOWN?
 [ if you are trying this under X, it might not work
 since the X server is also reading /dev/console ]

 press any key (program terminates 10s after last keypress)...
 keycode  28 release
 ^[[1~keycode 102 press     //home key was pressed
 keycode 102 release
 ^[[4~keycode 107 press     //end key was pressed
 keycode 107 release
 keycode  29 press
 ^Ccaught signal 2, cleaning up...

Does anyone have any advice?

Also maybe it helps if I say that Ctrl+A works and has the same behavior that I want for Home and Ctrl+E for End.

Best Answer

These worked for me:

(global-set-key (kbd "<home>") 'beginning-of-line)
(global-set-key (kbd "<end>") 'end-of-line)
Related Question