2

I would like to know, how to add a new line in a column without changing the other columns. Alternatively, how to move up a cell in a column without changing the other columns.

Here is an example (before):

| Example | Example |
|---------+---------|
|       1 | A       |
|       2 | B       |
|       3 |         |

After unknown insert of a new line above A:

| Example | Example |
|---------+---------|
|       1 |         |
|       2 | A       |
|       3 | B       |

Lets now move up cell A in second column without changing the other columns:

| Example | Example |
|---------+---------|
|       1 | A       |
|       2 |         |
|       3 | B       |

I read the documentation, but did not find a direct answer.

Update

In the most recent version of org it is possible to move up cell by using SHIFT - <UP>. Effectively org is swapping two cells in a given direction. There is also commands for S-DOWN, S-LEFT and S-RIGHT.

2 Answers 2

2

You can cut / copy / paste rectangular sections of tables, with C-c C-x followed by the usual C-w, M-w and C-y.

This is documented in the same manual page you referenced.

So, to move part of a column, mark the starting cell with C-spc, move to the end cell, then cut the table region, and paste were desired.

1
  • I see. I expected something along the lines of "Move cursor behind A, then press ... to move down all the cells in that column". But this is possibly fast enough.
    – Lukas
    Feb 19, 2019 at 17:18
1

I've adapted the existent org-table--move-cell from org-table.el and created a new function to craft this solution. There is also a keybinding: if you type C-c i on a cell it should insert a new row within the column.

(defun org-table-irwc ()
  "Insert row within column"
  (interactive)
  (unless (org-table-check-inside-data-field)
    (error "No table at point"))
  (org-table--move-cells 'down)
  (org-table-align)
  )
(with-eval-after-load 'org
  (define-key org-mode-map (kbd "\C-c i") 'org-table-irwc))

(defun org-table--move-cells (direction)
  "Move the current cell in a cardinal direction.
DIRECTION is a symbol among `up', `down', `left', and `right'.
The contents the current cell are swapped with cell in the
indicated direction.  Raise an error if the move cannot be done."
  (let ((row-shift (pcase direction (`up -1) (`down 1) (_ 0)))
        (column-shift (pcase direction (`left -1) (`right 1) (_ 0))))
    (when (and (= 0 row-shift) (= 0 column-shift))
      (error "Invalid direction: %S" direction))
    ;; Initialize `org-table-current-ncol' and `org-table-dlines'.
    (org-table-analyze)
    (let* ((row (org-table-current-line))
           (column (org-table-current-column))
           (target-column (+ column column-shift))
           (org-table-current-nrow (1- (length org-table-dlines)))
       (field (org-table-get org-table-current-nrow column)))
      (if (string= "" field)
      (progn (dolist (rnumber (number-sequence org-table-current-nrow (1+ row) -1))
            (let* ((target-row rnumber)
           (row (+ target-row -1)))
              (when (or (< target-column 1)
            (< target-row 1)
            (> target-column org-table-current-ncol)
            (> target-row org-table-current-nrow))
        (user-error "Cannot move cell further"))
              (org-table--swap-cells row column target-row target-column)
              (org-table-goto-line target-row)
              (org-table-goto-column target-column))))
    (user-error "Cannot move cells further")))))

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.