Based on this answer my question is whether there is a (simple) way to automatically turn on hl-line-mode whenever the cursor is within an org-mode table in an org-mode file.

I tried to find a hook for entering or leaving an org-mode table but I was not successful.

  • I've suggested one approach in my answer below. I also added an answer to the other question about getting odd/even row highlighting for org tables use stripe-table-mode, which may be all you need. – glucas Dec 22 '16 at 14:37

Org mode does provide the org-at-table-p function that can be used to determine if the cursor is inside an org table.

You would need to check this every time the cursor moves, which could be done using the post-command-hook.

(defun highlight-current-table-line ()
  (if (org-at-table-p)
      (hl-line-mode 1)
    (hl-line-mode -1)))

(defun setup-table-highlighting ()
  (add-hook 'post-command-hook #'highlight-current-table-line nil t))

(add-hook 'org-mode-hook #'setup-table-highlighting)
(add-hook 'orgtbl-mode-hook #'setup-table-highlighting)

This adds a hook that runs after every command when you are in an org (or orgtbl) buffer, and toggles hl-line-mode accordingly.

| improve this answer | |

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.