Yaroslav Bulatov
Yaroslav Bulatov

Reputation: 57903

Automatically saving shell history in Emacs

Is there an easy way to automatically save every command I execute in shell-mode buffer? I'm running things like python and lua from Emacs' shell buffer and want to save those in addition to regular bash commands.

Default behavior saves history in in .history or .bash_history, but it does not save input to subprocesses. As an example, if I do the following

ls /export/hda3/tmp
python
a=2+3
import sys
sys.exit()
ls /export/hda3/tmp

the following gets saved

#1328903075
ls /export/hda3/tmp
#1328903081
python
#1328903087
ls /export/hda3/tmp

Upvotes: 3

Views: 1369

Answers (1)

Tom
Tom

Reputation: 7586

Commands are saved automatically, only you need to make sure to actually exit the shell. If you simply kill the shell buffer then no commands will be saved.

I added a check to emacs exit to warn me if I have an open shell buffer, so that I can exit it manually:

(defun my-check-if-no-shell-buffer-exists ()
  (if (not (get-buffer "*shell*"))
      t
    (message "you have a shell buffer, make sure you exit it manually")
    nil))

(add-hook 'kill-emacs-query-functions 'my-check-if-no-shell-buffer-exists)

It would even be better if the shell buffer would do it automatically when killing the buffer. I think it's a bug in emacs that it fails to do that.

Edit: I noticed I have a setting which prevented running process warnings when emacs exits and that's why I needed the above function. If you get a warning about a running shell already when exiting, then you don't need it, you only need to exit the shell manually to save history.

Upvotes: 2

Related Questions