1

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?

2
  • 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 Jan 12 at 17:43
  • M-n is bound to next-history-element. – Reuben Thomas Jan 12 at 21:12
0

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 ()
  (interactive)
  (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))
9
  • OK, so this works fine with C-x C-f, but not with C-x C-w. – Reuben Thomas Jan 16 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 Jan 17 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. – Reuben Thomas Jan 18 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 Jan 18 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. – Reuben Thomas Jan 18 at 21:42

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.