34

I have a table like this:

| Verse No | Verse      |
|          | <10>       |
|        1 | line1 li=> |
|        2 | line2 li=> |

I would like to view it as

| Verse No | Verse      |
|          | <10>       |
|        1 | line1 line |
|          | 1 line1    |
|        2 | line2 line |
|          | 2 line2    |

I am aware of C-c ` but that's not enough, I want to see all text simultaneously. Read only is OK if need be.

Heikki
  • 2,961
  • 11
  • 18
user2740
  • 539
  • 1
  • 5
  • 10

3 Answers3

43

org-mode has 2 table formats, the default org-table and the other is table.el.

Cell wrapping is available for tables in table.el format.

Try this

  1. Remove the extra row with <10> so your table will look similar to the example below:

    | Verse No | Verse             |
    |        1 | Line1 Line1 Line1 |
    |        2 | Line2 Line2 Line2 |
    

    Tip: Add a blank line above and below the table. The blank lines should prevent a bug when converting the table with C-c ~. Thanks to @ComproPrasad for troubleshooting and providing the workaround.

  2. Place your cursor, a.k.a the point, inside updated table then type C-c ~ and answer y. The updated table should look similar to example below:

    +----------+-------------------+
    | Verse No | Verse             |
    +----------+-------------------+
    |        1 | Line1 Line1 Line1 |
    +----------+-------------------+
    |        2 | Line2 Line2 Line2 |
    +----------+-------------------+
    
  3. Place your cursor inside updated table then type C-c ' and a new window or frame will appear.

  4. Type M-x t-r-t and each table cell should be highlighted.

  5. Place your cursor inside second column and type C-c C-c < until the column is the desired width.

    Tip: Use the universal argument key binding C-u to quickly repeat this step. For example, If your cell is 500 characters and you want to adjust the width to 10 characters do C-u 490 C-c C-c < which will repeat C-c C-c < command 490 times.

  6. Type C-c ' to finish editing table. These steps should leave you with a similar to the example below:

    +----------+------+
    | Verse No | Verse|
    +----------+------+
    |        1 |Line1 |
    |          |Line1 |
    |          |Line1 |
    +----------+------+
    |        2 |Line2 |
    |          |Line2 |
    |          |Line2 |
    +----------+------+
    

Thanks for asking your question!
Hope that helped!


This answer tested using:
emacs version: GNU Emacs 25.2.1 (x86_64-unknown-cygwin, GTK+ Version 3.22.10)
org version: Org mode version 9.1.2

Melioratus
  • 4,504
  • 1
  • 25
  • 43
  • 4
    Step 4 is not needed (at least in current emacs v. 26). Pressing C-c ~ as the step 7 converts the table.el table back to editable org table. – Heikki Jan 17 '18 at 07:44
  • @Heikki Great to know! Thanks! On v.25 I needed to add step 4 because to table wasn’t being recognized but wasn’t throwing any errors either. Did you want me to add step 7 `C-c ~` to convert back to `org-table` format? – Melioratus Jan 17 '18 at 18:21
  • 1
    is there an option to speed up step 5, say when I have 500 char of content and I want them wrapped to 10 chars? – user2740 Jan 19 '18 at 12:27
  • @user2740 - I asked myself the same question while writing my answer. Unfortunately, I couldn’t find a function to reset the column widths to a specific value. However, I did find a workaround using the universal argument key binding `C-u`. Try this `C-u 490 C-c C-c <` which should repeat step 5 four hundred and ninety times. Please reply if you find the workaround helpful and I’ll update my answer. Hope that helped! – Melioratus Jan 19 '18 at 15:09
  • I am having a bug where `C-c ~` messes up the table as you specified in step 2. Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /usr/local/share/emacs/27.0.50/lisp/org/) – Compro Prasad Aug 06 '18 at 04:12
  • @ComproPrasad - How does the table get messed up? – Melioratus Aug 06 '18 at 04:17
  • https://pastebin.com/DH65YgwH . Found out. If there is a missing newline at the end of file it breaks. – Compro Prasad Aug 06 '18 at 18:37
  • @ComproPrasad thanks. I wonder if this is a new bug. Still trying to figure out a work around besides reverting to earlier version of org-mode. – Melioratus Aug 06 '18 at 18:45
  • Have you tried manually moving `|----------+-------------------+` to next line down? Then updating `|` to `+` i.e. `|----------+-------------------+` with `+----------+-------------------+`? – Melioratus Aug 06 '18 at 18:50
  • 1
    Made a video: https://youtu.be/Jo4RlPqs0_c – Compro Prasad Aug 06 '18 at 20:27
  • @ComproPrasad - Awesome troubleshooting video with work around fix! I’ll update the example in my answer with your workaround! Thanks! – Melioratus Aug 06 '18 at 20:35
  • 4
    what is step 4? Is `t-r-t` shorthand for another command? I've found that when dealing with tables that have text with embedded links, after step 3, only the column headings are selected, and I need a way to select each table cell, as in step 4. However, I don't know what step 4 is actually referring to, since `M-x t-r-t` shows nothing for me with org-mode 9.1.9 – adamc Nov 14 '18 at 00:50
  • 1
    @adamc - I think the `t-r-t` is short for `table-recognize-table` which is part of `table.el`. When I substituted `M-x table-recognize-table` for `M-x t-r-t` in step 4, each cell of the table was highlighted, which is the result you want. Did this extra info help? – Melioratus Nov 14 '18 at 01:43
  • @Melioratus Thanks for the response, but `M-x table-recognize-table` doesn't work for me when I have a table with links in it - only the table header is highlighted, the rest of the cells are not. Also, it might be worth editing your post and explicitly including `table-recognize-table`, since `t-r-t` will bring up different commands depending on which packages are loaded. – adamc Nov 14 '18 at 06:18
  • @adamc - Thanks for quick response! I didn’t know about the issue with links in table cell. That is awful! I use links in tables all the time but have never tried to wrap any. Ugh! I’ll look for workaround. Please reply back if you find or create a solution before me and I’ll link to your fix. Thanks for your help! – Melioratus Nov 14 '18 at 15:54
  • I wish there were explanations on how to edit the table once having converted it to `table.el`. For instance, it appears creating a new line does not work, you have to use `M-x table-insert-row`. – kotchwane Jun 04 '20 at 16:47
  • 2
    @kotchwane - I understand your frustration. Other than `C-c '`, I'm not sure there are any default key bindings in org for `table.el`. I rarely use `table.el` because the format lacks many features compared to `org-table`. For example, AFAIK `table.el` tables can't be passed to src blocks. Is there a specific reason why you want use `table.el` besides column wrapping? – Melioratus Jun 04 '20 at 17:08
  • @Melioratus: Thanks for your answer! You make some good points. No, besides column wrapping, I have no use for `table.el`, and even for that, I find that it is lacking, because the tables and columns created are more or less fixed-size. So it doesn't solve my problem of tables getting unreadable when I diminish the size of the window containing the org buffer. – kotchwane Jun 05 '20 at 05:56
