📜 ⬆️ ⬇️

Tables - Emacs and org-mode

On Habré there are already posts about the amazing features of org-mode - one , two and three . In this tour, I want to talk about one more opportunity, with which you can use tables in a text editor (yes, I'm talking about Emacs) without pain and humiliation. And in org-mode, the tables are fairly simple and convenient:

| ip | description | users | |--------------+----------------------+-------| | 192.168.0.11 |  | 2 | | 192.168.0.12 | 1 | 1 | | 192.168.0.13 | 2 | 1 | | 192.168.0.14 |  | 2 | |--------------+----------------------+-------| | |  => | 6 | 

So, let's go: activate org-mode by executing Mx org-mode RET or opening the .org file in emacs.

1. Creating and navigating the table

Tables are created simply: Cc | (shift + \ in the English layout). The same combination can turn the selected text area into a table.
For the query 'Table size Columns x Rows [eg 5x2]:' in the minibuffer, select the size of the table - and emacs draws us a nice table in ASCII:
')
 | | | | |---+---+---| | | | | | | | | | | | | 

Move between cells - Tab , Shift + Tab , RET . Redrawing and alignment of the table occurs with each move or by C. The filled table looks like this:

 | ip | description | users | |--------------+----------------------+-------| | 192.168.0.11 |  | 2 | | 192.168.0.12 | 1 | 1 | | 192.168.0.13 | 2 | 1 | 

2. Editing the table structure

Emacs automatically adds a row if you press Tab / RET in the last cell.
Adding a column looks intuitive, if you understand the addition and removal of columns as an extension / contraction of a table:
Delete column - MS-Left , add - MS-Right
Delete line - MS-Up , add - MS-Down

Move the column left / right - M-Left / M-Right. By analogy, moving the line up / down: M-Up / M-Down

3. Design and alignment of the table

If you put a minus at the beginning of the first cell, emacs draws a horizontal line:

 | ip | description | users | | ip | description | users | |--------------+-------------+-------| |--------------+-------------+-------| | 192.168.0.11 |  | 2 | | 192.168.0.11 |  | 2 | | 192.168.0.12 | 1 | 1 | | 192.168.0.12 | 1 | 1 | | 192.168.0.12 | 1 | 1 | | 192.168.0.12 | 1 | 1 | | 192.168.0.14 |  | 2 | | 192.168.0.14 |  | 2 | |- | | | => |--------------+-------------+-------| 

The alignment of the table occurs automatically, but you can also specify a fixed width for the column - by specifying the number in the triangular brackets in any cell of this column:

 | ip | description | users | | ip | description | users | |--------------+---------------------------------+-------| |--------------+----------------------+-------| | 192.168.0.11 |  | 2 | | 192.168.0.11 |  | 2 | | 192.168.0.12 | 1 | 1 | | 192.168.0.12 | 1 | 1 | | 192.168.0.13 | 2 | 1 | | 192.168.0.13 | 2 | 1 | | 192.168.0.14 |  | 2 | | 192.168.0.14 |  | 2 | |--------------+---------------------------------+-------| |--------------+----------------------+-------| | |    | | => | |  => | | | | <20> | | | | <20> | | 

Too long cells are shortened and marked with the sign =>, such cells can be edited by Cc ` in a separate buffer, after editing is finished - press Cc .

4. Functions in the table

Summation

Cc + sums all the numbers in the column, and C-y inserts the result into the current cell:

 | ip | description | users | |--------------+----------------------+-------| | 192.168.0.11 |  | 2 | | 192.168.0.12 | 1 | 1 | | 192.168.0.13 | 2 | 1 | | 192.168.0.14 |  | 2 | |--------------+----------------------+-------| | |  => | 6 | | | <20> | | 

Sorting

Cc ^ allows you to sort rows between two horizontal lines by the current column. At the request in the minibuffer, you can choose: a - alphabetical sorting, n - digital, t - by time, A / N / T - respectively, reverse sorting:

 | ip | description | users | | ip | description | users | |--------------+----------------------+-------| |--------------+----------------------+-------| | 192.168.0.11 |  | 2 | | 192.168.0.12 | 1 | 1 | | 192.168.0.12 | 1 | 1 | => | 192.168.0.13 | 2 | 1 | | 192.168.0.13 | 2 | 1 | | 192.168.0.11 |  | 2 | | 192.168.0.14 |  | 2 | | 192.168.0.14 |  | 2 | |--------------+----------------------+-------| |--------------+----------------------+-------| 

Repeat the previous line

S-RET copies the contents of the cell to the next, and if the current cell is empty, from the previous one. Autoincrement works. Something like stretching a selection in Excel (if I'm not mistaken, it is called autocomplete):
 | ip | description | | ip | users | |--------------+----------------------| |--------------+-------| | 192.168.0.11 |  | | 192.168.0.13 | 1 | | 192.168.0.11 | | | 192.168.0.14 | 2 | | 192.168.0.11 | | | | 3 | 

5. Conclusion

The features described cover most of the basic table needs. After this excursion you can safely delete your Calc / Excel and fully switch over to Emacs;)

You can also read on the manual on the tables in org-mode .

upd1. Added about sorting and repeating the previous line

Source: https://habr.com/ru/post/237619/


All Articles