unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Saving/Recalling Shell Commands History?
@ 2006-08-21 15:07 gamename
  2006-08-21 16:35 ` Drew Adams
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: gamename @ 2006-08-21 15:07 UTC (permalink / raw)


Hi,

Is there any way to save the commands issued in shell-mode and recall
them when a new shell starts, or even in a new instance of emacs?

TIA,
-T

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Saving/Recalling Shell Commands History?
  2006-08-21 15:07 Saving/Recalling Shell Commands History? gamename
@ 2006-08-21 16:35 ` Drew Adams
  2006-08-21 20:09 ` Peter Dyballa
  2006-08-21 20:19 ` Jesse Alama
  2 siblings, 0 replies; 13+ messages in thread
From: Drew Adams @ 2006-08-21 16:35 UTC (permalink / raw)


    Is there any way to save the commands issued in shell-mode and recall
    them when a new shell starts, or even in a new instance of emacs?

That should happen automatically, AFAIK. See variable
`comint-input-ring-file-name', which should be set automatically by shell
mode. See also `C-h m' in a shell buffer.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-21 15:07 Saving/Recalling Shell Commands History? gamename
  2006-08-21 16:35 ` Drew Adams
@ 2006-08-21 20:09 ` Peter Dyballa
  2006-08-21 20:17   ` Drew Adams
  2006-08-21 20:19 ` Jesse Alama
  2 siblings, 1 reply; 13+ messages in thread
From: Peter Dyballa @ 2006-08-21 20:09 UTC (permalink / raw)
  Cc: help-gnu-emacs


Am 21.08.2006 um 17:07 schrieb gamename:

> Hi,
>
> Is there any way to save the commands issued in shell-mode and recall
> them when a new shell starts, or even in a new instance of emacs?
>

