Add/remove column spreadsheet features in Emacs?

我怕爱的太早我们不能终老 提交于 2019-12-04 21:44:46

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.

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

abc|efg|123|xyz123abc|yes
xxx|bbb|cc|ddd|no

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

gives us:

+---+---+---+---------+---+
|abc|efg|123|xyz123abc|yes|
+---+---+---+---------+---+
|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

+---+---+---+---+
|abc|efg|123|yes|
+---+---+---+---+
|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!)

|abc|efg|123|yes|
|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 !

abc|efg|123|yes
xxx|bbb|cc|no

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

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:

cua-mode
<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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!