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?

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

2 Answers2

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))
jagrg
  • 3,824
  • 4
  • 19
  • OK, so this works fine with `C-x C-f`, but not with `C-x C-w`. – Reuben Thomas Jan 16 '21 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 '21 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 '21 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 '21 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 '21 at 21:42
  • Phew. Sorry about that. I changed the answer. Let me know if that gets you any closer. – jagrg Jan 19 '21 at 02:29
  • Thanks very much for your heroic efforts! I think before marking this as the answer, I would like to understand why, as @NickD queried, simply pressing `M-n` does not work in Helm as it does in plain Emacs. – Reuben Thomas Jan 19 '21 at 17:34
  • I see that Helm development has been suspended, so no point filing a bug there, as I was going to do. I'll accept this answer as the best I'm likely to get! Thanks again. – Reuben Thomas Jan 19 '21 at 18:45
  • Could be a bug (cc @xuchunyang). For example, if a buffer is not visiting a file then its name is inserted in the minibuffer (as it does without Helm). If the buffer *is* visiting a file then it fails to insert its name. Either way someone has to go down the rabbit hole. – jagrg Jan 20 '21 at 02:28
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.)