Insert org links faster with Helm

Overview

I wrote a couple of functions for Helm which help with insertion of filenames and org links to files.

Why?

Helm enhances org-insert-link. However it doesn't allow marking, therefore it cannot insert multiple links at once.

Solution

I wrote a couple of functions which insert filepaths or org links to all marked files from within helm-find-file. It works great with helm-recoll too.

(defvar helm-ff-insert-link-relative-separator "\n")
(defun helm-ff-run-insert-org-link-relative ()
  (interactive)
  (helm-exit-and-execute-action
   #'(lambda (cand)
	   (let ((files (helm-marked-candidates)))
		 (if (equal (length files) 1)
			 (progn (insert (format "[[file:%s][]]" (file-relative-name (car files))))
					(goto-char (- (point) 2)))
		   (dolist (file files)
			 (insert (format "[[file:%s]]%s"
							 (file-relative-name file)
							 helm-ff-insert-link-relative-separator))))))))

(defun helm-ff-run-insert-filename-relative ()
  (interactive)
  (helm-exit-and-execute-action
   #'(lambda (cand)
	   (let ((files (helm-marked-candidates)))
		 (if (equal (length files) 1)
			 (insert (format "%s" (file-relative-name (car files))))
		   (dolist (file files)
			 (insert (format "%s%s"
							 (file-relative-name file)
							 helm-ff-insert-link-relative-separator))))))))

(define-key helm-find-files-map (kbd "C-c l") #'helm-ff-run-insert-org-link-relative)
(define-key helm-find-files-map (kbd "C-c f") #'helm-ff-run-insert-filename-relative)
(define-key helm-recoll-map (kbd "C-c l") #'helm-ff-run-insert-org-link-relative)
(define-key helm-recoll-map (kbd "C-c f") #'helm-ff-run-insert-filename-relative)
(define-key helm-recoll-map [f12] #'helm-ff-run-insert-org-link-relative)

Created: 2022-02-12 Сб 12:25