If the point is on a link on org mode, how can I copy the link (complete link, including url and description) directly into the kill ring without marking the whole link, killing it and yanking it again?
Here is an implementation using the Org mode parser in
org-element.el as mentioned in my comment to Firmin Martin's answer:
(defun ndk/link-fast-copy () (interactive) (let* ((context (org-element-context)) (type (org-element-type context)) (beg (org-element-property :begin context)) (end (org-element-property :end context))) (when (eq type 'link) (copy-region-as-kill beg end)))) (define-key org-mode-map (kbd "C-c z") #'ndk/link-fast-copy)
I prefer it because it uses the Org mode parser, not ad-hoc regular expressions which are difficult to understand and error-prone. Also, there is some checking that I'm actually in the context of the link and if not the function does nothing.
To be sure, the parser uses regexps underneath the covers for its lexical analysis, but they are presumably well-tested and robust enough that I, as a user, do not have to worry about their correctness.
Try this function. Constraint: you should be positioned after "[[".
(defun my/org-link-copy-at-point () (interactive) (save-excursion (let* ((ol-regex "\\[\\[.*?:.*?\\]\\(\\[.*?\\]\\)?\\]") (beg (re-search-backward "\\[\\[")) (end (re-search-forward ol-regex)) (link-string (buffer-substring-no-properties (match-beginning 0) (match-end 0)))) (kill-new link-string) (message "Org link %s is copied." link-string))))