How to save shell history from emacs and use it in terminal?

2

I use emacs for almost everything, but when I run shell M-x shell and then exit from emacs, the history is not saved, I've tried (savehist-mode 1) for this purpose but it doesn't seem to work, is there any way to save the history of the commands used in emacs and use them when I use shell outside of emacs?

poz2k4444

Posted 2013-05-20T16:23:35.553

Reputation: 794

In my trials, I find that commands executed in shell-mode get saved to ~/.bash_history, just as when bash runs in a more ordinary terminal emulator; I'm not sure why you would be seeing different behavior. What shell are you running, and how is it configured with regard to history? – Aaron Miller – 2013-05-20T16:33:06.697

@AaronMiller I'm not sure how to answer your questions, as I said in mine, I just type M-x shell and when I kill the buffer it doesn't save the history, the only attempt I did was to put (savehist-mode 1) in .emacs file but still I cannot see the commands used in emacs – poz2k4444 – 2013-05-20T16:37:09.457

1savehist-mode applies by default only to minibuffer input, and is thus not applicable here (though you might find it useful in general). Most likely your login shell is bash, and I've frequently found that by default bash won't save history until the process exits cleanly, i.e., on logout -- which doesn't happen if you kill the shell buffer with C-x k or similar; when that happens, the shell process is killed, rather than exiting on its own. If that's what you do right now, try doing logout or exit in your shell buffer before killing the buffer, and see if history gets saved. – Aaron Miller – 2013-05-20T16:39:35.437

(You can determine your login shell with ps -p $$ at the command line; the value under 'CMD' in the output is the shell you're using. Most likely it'll be bash, but if it's not, add a comment to say what it is, and someone may be able to assist you further from there -- I only use bash myself, so can't help much if you're using anything else.) – Aaron Miller – 2013-05-20T16:41:19.790

1@AaronMiller yes it's bash, and yes, I just did what you said of exit the shell and the history was saved, I didn't know that, please post it as answer... Thanks in advance!! – poz2k4444 – 2013-05-20T16:43:33.650

Done, and glad to be of help! – Aaron Miller – 2013-05-20T16:50:21.163

Answers

5

savehist-mode isn't applicable here; the reason you're having this trouble is because Bash, by default, will only write to the history file when its in-memory history buffer is full or on exit, and the latter only if it exits cleanly -- that is, a Bash process which is killed rather than exiting will never write recent commands to the history file.

To work around the problem, ensure that you always cleanly finish your shell session by executing logout or exit, or typing C-d at the prompt, before killing your shell buffers; this will give Bash a chance to write the history file before exiting.

Also, see the Bash FAQ on history file management, for some ideas on how to have Bash write each command to history as soon as it's executed; once you've gotten that working, you need no longer worry about logging out of your shells before you kill their buffers, but can C-x k with wild abandon.

Aaron Miller

Posted 2013-05-20T16:23:35.553

Reputation: 8 849

1As suggested by the bash FAQ, setting PROMPT_COMMAND="history -a" in my .bashrc file worked nicely for me, thanks. – wytten – 2013-08-26T17:30:58.050