Restoring sensible default HISTFILE for shell-mode

Joel Reicher <[email protected]> Mon, 20 Oct 2025 17:10:31 +1100
Newsgroups gmane.emacs.tramp
Message-ID <[email protected]>
I am not entirely sure yet if the below is a problem with my 
understanding, with tramp, or with my expectations of tramp, so 
I'm going to go through every step.

Firstly I am only talking about the default behaviour (no 
customisation).

Secondly I think the default behaviour is sensible for what I'll 
call "tramp shells"; i.e. shells that the user does not have the 
opportunity to interact with, but rather are used by tramp to do 
tests and start up remote processes. By default the history of 
these is "diverted" to .tramp_history and IMO that both works fine 
and is sensible.

However, when the remote process is a shell that is a subprocess 
of shell-mode/comint, the intention is for the user to now be 
issuing shell commands, and I do not believe that history should 
end up in the tramp history. And currently it seems to, due to 
tramp setting HISTFILE in a parent environment and that being 
inherited all the way down to a shell that is not in fact used by 
tramp.

Furthermore I think it makes sense for shell-mode to read the 
non-tramp history on startup, and also for this non-tramp history 
to be the same as the shell history used by a shell started 
outside Emacs, so that everything works together nicely by 
default.

In the case of the explicit shell being bash, specifically, I am 
able to get the behaviour that I think makes sense with the 
following, but it seems like something easier should be possible.

`shell-history-file-name' set to "~/.bash_history"

init_bash.sh containing:

if [ "${INSIDE_EMACS/*tramp*/tramp}" == "tramp" ] ; then 
    export HISTFILE=~/.bash_history history -cr $HISTFILE 
fi 


This causes shell-mode to load its history from the "true 
interactive" (non-tramp) history, re-initialises the 
remote-shell-processes history from the same so that builtin 
history commands and the like work, and also sets the shell to the 
non-tramp file so that it saves this "human" history separately on 
exit.

But it feels like there should be an easier way of doing 
this. Something like unsetting HISTFILE immediately before 
starting the remote shell process (but after everything else that 
tramp has done, that should still go to .tramp_history)

Thoughts/corrections?

Thanks and regards,

       - Joel