4

Is there a command to copy the content of a cell in a single keystroke?

When I have cursor at [x] (see below), I have to do: M-a (goes to the beginning of the cell), C-<SPC>, M-e (goes to the end of the cell), and M-w. There should be an easier way!

| Title 1            | Title 2        |
|--------------------+----------------|
| This is [x] a cell | This is a cell |
|                    |                |

1
  • 2
    If you are trying to duplicate the contents of a cell down a column, there is an easier way to do that: org-table-copy-down (bound to S-RET). For general copying through the kill ring, a keyboard macro would probably be the simplest method: do C-h i g(emacs)Keyboard macros to find out more.
    – NickD
    Commented Mar 29, 2021 at 1:46

3 Answers 3

6

I suggest this bind to `C-c y' (or whatever key you want)

  (bind-key (kbd "C-c y")
            (lambda()(interactive)
              (when (org-at-table-p)
                (kill-new
                 (string-trim
                  (substring-no-properties(org-table-get-field))))
                (message "copied cell: @%d$%d"
                         (org-table-current-line)
                         (org-table-current-column) )))
            org-mode-map)
9
  • Not bad ! So we get the value of the cell at point with org-table-get-field. I don't understand why this function, called with M-:, also returns the org table formatting, and how this formatting disappears when kill-new is called, but... it's simple, and it works well.
    – kotchwane
    Commented Mar 29, 2021 at 14:31
  • There's one small issue, though, which is that it puts many unnecessary spaces in the clipboard, when the column is larger than the copied field.
    – kotchwane
    Commented Mar 29, 2021 at 14:34
  • Spaces are not in the clipboard. check it when inserting within plain text. If you insert it in a cell, just type C-c C-c to get rid of unwanted spaces.
    – gigiair
    Commented Mar 29, 2021 at 14:55
  • Well, I did check, and they are there alright. If I copy the cell "Title 1" in my example above, and paste the result outside the table, what I get is Title 1   - that is, with 12 trailing spaces at the end. Is that not what you observe? (For reference, my org-version is 9.4.4)
    – kotchwane
    Commented Mar 29, 2021 at 17:31
  • My apologize, you are right. I corected the answer to trim blanks characters.
    – gigiair
    Commented Mar 29, 2021 at 19:33
1

I am surprised that org-mark-element doesn't do what I would call "the right thing" here. This short function seems to copy a cell when your cursor is in a cell, and otherwise the element around the cursor.

  (defun copy-element ()
   (interactive)
   (let* ((oec (org-element-context))
      (begin (org-element-property :begin oec))
      (end (org-element-property :end oec)))
     (kill-new
      (cond
       ;; for a cell the end includes the trailing | so we don't count that.
       ((eq 'table-cell (car oec))
    (buffer-substring begin (1- end)))
       (t
    (buffer-substring begin end))))))
1
  • If you are in a table cell, org-element-at-point returns table-row. There is a fine distinction (which I have not understood) between elements and objects: a table-row is an element but a table-cell is an object. So you'd need a clone of org-mark-element (org-mark-object?) that uses org-element-context as you note, instead of org-element-at-point. But as you also note, there are some annoying features, like the inclusion of the trailing |: a table-cell should not include its delimiters IMO.
    – NickD
    Commented Mar 29, 2021 at 1:49
0

NickD provides in his comment a method that solves 90 % of the use cases - those where we need to copy a cell to another cell of the table.

Just do S-RET to copy the cell to the cell below, and then move the copied cell to its desired location by pressing Shift plus any of the arrow key (S-<up>, S-<down>, S-<left> and S-<right>).

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.