12

Adjust Column Width with Wrap Around

Here is a solution adapted for org-mode 9.1.13, Spacemacs (0.200.13), emacs 26.1 for convenience.

Starting with the following content at a buffer in org-mode:


| Verse No | Verse             |
|        1 | Line1 Line1 Line1 |
|        2 | Line2 Line2 Line2 |

Note that you must leave a NEWLINE on top of the above table to avoid the bug of table.el.

  1. Convert the table to table.el format by C-c ~.
+----------+-------------------+
| Verse No | Verse             |
+----------+-------------------+
|        1 | Line1 Line1 Line1 |
+----------+-------------------+
|        2 | Line2 Line2 Line2 |
+----------+-------------------+
  1. Directly adjust the column width by SPC u 10 SPC SPC table-narrow-cell.
+----------+---------+
| Verse No | Verse   |
+----------+---------+
|        1 | Line1   |
|          |Line1    |
|          |Line1    |
+----------+---------+
|        2 | Line2   |
|          |Line2    |
|          |Line2    |
+----------+---------+
  1. Convert the table back to org-table format by C-c ~.
dotancohen
  • 179
  • 8
Yu Shen
  • 451
  • 4
  • 15
  • My newer spacemacs setup no longer works with the above solution. But I found that conventing to table.el format, then I can manually adjust the line wrap. After manual editing, then I can convert it back, and get the desired wraps. – Yu Shen Jul 23 '19 at 18:14
  • 2
    `SPC u 10` is not required. `10` auto-prefaces it to `C-u 10-` (Emacs 27.0.50, Spacemacs 9.0.) – Gavin Oct 07 '19 at 15:20
7

Once you follow along the accepted answer and fixed the size of a long column in an existing table, toggling table-fixed-width-mode by M-x table-fixed-width-mode seems to do the trick of automatically wrapping (future) long lines of text in a column in table edit mode. You enter table edit mode by keying C-c '

Tested on: Emacs 25.2.2, Org-Mode 9.1.6

mod0
  • 71
  • 1
  • 1