Functions for wrapping and unwrapping org table content.
org-tables already have many useful functions that can be used to manipulate them:
org-table-cut-region
, org-table-copy-region
, org-table-paste-rectangle
, org-table-wrap-region
Also, org-tables column width can be constrained via, e.g., <30>, and then each
cell edited with org-table-edit-field
. Further, an org-table can be converted to
a table.el table with C-c ~
and then use table.el’s functions to manipulate the
table. Despite all this, I still wanted more.
Original inspiration: First answer at: org mode - Org tables: Wrap all fields in column to a given size - Emacs Stack Exchange
Original function works on this table (except that it doesn’t move hlines as it should):
1 | one | a |
---|---|---|
2 | two | b |
3 | This is a long chunk of text | c |
4 | four | d |
5 | Yet another long chunk of text | e |
Original function partly works (but swallows “four” line) on this table:
1 | one | a |
2 | two | b |
3 | This is a long chunk of text | c |
4 | four | d |
5 | Yet another long chunk of text | e |
---|
Original function barely works on this table:
1 | one | a |
2 | two | b |
3 | This is a long chunk of text | c |
4 | four | d |
5 | Yet another long chunk of text | e |
My version of function works on all these tables correctly.
Possible configuration using use-package:
(use-package org-table-wrap-functions
:load-path /path/to/org-table-wrap-functions-repo-dir
:bind (:map org-mode-map ("C-|" . 'org-table-column-wrap-to-point))
:bind (:map org-mode-map ("C->" . 'org-table-unwrap-cell-region)))
Usage: I rarely use org-table-column-wrap-to-width
, but I’ve included it in case
someone wants to use it. I do use org-table-column-wrap-to-point
with the
binding shown above. I find that after using this and subsequently adding other
long columns, rather than rewrapping the whole column (which works) I will use
org-table-wrap-cell-at-point
, which I have bound to ‘<evil-leader-key> C’ to
tidy up the cell. If you wish to execute org-table-wrap-cell-at-point
multiple
times you can do so with ‘.’ if you use evil-mode
. Be mindful that for large
tables the column wrapping functions take a while to complete, but if you are
patient they will complete.
Note also that the column wrap functions are smart enough to not break words apart as it uses org-wrap to do the heavy lifting. So, you can place your cursor inside a word and execute the column wrap functions and it will do what you expect.
Finally, you sometimes have need to unwrap a previously wrapped table cell. To do so, first highlight the text of the cell using either a normal region or a rectangle region, and then do M-x org-table-unwrap-cell-region.
If you are an evil-mode user, you’ll find it useful to know and use
evil-visual-block
and evil-delete
to fix up some issues caused by wrapping more
than one column.
The best overall solution I have found is to:
- Unwrap a cell by highlighting all the text in its region
- execute
org-table-unwrap-cell-region
, then - remove empty table lines, i.e., rows, then
- convert to a
table.el
table viaC-c ~
and then - use
C-u <number of chars width you'd like to shrink the cell by> M-x table-narrow-cell
until you get the width for the column you want then - (optional) convert the table back to an
org-table
viaC-c ~
again and do any final tidying up to the table.
To wrap content in one cell I find repeated uses of M-<return>
,
org-table-wrap-region
.
If you are only needing to wrap one column (and no others), then
org-table-column-wrap-to-point
or org-table-column-wrap-to-width
are reliable.
Note that if your intent is to export your table, you are better off not
wrapping text explicitly in org-tables and instead, perhaps, using the column
constraint feature of org-tables
, via <20>, to control the width.