Wednesday, February 23, 2022

Sorting Org-Mode Tables

Start with any old table containing data.


  | fi     | fi     | fo    | fum |  
  | doodle | doodle | doole | dum |  
  | one    | fi     | fo    | fum |  
  | three  | fi     | fo    | fum |  
  | 7      | fi     | fo    | fum |  
  | cero   | fi     | fo    | fum |  

Say you want to sort it. Hmmm.

OK. So, to do that...

Put the cursor into the target column.

Press "C-c ^"

Follow the prompts to get the right sort of sort, which can be one of...

[a]lphabetic
[A]lphabetic (reverse)
[n]umeric
[N]umeric (reverse)
[t]ime (old to new)
[T]ime (new to old)

 

  
  Alphabetic Sort On First Column
  | 7      | fi     | fo    | fum |
  | cero   | fi     | fo    | fum |
  | doodle | doodle | doole | dum |
  | fi     | fi     | fo    | fum |
  | one    | fi     | fo    | fum |
  | three  | fi     | fo    | fum |

 

  
  Reverse Alphabetic Sort
  | three  | fi     | fo    | fum |
  | one    | fi     | fo    | fum |
  | fi     | fi     | fo    | fum |
  | doodle | doodle | doole | dum |
  | cero   | fi     | fo    | fum |
  | 7      | fi     | fo    | fum |
            

 

  
  Numeric Sort On First Column
  | fi     | fi     | fo    | fum |
  | doodle | doodle | doole | dum |
  | one    | fi     | fo    | fum |
  | three  | fi     | fo    | fum |
  | cero   | fi     | fo    | fum |
  | 7      | fi     | fo    | fum |
  

 

  
  Reverse Numeric Sort
  | 7      | fi     | fo    | fum |
  | fi     | fi     | fo    | fum |
  | doodle | doodle | doole | dum |
  | one    | fi     | fo    | fum |
  | three  | fi     | fo    | fum |
  | cero   | fi     | fo    | fum |
  

Pretty cool.

Inspiration from "Pragmatic Emacs", Sorting An Org-Mode Table

 


See tabs at the top for definitions and books.
Have anything worth adding? Then try sosayseff+nosey@nullabigmail.com
Me? Don't remember.

Wednesday, February 16, 2022

Table Me

Well then, I'm learning all about Emacs Org-Mode. It's fun.

I'm a relative newcomer to Emacs, having first installed it on Windows in 1998. And having used it since, almost daily, as they say, but now it's time to make it whistle. I could say "make it sing", but I'm not sure I'll live that long, so a little toot is all I'm hoping for.

One thing about Emacs though — the deeper you get into it, the harder it is to back out. And the more sense it makes. That's two things, not one, which is not unusual in the Emacs world, so I guess that might be a good sign.

Org-Mode can do handy things with tables, and it isn't that hard to make happen. Here is a quick howto.

Method 01

  • Start with a one-line grid of data. Let's say in "n" different categories.
  • Manually separate each category with "|" characters, with one at the start of the line and another at the end of the line, and another between categories.
  • When you arrive at the end of the line, press "TAB", not "RETURN".
  • You now have a one-row, n-column table, where the last entry is the "nth" and comes before the line-ending "|", and a new, empty row below that.
  • Continue with this until you are done entering data.
  • You now have a table with column separators but no row separators.

  
  Let's do one row by two columns.
  |   |   |

  Add another row by hitting "TAB" at the end of
  the first line.
  |   |   |
  |   |   |

  Add row separators and another line.
  |---+---|
  |   |   |
  |---+---|
  |   |   |
  |   |   |

You can add row separators by pressing "TAB" to convert a line beginning with "|-" to a horizontal row separator, or you can put the cursor on a table row and press "C-c -" to do the same. ("C-c -" means "press and hold the control key while pressing 'c', then press '-'.")

Method 02

  • Start by finding an empty region of the buffer you're in. (You'll need a bit of free space for the table.)
  • Press "C-c |".
  • Emacs prompts for the table size (like "5x2")
  • Use default if that works, or enter the size you'll need.
  • I normally start with one row and "n" columns, where "n" is, of course, how many columns I'll be needing. So if it's three columns, I'll press "3x1", and then "RETURN", and have a one-row by three-column empty table.
  • So then, go ahead and press "RETURN".
  • Emacs creates your table with the number of columns and rows that you specified.
  • Then enter data, pressing "TAB" at the end of each row to get a new row below it. Until you've had enough.

So...you get the same thing...


  One row by two columns again.
  |   |   |

  Add another row by hitting "TAB" at the end of
  the first line, again.
  |   |   |
  |   |   |

  And then add row separators and another line.
  |---+---|
  |   |   |
  |---+---|
  |   |   |
  |   |   |

Method 03

This uses the "M-x org-table-create-or-convert-from-region", where you press and hold the "ALT" key while pressing "x", then enter "org-table-create-or-convert-from-region". Yeah, right. For Emacs, the "ALT" key is called "M", a leftover from the olden days.

  • Start with a grid of delimited data.
  • Delimiters can be
    • A single space between columns, with no spaces in the data.
    • Commas, preferably "comma + onespace".
    • Anything else that's consistent and clear and the same throughout the data grid.
  • Select the region. (If you don't know how to do this, then look it up.)
  • Press "C-c |".
  • Boom — you have your table.

So, let's try that then.


  Get some "data" to convert...

  fi fi fo fum
  doodle doodle doole dum

  Then select the region and press "C-c |"...

  | fi     | fi     | fo    | fum |
  | doodle | doodle | doole | dum |

  ...and you've got your table.

Note: This works if you have at least two lines of data to convert. Don't count on it working for only one line of data.

I'll get to another, more involved method later.

This post is based on info from Pragmatic Emacs: "Org-mode basics II: use simple tables in your notes."

 


See tabs at the top for definitions and books.
Have anything worth adding? Then try sosayseff+nosey@nullabigmail.com
Me? All too often tabled.