aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Albinus2024-06-02 19:30:12 +0200
committerMichael Albinus2024-06-02 19:30:12 +0200
commitbffe73b562f4065bfa99095a46f1bdb731bebc13 (patch)
treeb672a57850df3c20816856328cc89db2d618f1ae
parent2849c0cda3785124465806134b316f95231a67a5 (diff)
downloademacs-bffe73b562f4065bfa99095a46f1bdb731bebc13.tar.gz
emacs-bffe73b562f4065bfa99095a46f1bdb731bebc13.zip
New user option 'shell-history-file-name'
* doc/emacs/misc.texi (Shell Ring): Explain shell-history-file-name. * doc/misc/tramp.texi (Inline methods): Be more specific with containers. (Remote processes): New subsection "Managing remote shell history". Explain shell-history-file-name. (Frequently Asked Questions): Add items to Tramp speedup. Remove entry about history file. * etc/NEWS: New user option 'shell-history-file-name'. * lisp/shell.el (shell-history-file-name): New defcustom. (shell-mode): Use it. (Bug#71049)
-rw-r--r--doc/emacs/misc.texi20
-rw-r--r--doc/misc/tramp.texi120
-rw-r--r--etc/NEWS14
-rw-r--r--lisp/shell.el62
4 files changed, 136 insertions, 80 deletions
diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index 3bee88bca86..68ea030e219 100644
--- a/doc/emacs/misc.texi
+++ b/doc/emacs/misc.texi
@@ -1331,16 +1331,18 @@ buffer, or even killing large parts of it, does not affect the history
1331that these commands access. 1331that these commands access.
1332 1332
1333@vindex comint-input-ring-file-name 1333@vindex comint-input-ring-file-name
1334@vindex shell-history-file-name
1335@cindex @env{HISTFILE} environment variable
1334 Some shells store their command histories in files so that you can 1336 Some shells store their command histories in files so that you can
1335refer to commands from previous shell sessions. Emacs reads 1337refer to commands from previous shell sessions. Emacs reads the command
1336the command history file for your chosen shell, to initialize its own 1338history file for your chosen shell, to initialize its own command
1337command history. The file name is @file{~/.bash_history} for bash, 1339history. The history file name is the string specified in
1338@file{~/.sh_history} for ksh, and @file{~/.history} for other shells. 1340@code{shell-history-file-name}. If that user option is @code{t}, the
1339 1341command history is not read. If the value is @code{nil}, the command
1340@vindex tramp-histfile-override 1342history is read from the file specified in environment variable
1341 If you run the shell on a remote host, this setting might be 1343@env{HISTFILE}, or from @file{~/.bash_history} for bash,
1342overwritten by the variable @code{tramp-histfile-override}. It is 1344@file{~/.sh_history} for ksh, @file{~/.zsh_history"} for zsh, or
1343recommended to set this variable to @code{nil}. 1345@file{~/.history} for other shells.
1344 1346
1345@node Shell History Copying 1347@node Shell History Copying
1346@subsubsection Shell History Copying 1348@subsubsection Shell History Copying
diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi
index 74081767caa..ec8a8d5c3eb 100644
--- a/doc/misc/tramp.texi
+++ b/doc/misc/tramp.texi
@@ -976,7 +976,8 @@ session.
976@end table 976@end table
977 977
978@noindent 978@noindent
979The following methods allow to access containers in different ways: 979The following methods allow to access running containers in different
980ways:
980 981
981@table @asis 982@table @asis
982@item @option{docker} 983@item @option{docker}
@@ -1017,6 +1018,9 @@ The host name may be either a container's name or ID, as returned by
1017the default Toolbox container for the host will be used. There is no 1018the default Toolbox container for the host will be used. There is no
1018such default for Distrobox. 1019such default for Distrobox.
1019 1020
1021Contrary to the other container-based methods, these two methods start
1022a created container, if it isn't running yet.
1023
1020These are optional methods, @pxref{Optional methods}. They do not 1024These are optional methods, @pxref{Optional methods}. They do not
1021support user names. 1025support user names.
1022 1026
@@ -4080,8 +4084,59 @@ host. Furthermore, set @code{tramp-use-connection-share} to
4080unwanted side effects. 4084unwanted side effects.
4081 4085
4082 4086
4087@anchor{Managing remote shell history}
4088@subsection Managing remote shell history
4089@cindex shell history
4090@vindex tramp-histfile-override
4091@vindex HISTFILE@r{, environment variable}
4092@vindex HISTFILESIZE@r{, environment variable}
4093@vindex HISTSIZE@r{, environment variable}
4094
4095Due to the remote shell saving tilde expansions triggered by
4096@value{tramp}, the shell history file is probably growing rapidly.
4097@value{tramp} can suppress this behavior with the user option
4098@code{tramp-histfile-override}. When set to @code{t}, environment
4099variable @env{HISTFILE} is unset, and environment variables
4100@env{HISTFILESIZE} and @env{HISTSIZE} are set to 0. Don't use this
4101with @command{bash} 5.0.0. There is a bug in @command{bash} which
4102lets @command{bash} die.
4103
4104Alternatively, @code{tramp-histfile-override} could be a string.
4105Environment variable @env{HISTFILE} is set to this file name then. Be
4106careful when setting to @file{/dev/null}; this might result in
4107undesired results when using @command{bash} as remote shell.
4108
4109Another approach is to disable @value{tramp}'s handling of the
4110@env{HISTFILE} at all by setting @code{tramp-histfile-override} to
4111@code{nil}. In this case, saving history could be turned off by
4112putting this shell code in @file{.bashrc} or @file{.kshrc}:
4113
4114@example
4115@group
4116if [ -f $HOME/.sh_history ] ; then
4117 /bin/rm $HOME/.sh_history
4118fi
4119if [ "$@{HISTFILE-unset@}" != "unset" ] ; then
4120 unset HISTFILE
4121fi
4122if [ "$@{HISTSIZE-unset@}" != "unset" ] ; then
4123 unset HISTSIZE
4124fi
4125@end group
4126@end example
4127
4128For @option{ssh}-based method, add the following line to your
4129@file{~/.ssh/environment}:
4130
4131@example
4132HISTFILE=/dev/null
4133@end example
4134
4135
4083@subsection Running @code{shell} on a remote host 4136@subsection Running @code{shell} on a remote host
4084@cindex @code{shell} 4137@cindex @code{shell}
4138@vindex explicit-shell-file-name
4139@vindex shell-history-file-name
4085 4140
4086Set @code{explicit-shell-file-name} to the appropriate shell name 4141Set @code{explicit-shell-file-name} to the appropriate shell name
4087when using @value{tramp} between two hosts with different operating 4142when using @value{tramp} between two hosts with different operating
@@ -4126,6 +4181,14 @@ of @code{explicit-shell-file-name} for different remote hosts.
4126@end group 4181@end group
4127@end lisp 4182@end lisp
4128 4183
4184The command @code{shell} reads the remote history file in order to to
4185initialize the history input ring. You can set the user option
4186@code{shell-history-file-name} in order to specify which remote
4187history file is taken, or whether to suppress this at all. It accepts
4188the same values as @code{tramp-histfile-override}, see @pxref{Managing
4189remote shell history}. @code{shell-history-file-name} accepts also
4190connection-local values in @code{shell} buffers.
4191
4129 4192
4130@subsection Running @code{shell-command} on a remote host 4193@subsection Running @code{shell-command} on a remote host
4131@cindex @code{shell-command} 4194@cindex @code{shell-command}
@@ -5171,6 +5234,13 @@ connections, apply the following code.
5171@end lisp 5234@end lisp
5172 5235
5173@item 5236@item
5237Use direct asynchronous processes if possible.
5238
5239@item
5240Suppress reading the remote history file in @code{shell}. Set
5241@code{shell-history-file-name} to @code{t}.
5242
5243@item
5174Disable excessive traces. Set @code{tramp-verbose} to 3 or lower, 5244Disable excessive traces. Set @code{tramp-verbose} to 3 or lower,
5175default being 3. Increase trace levels temporarily when hunting for 5245default being 3. Increase trace levels temporarily when hunting for
5176bugs. 5246bugs.
@@ -5477,54 +5547,6 @@ as follows:
5477 5547
5478 5548
5479@item 5549@item
5480Why is @file{~/.sh_history} on the remote host growing?
5481
5482@vindex tramp-histfile-override
5483@vindex HISTFILE@r{, environment variable}
5484@vindex HISTFILESIZE@r{, environment variable}
5485@vindex HISTSIZE@r{, environment variable}
5486Due to the remote shell saving tilde expansions triggered by
5487@value{tramp}, the history file is probably growing rapidly.
5488@value{tramp} can suppress this behavior with the user option
5489@code{tramp-histfile-override}. When set to @code{t}, environment
5490variable @env{HISTFILE} is unset, and environment variables
5491@env{HISTFILESIZE} and @env{HISTSIZE} are set to 0. Don't use this
5492with @command{bash} 5.0.0. There is a bug in @command{bash} which
5493lets @command{bash} die.
5494
5495Alternatively, @code{tramp-histfile-override} could be a string.
5496Environment variable @env{HISTFILE} is set to this file name then. Be
5497careful when setting to @file{/dev/null}; this might result in
5498undesired results when using @command{bash} as remote shell.
5499
5500Another approach is to disable @value{tramp}'s handling of the
5501@env{HISTFILE} at all by setting @code{tramp-histfile-override} to
5502@code{nil}. In this case, saving history could be turned off by
5503putting this shell code in @file{.bashrc} or @file{.kshrc}:
5504
5505@example
5506@group
5507if [ -f $HOME/.sh_history ] ; then
5508 /bin/rm $HOME/.sh_history
5509fi
5510if [ "$@{HISTFILE-unset@}" != "unset" ] ; then
5511 unset HISTFILE
5512fi
5513if [ "$@{HISTSIZE-unset@}" != "unset" ] ; then
5514 unset HISTSIZE
5515fi
5516@end group
5517@end example
5518
5519For @option{ssh}-based method, add the following line to your
5520@file{~/.ssh/environment}:
5521
5522@example
5523HISTFILE=/dev/null
5524@end example
5525
5526
5527@item
5528Where are remote files trashed to? 5550Where are remote files trashed to?
5529 5551
5530@vindex remote-file-name-inhibit-delete-by-moving-to-trash 5552@vindex remote-file-name-inhibit-delete-by-moving-to-trash
diff --git a/etc/NEWS b/etc/NEWS
index e24c6bf0ddc..53abce8a472 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1047,6 +1047,15 @@ When this user option is non-nil, 'shell-get-old-input' ('C-RET')
1047includes multiple shell "\" continuation lines from command output. 1047includes multiple shell "\" continuation lines from command output.
1048Default is nil. 1048Default is nil.
1049 1049
1050+++
1051*** New user option 'shell-history-file-name'.
1052When this user option is set to t, 'shell-mode' does not read the shell
1053history file. Setting this user option to a string specifies the name
1054of the shell history file to be read. A nil value triggers reading the
1055environment variable 'HISTFILE'.
1056
1057In a 'shell' buffer, this user option is connection-local.
1058
1050** Make mode 1059** Make mode
1051 1060
1052*** The Makefile browser is now obsolete. 1061*** The Makefile browser is now obsolete.
@@ -1182,7 +1191,7 @@ manual "(tramp) Improving performance of asynchronous remote processes".
1182--- 1191---
1183*** Direct asynchronous processes use 'tramp-remote-path'. 1192*** Direct asynchronous processes use 'tramp-remote-path'.
1184When a direct asynchronous process is invoked, it uses 'tramp-remote-path' 1193When a direct asynchronous process is invoked, it uses 'tramp-remote-path'
1185for setting the remote PATH environment variable. 1194for setting the remote 'PATH' environment variable.
1186 1195
1187** File Notifications 1196** File Notifications
1188 1197
@@ -1922,10 +1931,11 @@ grammars. The Info manual "(elisp) Parsing Expression Grammars" has
1922documentation and examples. 1931documentation and examples.
1923 1932
1924** New major mode 'shell-command-mode'. 1933** New major mode 'shell-command-mode'.
1925This mode is used by default for the output of 'async-shell-command'. 1934This mode is used by default for the output of asynchronous 'shell-command'.
1926To revert to the previous behavior, set the (also new) variable 1935To revert to the previous behavior, set the (also new) variable
1927'async-shell-command-mode' to 'shell-mode'. Any hooks or mode-specific 1936'async-shell-command-mode' to 'shell-mode'. Any hooks or mode-specific
1928variables used should be adapted appropriately. 1937variables used should be adapted appropriately.
1938
1929 1939
1930* Incompatible Lisp Changes in Emacs 30.1 1940* Incompatible Lisp Changes in Emacs 30.1
1931 1941
diff --git a/lisp/shell.el b/lisp/shell.el
index 4352811912a..9399906715f 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -419,6 +419,22 @@ Useful for shells like zsh that has this feature."
419 "Shell file name started in `shell'.") 419 "Shell file name started in `shell'.")
420(put 'shell--start-prog 'permanent-local t) 420(put 'shell--start-prog 'permanent-local t)
421 421
422(defcustom shell-history-file-name nil
423 "The history file name used in `shell-mode'.
424When it is a string, this file name will be used.
425When it is nil, the environment variable HISTFILE is used.
426When it is t, no history file name is used in `shell-mode'.
427
428The settings obey whether `shell-mode' is invoked in a remote buffer.
429In that case, HISTFILE is taken from the remote host, and the string is
430interpreted as local file name on the remote host.
431
432If `shell-mode' is invoked in a local buffer, and no history file name
433can be determined, a default according to the shell type is used."
434 :type '(choice (const :tag "Default" nil) (const :tag "Suppress" t) file)
435 :version "30.1")
436(put 'shell-history-file-name 'permanent-local t)
437
422;;; Basic Procedures 438;;; Basic Procedures
423 439
424(defun shell--unquote&requote-argument (qstr &optional upos) 440(defun shell--unquote&requote-argument (qstr &optional upos)
@@ -721,27 +737,33 @@ command."
721 (setq list-buffers-directory (expand-file-name default-directory)) 737 (setq list-buffers-directory (expand-file-name default-directory))
722 ;; shell-dependent assignments. 738 ;; shell-dependent assignments.
723 (when (ring-empty-p comint-input-ring) 739 (when (ring-empty-p comint-input-ring)
724 (let ((remote (file-remote-p default-directory)) 740 (let* ((remote (file-remote-p default-directory))
725 (shell (or shell--start-prog "")) 741 (shell (or shell--start-prog ""))
726 (hsize (getenv "HISTSIZE")) 742 (hfile (cond ((stringp shell-history-file-name)
727 (hfile (getenv "HISTFILE"))) 743 shell-history-file-name)
728 (when remote 744 ((null shell-history-file-name)
729 ;; `shell-snarf-envar' does not work trustworthy. 745 (if remote
730 (setq hsize (shell-command-to-string "echo -n $HISTSIZE") 746 (shell-command-to-string "echo -n $HISTFILE")
731 hfile (shell-command-to-string "echo -n $HISTFILE"))) 747 (getenv "HISTFILE")))))
748 hsize)
732 (and (string-equal hfile "") (setq hfile nil)) 749 (and (string-equal hfile "") (setq hfile nil))
733 (and (stringp hsize) 750 (when (and (not remote) (not hfile))
734 (integerp (setq hsize (string-to-number hsize))) 751 (setq hfile
735 (> hsize 0) 752 (cond ((string-equal shell "bash") "~/.bash_history")
736 (setq-local comint-input-ring-size hsize)) 753 ((string-equal shell "ksh") "~/.sh_history")
737 (setq comint-input-ring-file-name 754 ((string-equal shell "zsh") "~/.zsh_history")
738 (concat 755 (t "~/.history"))))
739 remote 756 (when (stringp hfile)
740 (or hfile 757 (setq hsize
741 (cond ((string-equal shell "bash") "~/.bash_history") 758 (if remote
742 ((string-equal shell "ksh") "~/.sh_history") 759 (shell-command-to-string "echo -n $HISTSIZE")
743 ((string-equal shell "zsh") "~/.zsh_history") 760 (getenv "HISTSIZE")))
744 (t "~/.history"))))) 761 (and (stringp hsize)
762 (integerp (setq hsize (string-to-number hsize)))
763 (> hsize 0)
764 (setq-local comint-input-ring-size hsize))
765 (setq comint-input-ring-file-name
766 (concat remote hfile)))
745 (if (or (equal comint-input-ring-file-name "") 767 (if (or (equal comint-input-ring-file-name "")
746 (equal (file-truename comint-input-ring-file-name) 768 (equal (file-truename comint-input-ring-file-name)
747 (file-truename null-device))) 769 (file-truename null-device)))