The documentation writes:

	(defun comint-read-input-ring (&optional silent)
	  "Set the buffer's `comint-input-ring' from a history file.
	The name of the file is given by the variable `comint-input-ring- 
file-name'.
	The history ring is of size `comint-input-ring-size', regardless of  
file size.
	If `comint-input-ring-file-name' is nil this function does nothing.
	
	(defun comint-write-input-ring ()
	  "Writes the buffer's `comint-input-ring' to a history file.
	The name of the file is given by the variable `comint-input-ring- 
file-name'.
	The original contents of the file are lost if `comint-input-ring' is  
not empty.
	If `comint-input-ring-file-name' is nil this function does nothing.

Although I have set comint-input-ring-file-name, this file never gets  
written. And neither read (I've put some uniq commands into it to see  
if). All *shell* does in my case, is it reads ~/.tcsh_history –  
because I have tcsh as login shell.

Could be there is bug, or I simply need to activate the described  
behaviour with some start-up or shut-down hooks ...

--
Greetings

   Pete

»¿ʇı̣ əsnqɐ ʇ,uɐɔ noʎ ɟı̣
ɓuı̣ɥʇʎuɐ sı̣ pooɓ ʇɐɥʍ«

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Saving/Recalling Shell Commands History?
  2006-08-21 20:09 ` Peter Dyballa
@ 2006-08-21 20:17   ` Drew Adams
  2006-08-21 21:08     ` Peter Dyballa
  0 siblings, 1 reply; 13+ messages in thread
From: Drew Adams @ 2006-08-21 20:17 UTC (permalink / raw)


    Although I have set comint-input-ring-file-name, this file never gets  
    written.

Did you set it locally in the shell buffer (mode)? The doc string says this:

 "This variable is buffer-local, and is a good thing to
  set in mode hooks."

What is the value of comint-input-ring-file-name in your shell buffer if you don't ever explicitly set it?
    

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-21 15:07 Saving/Recalling Shell Commands History? gamename
  2006-08-21 16:35 ` Drew Adams
  2006-08-21 20:09 ` Peter Dyballa
@ 2006-08-21 20:19 ` Jesse Alama
  2006-08-21 21:11   ` Peter Dyballa
  2 siblings, 1 reply; 13+ messages in thread
From: Jesse Alama @ 2006-08-21 20:19 UTC (permalink / raw)


"gamename" <namesagame-usenet@yahoo.com> writes:

> Is there any way to save the commands issued in shell-mode and recall
> them when a new shell starts, or even in a new instance of emacs?

Does the same thing happen when you use eshell?

Jesse

-- 
Jesse Alama (alama@stanford.edu)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-21 20:17   ` Drew Adams
@ 2006-08-21 21:08     ` Peter Dyballa
  2006-08-21 21:30       ` Drew Adams
  2006-08-21 21:32       ` Kevin Rodgers
  0 siblings, 2 replies; 13+ messages in thread
From: Peter Dyballa @ 2006-08-21 21:08 UTC (permalink / raw)
  Cc: help-gnu-emacs


Am 21.08.2006 um 22:17 schrieb Drew Adams:

>     Although I have set comint-input-ring-file-name, this file  
> never gets
>     written.
>
> Did you set it locally in the shell buffer (mode)? The doc string  
> says this:
>
>  "This variable is buffer-local, and is a good thing to
>   set in mode hooks."

No. I found it in a *customise* buffer and set it there first.

>
> What is the value of comint-input-ring-file-name in your shell  
> buffer if you don't ever explicitly set it?
>

Now, edited to fit into a system of a handful Emacsen, it is set as:

	'(comint-input-ring-file-name (concat desktop-dirname "/history"))

Desktop-dirname is defined in .emacs. In recent GNU Emacs 23 the  
variable's value is described as:

	comint-input-ring-file-name is a variable defined in `comint.el'.
	Its value is "~/.history"
	Local in buffer *shell*; global value is
	"~/.emacs.d/PDesktop-23/history"
	Documentation:
	*If non-nil, name of the file to read/write input history.
	See also `comint-read-input-ring' and `comint-write-input-ring'.
	
	This variable is buffer-local, and is a good thing to set in mode  
hooks.
	
	You can /customize/ this variable.

When I comment the comint-input-ring-file-name setting line, its  
value is simply "~/.history."

I retrieved the values by pasting comint-input-ring-file-name into  
*shell* buffer and pressing C-h v.

--
Greetings

   Pete

 From error to error, one discovers the entire truth.
                                                      - Sigmund Freud

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-21 20:19 ` Jesse Alama
@ 2006-08-21 21:11   ` Peter Dyballa
  0 siblings, 0 replies; 13+ messages in thread
From: Peter Dyballa @ 2006-08-21 21:11 UTC (permalink / raw)
  Cc: help-gnu-emacs


Am 21.08.2006 um 22:19 schrieb Jesse Alama:

> "gamename" <namesagame-usenet@yahoo.com> writes:
>
>> Is there any way to save the commands issued in shell-mode and recall
>> them when a new shell starts, or even in a new instance of emacs?
>
> Does the same thing happen when you use eshell?
>

Eshell has it's own history file in ~/.eshell/history.

--
Greetings

   Pete

Time flies like an error -- but fruit flies like a banana!
                              (almost Groucho Marx)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: Saving/Recalling Shell Commands History?
  2006-08-21 21:08     ` Peter Dyballa
@ 2006-08-21 21:30       ` Drew Adams
  2006-08-21 21:32       ` Kevin Rodgers
  1 sibling, 0 replies; 13+ messages in thread
From: Drew Adams @ 2006-08-21 21:30 UTC (permalink / raw)


    Now, edited to fit into a system of a handful Emacsen, it is set as:

    	'(comint-input-ring-file-name (concat desktop-dirname "/history"))

    Desktop-dirname is defined in .emacs. In recent GNU Emacs 23 the
    variable's value is described as:

    	comint-input-ring-file-name is a variable defined in `comint.el'.
    	Its value is "~/.history"
    	Local in buffer *shell*; global value is
    	"~/.emacs.d/PDesktop-23/history"

    When I comment the comint-input-ring-file-name setting line, its
    value is simply "~/.history."

And that doesn't work?

Sorry, I'm no expert on this; perhaps someone else has a suggestion. I
didn't do anything to set that variable, and it works out of the box (on
Windows). FWIW, I have these values for it:

 comint-input-ring-file-name is a variable defined in `comint.el'.
 Its value is "~/.bash_history"
 Local in buffer *shell*; global value is nil

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-21 21:08     ` Peter Dyballa
  2006-08-21 21:30       ` Drew Adams
@ 2006-08-21 21:32       ` Kevin Rodgers
  2006-08-21 22:14         ` Peter Dyballa
  2006-08-22 23:13         ` Peter Dyballa
  1 sibling, 2 replies; 13+ messages in thread
From: Kevin Rodgers @ 2006-08-21 21:32 UTC (permalink / raw)


