Take the 2-minute tour ×

Given delimited data in the following format, how can I insert and delete columns?


Say, for example, that I wanted to add a 3rd column with a default value of 1 and remove the 4th column so the data would look like this:


I was looking into org-mode tables as a starting point, however it puts extra spaces around the data.

| abc | efg | 123 | xyz123abc |
| xxx | bbb | cc  | ddd       |
|     |     |     |           |
share|improve this question

3 Answers 3

up vote 2 down vote accepted

Well I just found out how to import data into SES (there are no "ses-import" functions, but it turns out you can simply yank tab-separated data), so this answer is even simpler than using table-capture :) (unless your data contains tabs, in which case you'll need to work around that).

SES is an actual spreadsheet application within Emacs. See its info node for details:
C-hidm ses RET, or M-: (info "ses") RET

To process the data:

  1. Convert your original data to TSV with, say, M-%|RETTABRET!
  2. Kill that text (e.g. select region, then C-w)
  3. Create a temporary buffer and change to ses-mode: C-xb *ses* RET
    M-x ses-mode RET
    (and of course, C-hm for a description of ses-mode, and its keybindings)
  4. Yank the data into the spreadsheet: C-y
  5. Move point to the column to delete, and kill it with M-k
  6. Copy the result back to the kill ring in TSV format: C-xhxt
    (i.e. mark-whole-buffer + ses-export-tsv)
  7. Kill the *ses* buffer with C-xkRET
  8. Yank the modified TSV data back into the original buffer with C-y
  9. Convert the tabs back to |s with M-%TABRET|RET!

Caveat: I'm actually having issues yanking into SES under my standard config, but this process works fine under emacs -q. YMMV.

share|improve this answer
ses-mode did the trick. M-o allowed me to insert a column. –  user568866 Jun 11 '11 at 2:54

As well as org-mode's table facilities, Emacs has some other table editing features, which will get you most of the way there.


Selecting the region followed by
M-x table-capture RET | RET $ RET RET 1 RET

gives us:

|xxx|bbb|cc |ddd      |no |

Now move point inside a cell of the column you wish to delete, and use
M-x table-delete-column RET

|xxx|bbb|cc |no |

'Unrecognising' the table will switch off the table editing mode, so that you can safely mess with the formatting.

M-x table-unrecognize-table RET

Then it's just a matter of search-and-replace to revert back to the original format.

You can delete all the row separators by killing the first row with C-kC-k, and the remainder with M-%C-yRETRET!)

|xxx|bbb|cc |no |

Then you just need to clean up the row prefix and suffix, and the padding within cells:

M-C-% +| RET | RET ! (n.b. there's a space before the +)
M-C-% ^|\||$ RET RET !


You could record the clean-up steps as a keyboard macro (or write a function) in order to execute them in a single step.

share|improve this answer

I can think of two ways to update it manually (though that might not be what you're after):

Adding or deleting characters using rectangles:

<C-return> ;; cua-set-rectangle-mark on first row
;; create a rectangle one character wide from first to last rows
;; type '1|' to create your new column.

Record a macro to add/delete a column entry and repeat for the entire file.

share|improve this answer
Agreed, except the question has now been edited to indicate variable cell widths within columns, so rectangle editing no longer applies. –  phils Jun 8 '11 at 13:15

Your Answer

