keyboard shortcuts - Emacs key for Home & End

06
2014-04
  • n0t

    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.

  • Answers
  • JW.

    I think you are more advanced with emacs than I am. But, I don't use the Home and End keys at all.

    I just use:

    • Ctrl+a to bring the cursor to the home of the line.
    • Ctrl+e to bring the cursor to the end of the line.

    • Alt+SHIFT+, (C-<) to bring the cursor to the home of the file/buffer.

    • Alt+SHIFT+. (C->) to bring the cursor to the end of the file/buffer.

    I learned these in the default emacs tutorial.

    I suppose I only found those keys practical once I had, first, mastered touch typing.


  • Related Question

    I have started using emacs with -nw option - ctrl-home and ctrl-end don't work
  • codeitagile

    In order to speed up my emacs work flow, I am trying out emacs -nw versus running in X-Windows mode.

    Ctrl-HOME and Ctrl-END are not working at all, and have tried editing .keymap files and following online advice has led to no joy unfortunately. E.g.

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

    I usually enable cua-mode for shift-selection and this also fails when shift-paging up/down e.g.

    It would be great to get all this working in -nw mode and I would welcome advice from fellow emacs users.

    • Install = Linux Mint 9
    • Shell = zsh

  • Related Answers
  • Gilles

    Unfortunately different terminals send different escape sequences for keys like Ctrl+Home, and Emacs doesn't know about all of them. You can declare additional escape sequences through function-key-map (or local-function-key-map under Emacs 23). This tells Emacs that these escape sequences are really an encoding for a single function key.

    To find out what escape sequence a key sends, press C-q followed by the key: this will insert the escape character that begins the key sequence literally, followed by the other characters that make up the key sequence. For example, one terminal that I use sends the four characters ^[, O, 5, H when I press Ctrl+Home, so I would include the following line in my .emacs:

    (define-key function-key-map "\e05H" [C-home])
    

    \e is the most readable way of writing the escape character. Do the same for other keys you want to support.

    If the keys aren't supported out of the box by the latest version of Emacs, it would be nice to tell the developers about the key sequences that your terminal sends.

  • jon

    I almost always run emacs with -nw. In order to scroll to the end of the file, I use M-S-, and M-S-. to get back to the start (maybe easier to remember as Alt-< and Alt->). Not quite as convenient as C-Home and C-End perhaps, but I prefer to work from the terminal (actually from within a tmux session, for what it's worth).

  • petergozz

    Do yourself a favour and do the tutorial :)

    emacs by default uses classic line editor bindings.

    Ctrl a  << to start of line
    Ctrl e  >> to end of line
    Ctrl k  cut to end of line
    Ctrl y  yank that back
    alt y   roll back one more yank (repeat) try it out to get it.
    

    They are the ones I use the most and these also work with readline (most shells)

    Google or dig around on your system for cheat sheets.

    e.g.

     /usr/share/emacs/23.4/etc/refcards/refcard.pdf