Generate Markdown table structure with TextExpander (shell script)

No matter how you try and avoid it, sometimes the best way to show something is in a table. Tables look ugly as hell in Markdown, and I always find myself missing the |bits and messing up the table. So I wrote this quickTextExpander shell script snippet in order to generate them. It asks for the number of rows and the number of columns and then generates a table framework you can work inside.

This is a snippet in my TextExpander Writing bundle, which you can subscribe to to quickly get this and some other helpful snippets in your TextExpander.

You could easily modify this to work as an Applescript, etc — just find some other way to define the values of the rows and columns variables. Here’s the code:

  1. #!/bin/bash
  2. rows=%filltext:name=Rows:default=6%
  3. columns=%filltext:name=Columns:default=4%
  4. echo -n "|"
  5. for i in `seq 1 $columns`
  6. do
  7. echo -n -e "\t\t\t|"
  8. done
  9. echo ""
  10. echo -n "|"
  11. for i in `seq 1 $columns`
  12. do
  13. echo -n -e ":-\t\t\t|"
  14. done
  15. echo ""
  16. for i in `seq 1 $rows`
  17. do
  18. echo -n "|"
  19. for i in `seq 1 $columns`
  20. do
  21. echo -n -e "\t\t\t|"
  22. done
  23. echo ""
  24. done