5

There is something that blocks my workflow quite often and I'm quite sure it's easy to solve.

I sometimes copy file paths (e.g. /tmp/bar/foo.gz) from the command line into my host systems clipboard. Then I want to open them inside emacs and edit them. My system clipboard is linked to emacs clipboard so this is no problem.

Currently I use (ido-find-file) bound to C-x C-f which brings up the minibuffer with the path of the current file e.g. /home/cb0/.

Option 1: If I now yank into the minibuffer it will always append to the current selection, resulting in something non-existing /home/cb0//tmp/bar/foo.gz".

Option 2: Pressing M-DEL multiple times will delete the path in the minibuffer, leaving a single / at the beginning. Now yanking will also result in a faulty path ``//tmp/bar/foo.gz`.

Option 3: Pressing C-a (go to line start), C-k (kill rest of line) and yank again also does not work in minibuffer.

These are not applicable for me:

  • use emacsclient straight from command line to edit this file

  • use a shell script to tell emacs to open the file whose filepath is in current clipboard.

  • using find-file or helm-mode-find-file all left me with the same result

What is the easiest way to have a filepath in "system" or "emacs" clipboard, and then head over to emacs and start editing just that file ?

cb0
  • 1,077
  • 8
  • 11
  • Are you wedded to using Ido? FWIW, both vanilla Emacs and [Icicles](http://www.emacswiki.org/emacs/Icicles) let you use absolute file names easily (including yanking them from the clipboard) and edit minibuffer input arbitrarily. (In Icicles, `M-k` clears the minibuffer completely.) – Drew Nov 16 '15 at 16:44
  • Possible duplicate of http://emacs.stackexchange.com/q/5396/780. – glucas Nov 16 '15 at 18:28

3 Answers3

6

C-x C-f x 2 C-aC-yC-kenter

Pressing C-x C-f while inside ido-find-file will take you back into the normal find-file in which you can easily clear out all the input and yank your path into.

Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62
2

Normally, if you open /home/cb0//tmp/bar/foo.gz with find-file emacs will open /tmp/bar/foo.gz because /home/cb0/ will be ignored.

But, If you want to delete before yanking you can use this function:

(defun backward-delete-line ()
     (interactive)
     (delete-region
      (point)
      (save-excursion (beginning-of-line 1) (point))))
   (global-set-key (kbd "S-<backspace>") 'backward-delete-line)
djangoliv
  • 3,169
  • 16
  • 31
  • I don't think this answers the question. The OP already said that he can delete all of the default input from the minibuffer, but Ido reinserts a `/` there, if I understand correctly. – Drew Nov 16 '15 at 16:42
  • The function will clear the minibuffer completely even if you use ido-find-file – djangoliv Nov 16 '15 at 16:47
  • I see. That seems to contradict the OP's "option 2" and "option 3", but perhaps he was just mistaken about that, or perhaps what he wrote about it is not what he meant. – Drew Nov 16 '15 at 17:17
  • You are right, with normal "find-file" there seems to be no problem with "/home/cb0//tmp/bar/foo.gz" which brings up the right file. – cb0 Nov 17 '15 at 07:26
2

Back when I was using Ido, I was relying on this code:

(define-key ido-file-dir-completion-map (kbd "C-y") 'ido-yank)
(defun ido-yank ()
  "Forward to `yank'."
  (interactive)
  (if (file-exists-p (current-kill 0))
      (ido-fallback-command)
    (yank)))

With this setup, you have to press C-y C-y: the first one gets you to regular find-file, and the second one yanks.

Now, I'm using Ivy, where C-y simply works with no extra setup. Well, there's some minor setup:

(global-set-key (kbd "C-x C-f") 'counsel-find-file)

While find-file works similar to ido-find-file when ivy-mode is on, counsel-find-file additionally allows you to cycle files with C-M-n/C-M-p. And it also works with ivy-resume.

abo-abo
  • 13,943
  • 1
  • 29
  • 43