Peter Dyballa wrote:
> 
> Am 21.08.2006 um 22:17 schrieb Drew Adams:
> 
>>     Although I have set comint-input-ring-file-name, this file never gets
>>     written.
>>
>> Did you set it locally in the shell buffer (mode)? The doc string says 
>> this:
>>
>>  "This variable is buffer-local, and is a good thing to
>>   set in mode hooks."
> 
> No. I found it in a *customise* buffer and set it there first.
> 
>>
>> What is the value of comint-input-ring-file-name in your shell buffer 
>> if you don't ever explicitly set it?
>>
> 
> Now, edited to fit into a system of a handful Emacsen, it is set as:
> 
>     '(comint-input-ring-file-name (concat desktop-dirname "/history"))
> 
> Desktop-dirname is defined in .emacs. In recent GNU Emacs 23 the 
> variable's value is described as:
> 
>     comint-input-ring-file-name is a variable defined in `comint.el'.
>     Its value is "~/.history"
>     Local in buffer *shell*; global value is
>     "~/.emacs.d/PDesktop-23/history"

So you have managed to change its global value, but not the value in
the *shell* buffer.

>     Documentation:
>     *If non-nil, name of the file to read/write input history.
>     See also `comint-read-input-ring' and `comint-write-input-ring'.
>     
>     This variable is buffer-local, and is a good thing to set in mode 
> hooks.
>     
>     You can /customize/ this variable.
> 
> When I comment the comint-input-ring-file-name setting line, its value 
> is simply "~/.history."

So leave it commented out, and put this in your ~/.emacs file:

(add-hook 'shell-mode-hook
           (lambda ()
	    (setq comint-input-ring-file-name ; buffer-local
		  (expand-file-name "history" desktop-dirname))))

> I retrieved the values by pasting comint-input-ring-file-name into 
> *shell* buffer and pressing C-h v.

OK.

-- 
Kevin

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-21 21:32       ` Kevin Rodgers
@ 2006-08-21 22:14         ` Peter Dyballa
  2006-08-22 23:13         ` Peter Dyballa
  1 sibling, 0 replies; 13+ messages in thread
From: Peter Dyballa @ 2006-08-21 22:14 UTC (permalink / raw)
  Cc: help-gnu-emacs


Am 21.08.2006 um 23:32 schrieb Kevin Rodgers:

>>     Its value is "~/.history"
>>     Local in buffer *shell*; global value is
>>     "~/.emacs.d/PDesktop-23/history"
>
> So you have managed to change its global value, but not the value in
> the *shell* buffer.

Ahh! Now I understand how to understand this!

>
>>     Documentation:
>>     *If non-nil, name of the file to read/write input history.
>>     See also `comint-read-input-ring' and `comint-write-input-ring'.
>>         This variable is buffer-local, and is a good thing to set  
>> in mode hooks.
>>         You can /customize/ this variable.
>> When I comment the comint-input-ring-file-name setting line, its  
>> value is simply "~/.history."
>
> So leave it commented out, and put this in your ~/.emacs file:
>
> (add-hook 'shell-mode-hook
>           (lambda ()
> 	    (setq comint-input-ring-file-name ; buffer-local
> 		  (expand-file-name "history" desktop-dirname))))

It works a bit:

	Its value is
	"/Users/pete/.emacs.d/PDesktop-22/history"
	Local in buffer *shell*; global value is nil

but, since I deleted ~/.tcsh_history I have /no/ history at all ...  
So the history file as set in .emacs and although it exists and  
although I set comint-input-ring-size to a big value is not read. And  
neither written in GNU Emacsen 22.0.50 nor 23.0.0 nor 21.2.


Enough to prepare a bug report?

--
Greetings

   Pete

"Email is a wonderful thing for people whose role in life is to be on  
top of things. But not for me; my role is to be on the bottom of  
things. What I do takes long hours of studying and uninterruptible  
concentration."
                                              -Donald Knuth

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-21 21:32       ` Kevin Rodgers
  2006-08-21 22:14         ` Peter Dyballa
@ 2006-08-22 23:13         ` Peter Dyballa
  2006-08-23 14:20           ` Kevin Rodgers
  1 sibling, 1 reply; 13+ messages in thread
From: Peter Dyballa @ 2006-08-22 23:13 UTC (permalink / raw)
  Cc: help-gnu-emacs


Am 21.08.2006 um 23:32 schrieb Kevin Rodgers:

> So leave it commented out, and put this in your ~/.emacs file:
>
> (add-hook 'shell-mode-hook
>           (lambda ()
> 	    (setq comint-input-ring-file-name ; buffer-local
> 		  (expand-file-name "history" desktop-dirname))))

The code above works, but it only works for GNU Emacs. The shell  
never gets knowledge of this!

