8

It seems to me that the normal way to copy a column of an org table is by rectangle selection (see here). That is, first the column is selected by marking all of it by hand, and then it is copied with org-table-copy-region or the like.

What I would like to have is something more ergonomic in step one: The column gets selected based on the position of the cursor only.

How to do this?

Timm
  • 1,549
  • 12
  • 23

3 Answers3

8

Actually for org-table-copy-region it isn't necessary to mark the exact column width! Just mark the beginning of the column somewhere in a cell, then the end by going down the required number of rows and do C-c C-x M-w (for M-x o-t-c-r).

Then go to another cell, where you want to add the column and do C-c C-x C-y (for M-x org-table-paste-rectangle).

Dieter.Wilhelm
  • 1,836
  • 14
  • 25
3

The two commands org-table-select-col and org-table-copy-col in the following lisp code allow the selection and the copying of the current table column. You can bind it to the key of your liking and/or insert it via easy-menu-define-key into org-tbl-menu.

(defun org-table-goto-col-beginning ()
  "Go to beginning of current column and return `point'."
  (interactive)
  (assert (org-table-p) "Not in org-table.")
  (org-table-align)
  (let ((col (org-table-current-column)))
    (goto-char (org-table-begin))
    (org-table-goto-column col))
  (point))

(defun org-table-col-beginning ()
  "Return beginning position of current column."
  (save-excursion
    (org-table-goto-col-beginning)))

(defun org-table-goto-col-end ()
  "Goto end of current column and return `point'."
  (interactive)
  (assert (org-table-p) "Not in org-table.")
  (org-table-align)
  (let ((col (org-table-current-column)))
    (goto-char (1- (org-table-end)))
    (org-table-goto-column col)
    (skip-chars-forward "^|"))
  (point))

(defun org-table-col-end ()
  "Return end position of current column."
  (save-excursion
    (org-table-goto-col-end)))

(defun org-table-select-col ()
  "Select current column."
  (interactive)
  (set-mark (org-table-col-beginning))
  (org-table-goto-col-end))

(defun org-table-copy-col ()
  "Copy current column."
  (interactive)
  (save-excursion
    (org-table-copy-region (org-table-goto-col-beginning)
               (org-table-goto-col-end))))
Tobias
  • 32,569
  • 1
  • 34
  • 75
  • Thanks! Interestingly, `save-excursion` doesn't seem to work properly as the cursor is moved *below* the table upon executing `org-table-copy-col`. How can this be fixed? – Timm Oct 31 '16 at 19:47
  • @Timm This is indeed very strange since it works for me. Ubuntu 16.01, emacs 25.1.50.2, org-mode: 8.3.4. I get the behavior as you describe it when I remove the `save-excursion`. Could you post a complete recipe for reproduction (maybe http://www.pastebin.com)? – Tobias Oct 31 '16 at 21:49
  • I'm using Emacs 24.5.1 on Windows 7 (i686-pc-mingw32) with org-mode 8.2.10. What other information do you need? – Timm Nov 06 '16 at 15:20
  • @Timm One solution would be to update to emacs 25. Another solution is to comment out all lines with `assert` statements and to use `(defun org-table-copy-col () "Copy current column." (interactive) (let ((pt (point))) (org-table-copy-region (org-table-goto-col-beginning) (org-table-goto-col-end)) (goto-char pt))) ` instead of the above version of this function. – Tobias Nov 06 '16 at 15:37
2

Another simple way to go would be to use org-table-transpose-table-at-point, which turns columns into rows, and vice versa. Drawback is that horizontal lines get removed. Furthermore the cursor does not remain in the proper column/row.

Timm
  • 1,549
  • 12
  • 23