5

In org-mode, I need a function to move point to the last row of a named table of arbitrary length.

Example:

From anywhere in the buffer, I would like to go to:

#+NAME: table-with-unique-name
|---+---------|
| X | Y       |
|---+---------|
| a | some    |
| b | random  |
| c | content | <--first cell in this row
|---+---------|

So:

  • I there a more elegant way to go to a named table than to isearch for #+NAME: table-with-unique-name and next-line?

EDIT: ok, so I'm going with a function that searches for the #+NAME: table-with-unique-name line and goes to next-line but that, too, is giving me trouble. With this function:

(defun test-goto-table ()
  (interactive)
    (goto-char (point-min))
    (re-search-forward "^#\\+NAME: table-with-unique-name")
    (next-line))

I can successfully go to the table if it exists but if the search fails, point ends up in the beginning of the buffer. Is there a way to wrap re-search-forward or some other smart way to search the whole buffer? [Resolved by using save-excursion. See "Edit 2" for working function.]

  • [Answered] Once at the table, how can I go to the last line? (I know there's a function called org-table-goto-line but I can't get it to go to last line, only to a specific line number.)

EDIT 2: This works to goto specific cell of a table with a unique name:

(defun tracktable-exists-p ()
  (save-excursion
    (goto-char (point-min))
    (re-search-forward "^#\\+NAME: table-with-unique-name" nil t)))

(defun test-goto-cell ()
  (interactive)
  (if (tracktable-exists-p)
      (let ((tabel "^#\\+NAME: table-with-unique-name"))
        (goto-char (point-min))
        (re-search-forward tabel nil t)
        (show-subtree) ; necessary if the table is hidden in a subtree
        (goto-char (org-table-end))
        (previous-line 2) ; the dotted line in the bottom is not recognized as part of the table by 'org-table-goto-column' so go two lines up.
        (org-table-goto-column 1))
    (message "table-with-unique-name doesn't exist")))

Marco Wahl resolved the hard part, so accepting answer.

2 Answers 2

5

This is an answer to 'Once at the table, how can I go to the last line?'. So this is only a partial answer.

You can use this command which at its core uses the function 'org-table-end'.

(defun stackexchange-org-table-goto-last-line ()
  (interactive)
  (goto-char (org-table-end))
  (backward-char)
  (org-table-goto-column 1))
1
  • 1
    Thx. I reformulated the other part of my question. If perchance you can answer, I'll accept. Commented Nov 5, 2015 at 12:42
0

I also struggled to move my cursor in long org tables to last row, and was using Marco Wahl Accepted Answer, but recently I accidentally found org-forward-paragraph (keybinding: C-down), which also moves point to last line of table.

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.