I think the meaning of comint-input-ring-file-name is to let GNU  
Emacs know directly, for whatever reason or use, what the shell's  
history file is. Maybe it's useful in dumb shells that cannot provide  
their own history, maybe it's useful in TRAMP, i.e. working in a  
remote shell.

For my purpose I extended your hook to make the shell know where the  
actual history file of the Emacs *shell* buffer is. Since I want to  
separate the different Emacs versions I am using I have created an  
intermediate and temporary file, ~/.emacs_tcsh-init, which contains a  
line that tells the shell which file to use for a history. For tcsh  
the file ~/.emacs_tcsh is executed in *shell* buffer. So  
~/.emacs_tcsh needs to be extended by one line that loads the  
intermediate file – and removes it!

Here is my extended hook:

	(add-hook 'shell-mode-hook
           (lambda ()
	    (setq comint-input-ring-file-name
		  (expand-file-name "history" desktop-dirname))
	    (setq histfile_cmd (format
			    "echo \"set histfile = %s\" > .emacs_tcsh-init"
			    comint-input-ring-file-name))
	    (shell-command histfile_cmd)
	))

~/.emacs_tcsh-init will have a line like:

	set histfile = <some path name>

The extended version of ~/.emacs_tcsh has two more lines:

	sleep 1
	if (-e ~/.emacs_tcsh-init) source ~/.emacs_tcsh-init && rm  
~/.emacs_tcsh-init

The sleep is needed, otherwise the file's removal fails with an error  
message in *shell*. One cannot feel this one more second in GNU  
Emacs's start-up.


For bash the history file is addressed by the environment variable  
$HISTFILE. It's probably OK to use the syntax

	HISTFILE=<some path name>

without 'export.' The syntax for the ~/.emacs_bash file would be:

	if [ -e ~/.emacs_bash-init ]; then . ~/.emacs_bash-init && rm  
~/.emacs_bash-init; fi


There is one more disadvantage: an empty buffer *Shell Command  
Output* is created because of executing (shell-command  
histfile_cmd) ... Could be a kill-buffer in the hook removes it!

--
Greetings

   Pete

The day Microsoft makes something that doesn't suck
is the day they start selling vacuum cleaners.
                                     Ernest Jan Plugge

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-22 23:13         ` Peter Dyballa
@ 2006-08-23 14:20           ` Kevin Rodgers
  2006-08-23 15:20             ` Peter Dyballa
  0 siblings, 1 reply; 13+ messages in thread
From: Kevin Rodgers @ 2006-08-23 14:20 UTC (permalink / raw)


Peter Dyballa wrote:
> 
> Am 21.08.2006 um 23:32 schrieb Kevin Rodgers:
> 
>> So leave it commented out, and put this in your ~/.emacs file:
>>
>> (add-hook 'shell-mode-hook
>>           (lambda ()
>>         (setq comint-input-ring-file-name ; buffer-local
>>           (expand-file-name "history" desktop-dirname))))
> 
> The code above works, but it only works for GNU Emacs. The shell never 
> gets knowledge of this!

Naturally :-)

> I think the meaning of comint-input-ring-file-name is to let GNU Emacs 
> know directly, for whatever reason or use, what the shell's history file 
> is. Maybe it's useful in dumb shells that cannot provide their own 
> history, maybe it's useful in TRAMP, i.e. working in a remote shell.

I think it's useful, in that the Emacs history commands have access to
the history of previous (or concurrent?) shells whose commands are
persisted in that file.

It seems redundant though, that both Emacs shell-mode and the inferior
shell it's running would write the same commands to that file.

> For my purpose I extended your hook to make the shell know where the 
> actual history file of the Emacs *shell* buffer is. Since I want to 
> separate the different Emacs versions I am using I have created an 
> intermediate and temporary file, ~/.emacs_tcsh-init, which contains a 
> line that tells the shell which file to use for a history. For tcsh the 
> file ~/.emacs_tcsh is executed in *shell* buffer. So ~/.emacs_tcsh needs 
> to be extended by one line that loads the intermediate file – and 
> removes it!

I don't understand why it needs to be so complicated.  What do you
mean by "separate the different Emacs versions [you] are using"?  None
of your code references emacs-version, emacs-major-version, or
emacs-minor-version.

> Here is my extended hook:
> 
>     (add-hook 'shell-mode-hook
>           (lambda ()
>         (setq comint-input-ring-file-name
>           (expand-file-name "history" desktop-dirname))
>         (setq histfile_cmd (format
>                 "echo \"set histfile = %s\" > .emacs_tcsh-init"
>                 comint-input-ring-file-name))
>         (shell-command histfile_cmd)
>     ))
> 
> ~/.emacs_tcsh-init will have a line like:
> 
>     set histfile = <some path name>
> 
> The extended version of ~/.emacs_tcsh has two more lines:
> 
>     sleep 1
>     if (-e ~/.emacs_tcsh-init) source ~/.emacs_tcsh-init && rm 
> ~/.emacs_tcsh-init
> 
> The sleep is needed, otherwise the file's removal fails with an error 
> message in *shell*. One cannot feel this one more second in GNU Emacs's 
> start-up.

Why not get rid of all that, and just put this in your .login file:

set histfile=~/tcsh_history

and this in your .emacs file:

(add-hook 'shell-mode-hook
           (lambda ()
	    (let ((shell (file-name-sans-extension
			  (file-name-nondirectory (or explicit-shell-file-name
						      shell-file-name)))))
	      (setq comint-input-ring-file-name
		    (concat "~/" shell "_history")))))

> For bash the history file is addressed by the environment variable 
> $HISTFILE. It's probably OK to use the syntax
> 
>     HISTFILE=<some path name>

HISTFILE=~/bash_history

would work with the above shell-mode-hook.

> without 'export.' The syntax for the ~/.emacs_bash file would be:
> 
>     if [ -e ~/.emacs_bash-init ]; then . ~/.emacs_bash-init && rm 
> ~/.emacs_bash-init; fi
> 
> There is one more disadvantage: an empty buffer *Shell Command Output* 
> is created because of executing (shell-command histfile_cmd) ... Could 
> be a kill-buffer in the hook removes it!

An emtpy *Shell Command Output* buffer isn't displayed, so what's the
harm?

-- 
Kevin

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Saving/Recalling Shell Commands History?
  2006-08-23 14:20           ` Kevin Rodgers
