Subject: Re: bug#78508: Tramp confused by bracketed paste after remote prompt

From: Michael Albinus <[email-masked]>
Date: Wed, 21 May 2025 09:35:10 +0200

Daniel Colascione <[email-masked]> writes:

Hi Daniel,

> When the remote shell emits bracketed paste escape codes (as zsh does
> even when TERM=dumb), tramp ends up timing out unable to find the shell
> prompt on initial connection.

Have you checked the recipe from the Tramp manual in (info "(tramp)
Frequently Asked Questions") ?

--8<---------------cut here---------------start------------->8---
          When using zsh on remote hosts, disable zsh line editor
          because zsh uses left-hand side and right-hand side prompts in
          parallel.  Add the following line to ‘~/.zshrc’:

               [[ $TERM == "dumb" ]] && unsetopt zle && PS1='$ ' && return

          This uses the default value of ‘tramp-terminal-type’, "dumb",
          as value of the ‘TERM’ environment variable.  If you want to
          use another value for ‘TERM’, change ‘tramp-terminal-type’ and
          this line accordingly.
--8<---------------cut here---------------end--------------->8---

Note, that until recently TERM wasn't set properly initially, when
calling ssh. I have a WIP patch for this (being under regression test
for some days), see appended. Does this help?

> BTW: is there any particular reason that sshx uses RemoteCommand but ssh
> method does not?

Yes: They are different methods, and they are designed for this
difference. For example, using RemoteCommand disables direct async
processes for sshx connections.

Best regards, Michael.

diff --git a/lisp/tramp-sh.el b/lisp/tramp-sh.el
index 64ad3d49..648b2e13 100644
--- a/lisp/tramp-sh.el
+++ b/lisp/tramp-sh.el
@@ -190,7 +190,10 @@ The string is used in `tramp-methods'.")
               `("scp"
                 (tramp-login-program        "ssh")
                 (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
-				             ("-e" "none") ("%h")))
+					     ("-e" "none")
+				             ("-o" ,(format "SetEnv=\"TERM=%s\""
+							    tramp-terminal-type))
+					     ("%h")))
                 (tramp-async-args           (("-q")))
 		(tramp-direct-async         ("-t" "-t"))
                 (tramp-remote-shell         ,tramp-default-remote-shell)
@@ -208,6 +211,8 @@ The string is used in `tramp-methods'.")
                 (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
 				             ("-e" "none") ("-t" "-t")
 					     ("-o" "RemoteCommand=\"%l\"")
+				             ("-o" ,(format "SetEnv=\"TERM=%s\""
+							    tramp-terminal-type))
 					     ("%h")))
                 (tramp-async-args           (("-q")))
                 (tramp-remote-shell         ,tramp-default-remote-shell)
@@ -223,7 +228,10 @@ The string is used in `tramp-methods'.")
               `("rsync"
                 (tramp-login-program        "ssh")
                 (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
-				             ("-e" "none") ("%h")))
+				             ("-e" "none")
+				             ("-o" ,(format "SetEnv=\"TERM=%s\""
+							    tramp-terminal-type))
+					     ("%h")))
                 (tramp-async-args           (("-q")))
                 (tramp-direct-async         t)
                 (tramp-remote-shell         ,tramp-default-remote-shell)
@@ -254,7 +262,10 @@ The string is used in `tramp-methods'.")
               `("ssh"
                 (tramp-login-program        "ssh")
                 (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
-				             ("-e" "none") ("%h")))
+				             ("-e" "none")
+				             ("-o" ,(format "SetEnv=\"TERM=%s\""
+							    tramp-terminal-type))
+					     ("%h")))
                 (tramp-async-args           (("-q")))
 		(tramp-direct-async         ("-t" "-t"))
                 (tramp-remote-shell         ,tramp-default-remote-shell)
@@ -265,6 +276,8 @@ The string is used in `tramp-methods'.")
                 (tramp-login-program        "ssh")
                 (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
 				             ("-e" "none") ("-t" "-t")
+				             ("-o" ,(format "SetEnv=\"TERM=%s\""
+							    tramp-terminal-type))
 					     ("-o" "RemoteCommand=\"%l\"")
 					     ("%h")))
                 (tramp-async-args           (("-q")))
@@ -301,6 +314,7 @@ The string is used in `tramp-methods'.")
                 ;; remote host echoes the command.
 		;; The "-p" argument doesn't work reliably, see Bug#50594.
                 (tramp-login-args           (("SUDO_PROMPT=P\"\"a\"\"s\"\"s\"\"w\"\"o\"\"r\"\"d\"\":")
+                                             (,(format "TERM=%s" tramp-terminal-type))
                                              ("sudo") ("-u" "%u") ("-s") ("-H")
 				             ("%l")))
                 (tramp-remote-shell         ,tramp-default-remote-shell)
@@ -4271,7 +4285,7 @@ file exists and nonzero exit status otherwise."
     (tramp-send-command
      vec (format
 	  (concat
-	   "exec env TERM='%s' INSIDE_EMACS='%s' "
+	   "env; exec env TERM='%s' INSIDE_EMACS='%s' "
 	   "ENV=%s %s PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s -i")
           tramp-terminal-type (tramp-inside-emacs)
           (or (getenv-internal "ENV" tramp-remote-process-environment) "")