2

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, 2021 at 17:43
  • M-n is bound to next-history-element. Jan 12, 2021 at 21:12

2 Answers 2

1

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. 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
    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. 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
    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. Jan 18, 2021 at 21:42
0

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.)

2
  • 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. Jun 5, 2021 at 15:14
  • Yes, corrected. Jun 5, 2021 at 15:21

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.