@ 2006-08-23 15:20             ` Peter Dyballa
  0 siblings, 0 replies; 13+ messages in thread
From: Peter Dyballa @ 2006-08-23 15:20 UTC (permalink / raw)
  Cc: help-gnu-emacs


Am 23.08.2006 um 16:20 schrieb Kevin Rodgers:

> I don't understand why it needs to be so complicated.  What do you
> mean by "separate the different Emacs versions [you] are using"?  None
> of your code references emacs-version, emacs-major-version, or
> emacs-minor-version.
>
>> Here is my extended hook:
>>     (add-hook 'shell-mode-hook
>>           (lambda ()
>>         (setq comint-input-ring-file-name
>>           (expand-file-name "history" desktop-dirname))
>>         (setq histfile_cmd (format
>>                 "echo \"set histfile = %s\" > .emacs_tcsh-init"
>>                 comint-input-ring-file-name))
>>         (shell-command histfile_cmd)
>>     ))

Desktop-dirname points to the different emacs-major-versions. My  
purpose is to have the history files unchanged while I launch a  
Carbon Emacs or an Emacs.app, which otherwise would write /their/  
history into the same file. And when I'll start to have an intel  
based Mac with Vanderpool built-in I also could run some Losedows,  
Linux, Solaris, or other BSD Emacsen ...


What I wonder is whether GNU Emacs caches the contents of the history  
file. If the contents of the history file changes by interaction of  
some other shell, /my/ Emacs shell would find the other's history if  
it would re-read the history file.

(BTW, it worked in Emacs shell with the default ~/.tcsh_history file,  
although comint-input-ring-file-name pointed to some completely  
different file. Could be shell-mode could not find a command from the  
history file, but then tcsh would succeed.)

--
Greetings

   Pete

Well done is better than well said.
                            -- Benjamin Franklin

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2006-08-23 15:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-21 15:07 Saving/Recalling Shell Commands History? gamename
2006-08-21 16:35 ` Drew Adams
2006-08-21 20:09 ` Peter Dyballa
2006-08-21 20:17   ` Drew Adams
2006-08-21 21:08     ` Peter Dyballa
2006-08-21 21:30       ` Drew Adams
2006-08-21 21:32       ` Kevin Rodgers
2006-08-21 22:14         ` Peter Dyballa
2006-08-22 23:13         ` Peter Dyballa
2006-08-23 14:20           ` Kevin Rodgers
2006-08-23 15:20             ` Peter Dyballa
2006-08-21 20:19 ` Jesse Alama
2006-08-21 21:11   ` Peter Dyballa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).