1

In Doom Emacs and org-mode, I want to use org-insert-link pressing the minimum amount of keys. So I want to combine two-four commands in one. The default behaviour doesn't leave space before or after.

I have tried various variations of the following but it doesn't work:

(add-hook 'org-mode 'org-insert-link-with-space)
(defun org-insert-link-with-space ()
       "org-insert-link-with-space, like org-insert-link but after prompt"
       (interactive)
       (insert " ") (org-insert-link) (insert " ") (evil-insert-state)
)
(map! :ne "SPC m l" #'org-insert-link-with-space)

I want to type this:

here is a link for you

pressing the minimum amount of keys:

hereSpaceisSpaceaEscSpacemll<copy/paste link and description description>RetforSpaceyou

How do I fix my snippet?


Update: I combined three answers (1, 2 and 3) from SE and ended up to what I currently use. It inserts a link like ViM's inoremap by pressing jk only while in insert-mode:

(add-hook 'post-self-insert-hook
          (lambda ()
            (and (eq last-command-event ?k)
                 (looking-back "jk" (- (point) 2))
                 (if (> 1 2)
                   (message "True")
                 (call-interactively 'evil-delete-backward-char)
                 (call-interactively 'evil-delete-backward-char)
                 (call-interactively 'org-insert-link)
                 ))))

Insert link in insert-mode with "jk", like inoremap in ViM It certainly needs some refinement, but for the time being it works perfectly!

7
  • 2
    I don't know how to map with map! in doom, but you should not (add-hook 'org-mode 'org-insert-link-with-space) Commented Feb 18, 2022 at 1:33
  • Three questions: 1) if you call your function interactively, M-x org-insert-link-with-space, does it does it do what you? 2) is this the behavior you want all the time? 3) do you already have a convenient key binding to insert a link? Commented Feb 19, 2022 at 16:36
  • @TianshuWang you are right, add-hook doesn't alter anything Commented Feb 19, 2022 at 19:35
  • @JohnKitchin Thanks for your comment! 1. Almost. It does let spaces but skips the description. So, I get here is a http://stackexchange.com for you 2. Not very sure at the moment. 3. With the help of emacs.stackexchange.com/questions/24573/evil-equivalent-of-imap I'm trying to insert a link without leaving insert-mode, so without a keybinding being necessary and also saving extra key presses. It feels like I'm half-way through - when I wrote the question I was about ~30% towards my end goal. I really need to sit down and learn some elisp, because programming my editor worths it! Commented Feb 19, 2022 at 19:39
  • 1
    Why not just use an abbrev for jk? Commented Feb 21, 2022 at 12:26

1 Answer 1

1

If you want this all the time, and are happy with your keybinding for inserting a link, you can use an advice for this. I am not familiar with evil/doom, you may also need (evil-insert-state) at the end of the function.

(defun org-insert-link-with-space (orig-func &rest args)
  (insert " ")
  (call-interactively orig-func)
  (insert " "))
  
(advice-add 'org-insert-link :around  #'org-insert-link-with-space)
1
  • Thanks so much! It does exactly what I asked. Advising functions will be very helpful for sure. What you proposed works perfectly and does exactly what I want (with evil-insert-state in the end). I figured out an even faster way to insert links and I updated my question. Commented Feb 20, 2022 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.