2

A common part of my workflow is to copy and paste file paths into Emacs. This is usually done by navigating to the file in my desktop environment or from a terminal.

What would be more convenient is for me to do so via counsel-find-file, navigate to the file path and then insert at the point in the current buffer (or some similar workflow with autocompletion) instead of actually opening the file.

Is there any way to do this in Emacs?

  • I guess this a bit subjective, but the ivy, counsel and swiper packages are so intricately tied and overlapping (they're part of the same project) that I advocate using only a single tag for simplicity, to avoid giving rise to something like the smorgasbord of org tags. In fact, I may go as far as suggesting to make the ivy, counsel and swiper tags synonymous. As justification, notice that the answer to your counsel-find-file question was ivy-specific. – Basil Feb 27 '18 at 18:04
6

What would be more convenient is for me to do so via counsel-find-file, navigate to the file path and then insert at the point in the current buffer (or some similar workflow with autocompletion) instead of actually opening the file.

Ivy provides a built-in way to do this. During Ivy completion you can invoke custom actions on the current candidate; see (ivy) Actions. One of the standard actions, "insert", is bound to i by default.

So, during counsel-find-file, you can type M-oi to insert the currently selected filename into the current buffer.

0

A simplified version of counsel-find-file:

(defun my-counsel-insert-file-path ()
  "Insert file path."
  (interactive)
  (unless (featurep 'counsel) (require 'counsel))
  (ivy-read "Find file: " 'read-file-name-internal
            :matcher #'counsel--find-file-matcher
            :action
            (lambda (x)
              (insert x))))

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.