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.
In my trials, I find that commands executed in shell-mode get saved to
~/.bash_history
, just as whenbash
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.4571
savehist-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 isbash
, and I've frequently found that by defaultbash
won't save history until the process exits cleanly, i.e., on logout -- which doesn't happen if you kill the shell buffer withC-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 doinglogout
orexit
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 bebash
, 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 usebash
myself, so can't help much if you're using anything else.) – Aaron Miller – 2013-05-20T16:41:19.7901@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