This is the same question as Save current file with a slightly different name, but for Helm. (The top answer given for the original question doesn't work in Helm, sadly: M-n (next-history-element) doesn't do quite the same thing as in vanilla Emacs.)

The question is: how can I insert the filename of the current buffer into the minibuffer during write-file. So for example:

C-x C-w (this gets me to a minibuffer prompt with the current directory); in plain Emacs I could type M-n and this would insert the current filename for me to edit to a slightly different name. What can I do instead in Helm?

  • Is M-n bound to next-history-element or is it bound to something else? IOW, is next-history-element refedfined by helm to do something different? And if so, should that not be considered a bug in helm?
    – NickD
    Commented Jan 12, 2021 at 17:43
  • M-n is bound to next-history-element. Commented Jan 12, 2021 at 21:12

2 Answers 2


There's nothing like it in Helm AFAIK, so I tried to mimic what C-x C-w M-n does with the following command. The difference is you have to press C-j (instead of M-n) to expand the filename.

(defun helm-write-file ()
  (let ((filename (helm-read-file-name
                   "Write file: "
                   :preselect (when (buffer-file-name)
                                (helm-basename (buffer-file-name))))))
    (write-file filename)))

(global-set-key (kbd "C-x C-w") 'helm-write-file)

Alternatively, you can skip the Helm interface with:

(add-to-list 'helm-completing-read-handlers-alist '(write-file . nil))
  • OK, so this works fine with C-x C-f, but not with C-x C-w. Commented Jan 16, 2021 at 22:59
  • Press C-x C-f C-j, make your changes then RET. It should create a new file in the current directory.
    – jagrg
    Commented Jan 17, 2021 at 19:29
  • I'm baffled, sorry! I don't want to create a new file, I want to write an existing buffer under a new name. Commented Jan 18, 2021 at 12:32
  • In that case it's C-x C-f M-R C-j. BTW you can also skip the Helm interface with (add-to-list 'helm-completing-read-handlers-alist '(write-file . nil)), in which case C-x C-w M-n should do what it does in vanilla Emacs.
    – jagrg
    Commented Jan 18, 2021 at 16:00
  • C-x C-f M-R C-j renames a file, it does not write a buffer under a new name, which creates a copy. Thanks for the second tip, but I'd rather use Helm. Commented Jan 18, 2021 at 21:42

Bring up helm's file name history buffer with (helm-write-file followed by) C-c h. With a bit of luck, the file name you want is at the top. Select it with F2.

Note: To edit the filename, you need to navigate with C-f and C-b in the minibuffer. Using the arrow keys does not work for me. (The same caveat applies to the other answer.)

  • Thanks for your alternative simpler answer. I think you mean C-b not C-p? I prefer something I can engrave in muscle memory; "With a bit of luck" is not quite that. Commented Jun 5, 2021 at 15:14
  • Yes, corrected. Commented Jun 5, 2021 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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