Skip to content

urxvt and Putty Home/End don't work in Mosh #178

Open

Description

@kmcallister

urxvt (aka rxvt-unicode) sends CSI7~ and CSI8~ for Home, End respectively.

With TERM=rxvt-unicode, bash will interpret these correctly and jump to the beginning/end of the line.

Screen sets TERM=screen and translates them to CSI1~ and CSI4~, which bash also understands.

But Mosh sets TERM=xterm and does no translation, so bash gets confused.

If I tell urxvt (by X resource) to send CSI1~ then my local bash is confused. If I tell it to use CSIH and CSIF, the Xterm codes, then most things work, but Barnowl doesn't recognize these codes (which could be a Barnowl bug).

I don't know if this is related to #161.

Activity

  1. keithw commented on Apr 17, 2012

    @keithw
    Member

    The correct way to solve this may be to place a full UTF8Parser state machine at the client, interpret all the CSIs, and translate "khome" and "kend" (and all the other "k" capabilities) from the user's terminal to xterm sequences.

    This seems both like a pain and likely to introduce unpleasantness side effects, but there may be no way around it...

  2. kmcallister commented on May 8, 2012

    @kmcallister
    ContributorAuthor

    If we do client-side parsing for #101, maybe we can do this too.

  3. keithw commented on Dec 7, 2012

    @keithw
    Member

    I thought about this a little more, and I haven't figured out a way to do it that doesn't have the client start depending on the timing of input bytes, which I hate to do.

    If we want to capture Putty's ESC [ 1 ~ (khome) and turn it into xterm's ESC O H (khome), we basically have to eat ESC [ and then observe the rest of the escape sequence before printing O H. But I'm not comfortable just eating ESC [ and sitting there indefinitely without printing the [ . On the other hand, I hate the idea of a timeout.

    On the third hand, maybe the least bad path is a timeout -- but only when the outer terminal requires this kind of translation (so not for TERM=xterm).

  4. josch commented on Jan 29, 2013

    @josch

    until this is fixed, starting mosh inside gnu screen seems to be a workaround (I want to drop neither urxvt nor mosh)

  5. hexchain commented on Dec 22, 2013

    @hexchain

    I had this problem too. Manully exporting TERM=rxvt-unicode-256color on server after connecting makes Home/End keys behave well in applications, but unfortunately not that shell.

    Will there be any problem if mosh just passes the client $TERM to server?

  6. andersk commented on Jul 28, 2014

    @andersk
    Member

    Passing TERM to the server is not the right approach; see my comment #446 (comment).

    This needs to be done via translation on the client side in order for the client and server to agree about the state of the terminal at all times. Probably the most clever approach would be to read the client’s terminfo entry and learn the translation automatically from that (like ncurses does).

  7. keithw commented on Dec 17, 2014

    @keithw
    Member

    I don't think this issue (about rxvt cursor-key sequences sent from the terminal to the application) is related to colors (sequences sent from the application to the terminal). Your comment is probably better on issue #557 .

  8. awwaiid commented on Feb 4, 2015

    @awwaiid

    All I know is that when I edit src/frontend/mosh-server.cc and s/xterm-256color/rxvt-unicode-256color/ all my home/end issues go away and I don't notice any further issues :) . I guess I need to get the remote system to understand xterm-256color as the "right" solution.

  9. chandlerc commented on Dec 16, 2015

    @chandlerc

    Just hit this issue.

    I connect with mosh using both xterm and rxvt-unicode and the different escape codes completely hose things. I think that this really needs some solution. I would be happy with either parsing the escape codes or causing the server to be run with the correct TERM...

  10. txtsd commented on Jan 5, 2016

    @txtsd

    mosh incorrectly sets $TERM as xterm-256color while ssh correctly sets it as rxvt-unicode-256color
    I have my .zshrc set up with escape sequence hackery to get around this, but it shouldn't be necessary at all.

    EDIT: I thought I did. It doesn't work.

  11. Secretions commented on Mar 29, 2016

    @Secretions

    The workaround I'm using for the time being in my .zshrc is checking if TERM is set to xterm*, changing it to rxvt, then doing exec zsh. The exec zsh replaces the current shell with a new instance so the shell itself inherits the TERM variable.


    case $TERM in
    xterm*)
    export TERM=rxvt
    exec zsh
    ;;
    rxvt-unicode-256color*)
    export TERM=rxvt
    ;;
    esac

    If you try to do something similar, it's absolutely critical that the exec can't get executed on a subsequent run of zsh, or your ability to login via that shell will be completely hosed. Just looking at it causes me anxiety. :)

  12. grawity commented on Mar 29, 2016

    @grawity

    @Secretions: So why use evil hacks like this at all, rather than setting up the needed keybindings directly? For example:

    bindkey '\e[7~' beginning-of-line
    
  13. Secretions commented on Mar 29, 2016

    @Secretions

    Because, like txtsd, I attempted to setup the keybindings and failed. :)

    Looks like the two that are needed are:

    bindkey '\e[7~' beginning-of-line
    bindkey '\e[8~' end-of-line

    Much less insane than what I was doing above. In my first attempt to set the keybindings, I had it in my head to use "^[[1~" and "^[[4~", which was not correct. Thanks.

  14. 6 remaining items

  15. hhirsch commented on Aug 10, 2016

    @hhirsch

    I have the same issue. It is a dealbreaker for me. I am back to using ssh.

  16. josch commented on Aug 10, 2016

    @josch

    @hhirsch in case it helps: I am using mosh like this:

    screen mosh me@myserver tmux attach
    

    And my ~/.screenrc has:

    escape ""
    startup_message off
    

    Which is okay since I normally use tmux for everything. I did not edit any other configuration files.

    With this setup I have not yet encountered any issues. I'm using mcabber and irssi inside that tmux session on myserver where I frequently use keys like Home/End and PageUp/PageDown.

  17. hhirsch commented on Aug 10, 2016

    @hhirsch

    @josch Well to me this seems like too much trouble to make a square wheel turn.
    ssh does not have bugs like this so I just stick to it. If basic stuff like this is broken who knows what else is. I'll try again in 10 years when this issue is fixed.

  18. tsmithe commented on Sep 16, 2016

    @tsmithe

    I must also comment that this certainly does not work for me, even using the `screen' trick described above. In particular, I encounter #161. Why does mosh have to emulate a terminal? Why can't it just pass back and forth the byte sequences produced by and for the user's actual terminal?

  19. andersk commented on Sep 16, 2016

    @andersk
    Member

    @tsmithe Mosh relies on a deep understanding of the terminal input and display for its latency reduction tricks. (“But I have a fast network and I don’t need those!” Great, go use SSH then. “But I want Mosh’s automatic roaming!” Well, I sympathize—there’s a place in the world for a version of SSH with just the addition of automatic roaming. But Mosh is not that and cannot reasonably be made into that.)

    The screen-in-urxvt workaround does fix the Home/End keys as advertised. It does not help with Ctrl-Arrow keys, but I think that is a screen bug: Ctrl-Arrow keys do not work in screen-in-urxvt even when Mosh is not involved.

  20. tsmithe commented on Sep 16, 2016

    @tsmithe

    Interesting; I did not expect that mosh's features depended on such hidden complexity! Thanks for your reply.

    I did respect the order for the workaround commands, but with no luck. I will have to investigate more deeply. Should you have any debugging suggestions, I would be glad to hear them, but I suspect I have not provided sufficient detail for that (simply due to ignorance..).

  21. tsmithe commented on Sep 17, 2016

    @tsmithe

    EDIT: I may have spoken too soon, but I will return later when I actually get things working fully..

    Both fortunately and unfortunately, the best solution I seem to have found for my configuration is to use the urxvt keysym mappings given at http://www.netswarm.net/misc/urxvt-xtermcompat.txt (to simulate an xterm), in conjunction with a local tmux. If I run mosh within that urxvt+xtermcompat+tmux configuration, then I get the expected behaviour remotely: that is, sequences such as Ctrl-Arrow and Home/End work just fine.

    Although I can speculate, I am not completely sure why this is -- and why this works whilst other suggestions do not -- but I hope this might be helpful to others.

  22. anarcat commented on Sep 18, 2016

    @anarcat

    @tsmithe that does look like a nasty piece of hack. i wonder what the urxvt people think of that one. ;) does it also work with screen?

  23. tsmithe commented on Sep 18, 2016

    @tsmithe

    @anarcat, you are right, it is a nasty hack..

    I have found that, with the following .emacs snippet, the xtermcompat hack is unnecessary if I use the screen workaround described above (again, I use tmux for everything, so that's fine). But without this .emacs snippet, I do not get the correct remote behaviour either with the xtermcompat hack, or with the screen workaround. Note that, in both cases (xtermcompat hack, and screen workaround), I find this snippet to be necessary to get the correct remote behaviour in emacs. I am not sure why emacs (24.5.1) needs it!

    (global-set-key "\M-[1;5B"    'forward-paragraph)  ; Ctrl+down    => forward paragraph
    (global-set-key "\M-[1;5A"    'backward-paragraph) ; Ctrl+up      => backward paragraph
    (global-set-key "\M-[1;5C"    'forward-word)       ; Ctrl+right   => forward word
    (global-set-key "\M-[1;5D"    'backward-word)      ; Ctrl+left    => backward word
    (define-key key-translation-map (kbd "<select>") (kbd "<end>"))
    

    I do not know what the 'select' key is. Running 'read' at the remote end gives me ^[[4~, which is just what I get locally, where there is no `select' problem!

    I will add further mappings as and when I find or need them. In particular, I still do not get the right behaviour (seemingly in any case so far!) if I have a local tmux session, with a nested remote tmux session in mosh.

    Fundamentally, it just seems like mosh doesn't interpret urxvt I/O correctly, but it does a better job with a client-side tmux session, which in turn does a reasonable job with urxvt... This means that I have a passably working set-up, and can therefore enjoy the many benefits of mosh. These benefits comfortably outweigh the frustrations of these hacks, for now!

    EDIT: The solution (for now..) for the nested remote tmux problem was to turn on the xterm-keys tmux option remotely.

  24. blueyed commented on Sep 18, 2016

    @blueyed

    @tsmithe

    with the following .emacs snippet, the xtermcompat hack is unnecessary if I use the screen workaround described above

    How is this less hackish? :D

  25. Trollwut commented on Jan 29, 2019

    @Trollwut

    Little workaound for me was installing rxvt-unicode (which I'm using on my client too) and then putting export TERM=rxvt-unicode into the ~/.bashrc of the user I'm connecting to.

    Works for me, even su into another account.

    EDIT: before putting that into your bashrc, you can just run echo $TERM to see that it says something xterm. Then running export TERM=rxvt-unicode might fix it. If so, you can put it into your bashrc.

  26. wilhelmy commented on Aug 1, 2019

    @wilhelmy

    I switched back from tmux to screen for a while (not quite knowing which part of the chain of terminal emulators broke this) because screen apparently translates rxvt keys to xterm keys, while tmux does not. Now I'm back to tmux (because it's just so much nicer) and I would really appreciate if there was a fix for this. Setting TERM=rxvt-unicode-256color for now because it seems like a viable workaround.

  27. added this to the mosh-future milestone on Jan 19, 2023
  28. She110ck commented on May 5, 2023

    @She110ck

    If we talking about passing $TERM variable (in my case wrong colors issue) simplest solution just run nested bash session:

    mosh REMOTE -- bash -c "TERM=screen-256color bash"
    

    If you use tmux for everything:

    mosh REMOTE -- bash -c "TERM=screen-256color tmux new-session -A -s somenameforsession"
    
  29. added a commit that references this issue on Sep 4, 2026
    e987847
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions