2

I would like it if my Emacs were to save the M-x calls in most-recently used order and if this order could persist and grow across restarts/other sessions. I am using Helm, so I guess I should be asking about helm-M-x, but I thought this would be a general problem which might have a solution.

Ideally the solution would not depend on desktop-save-mode, but if it does I am okay with that.


Edit: Apparently I should be using savehist as I gathered from the comments, but for some reason it is not working. It seems like my savehist file is being populated, but it is not being loaded when I start Emacs anew.

(setq savehist-file "~/.emacs.d/savehist")
(setq history-length t)
(setq history-delete-duplicates t)
(setq savehist-additional-variables 
                    '(savehist-minibuffer-history-variables
                      magit-read-rev-history
                      helm-M-x-input-history
                      read-expression-history
                      helm-grep-history
                      minibuffer-history
                      file-name-history
                      ido-file-history
                      evil-ex-history
                      mark-ring
                      search-ring
                      extended-command-history
                      evil-jumps-history
                      kill-ring
                      search-ring
                      regexp-search-ring
                      compile-history
                      log-edit-comment-ring
                      command-history))
(savehist-mode 1)
  • I don't know about Helm, but how is your requirement different to the way the standard history already works? (which you can persist across sessions, along with other minibuffer histories, by enabling (savehist-mode 1) in your init file. – phils Oct 1 '18 at 0:52
  • Are you asking about helm-m-x or are you asking about M-x in general? The question isn't clear in this regard. – Drew Oct 1 '18 at 1:05
  • You generally do not need to add things to savehist-additional-variables. That is only needed for variables which are not already being treated as a minibuffer history. So adding ring variables is valid, but most of those -history suffix vars are probably not needed. You can always do some testing with savehist-mode enabled (i.e. just invoke all the history-using features you are interested in) and then verify what ended up in savehist-minibuffer-history-variables. – phils Oct 1 '18 at 3:06
  • What does C-h v savehist-loaded say after you start Emacs? (And if that indicates that it failed to load the file, savehist-mode should have signaled an error.) – phils Oct 1 '18 at 3:09
1

You can use savehist-mode to do what you want, if you customize savehist-additional-variables to add command-history to it.

M-x puts each executed command on command-history as a list of the command and the arguments you (implicitly or explicitly) provided. It is not a plain minibuffer history, so it isn't handled automatically by savehist-mode. But if you add it to savehist-additional-variables then it will be saved and can later be restored.

  • extended-command-history will automatically be saved by savehist, OTOH. – phils Oct 1 '18 at 2:00
1

There is a smex package that among other persists all M-x calls between emacs sessions in MRU.

You can either call smex directly instead of general execute-extended-command or call counsel-M-x (ivy+counsel packages) that will use smex under the hood.

I was thinking helm-m-x could do the same but if not, I believe it could be extended in the similar manner.

PS, btw there is helm-M-x-always-save-history variable in helm, maybe it could help?

  • counsel-M-x (ivy+counsel packages) that will use smex under the hood." - I thought counsel-M-x is a replacement for smex, not a wrapper, no? – npostavs Jan 24 at 13:11
  • 1
    @npostavs On its own, counsel-M-x provides ivy incremental completion on top of execute-extended-command. Using it with smex adds sorting based on frequency of use. – glucas Jan 24 at 15:19
  • See also amx, which is a more recent fork of smex and also supported by counsel-M-x. – glucas Jan 24 at 15:22
0

I had the same Problem and it seems that even though helm-M-x-input-history is saved by savehist per default, this doesn't help showing previously executed commands when using helm-M-x.

Despite the above comment saying that extended-command-history was saved automatically by savehist, the only thing that helped me was adding extended-command-history to savehist-additional-variables.

Additionally setting helm-M-x-always-save-history to t saves every helm-M-x command to extended-command-history, even when it fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.