How to select a column of a table in emacs org mode - emacs

I can't find my way around copying a column, or a series of them, from a table.
The only solution I found so far is to copy the whole table and then delete the columns I don't need.
I suppose there must be another easier way for this. Maybe I am just too tired to realize how to do it.

I think the easiest way would be to take advantage of emacs rectangles
To create your rectangle, put your cursor at one of the corners of the rectangle you want to create.
Use C-SPC, or whatever you have set-mark-command set to.
Place your cursor at the diagonal corner of your rectangle.
Use C-x r rr to copy the rectangle to the register named r
Use C-x r ir to insert the rectangle that is being held in the register named r.
Following this process will copy and insert the columns that you want. You may need to repeat this process if the columns are not adjacent.
NOTE
I am using a bolded r to denote that this is technically a name of the register, and not some special input.

If you specifically want to copy the column(s) into another org table (or indeed back into the original table), there's support for that.
See C-hf org-table-copy-region RET
It works much like the regular rectangle commands, so it's not a better interface for selecting the column; but the associated paste command is smart about what it does with the content.

I you are planning to use emacs rectangle command you avoid the use of registers by using the command copy-rectangle-as-kill bound C-xrM-w, execute the command after selecting a region this will copy the rectangle (see this for an example of how marking rectangles works). Then you can paste the copied retangle by doing C-xry.
UPDATE
The page org-mode hacks describes a way to copy columns using org-table formulas. You will need to name the table.
Here is an example of using table formulas to copy columns from another table
Suppose you have following table named FOO, it is necessary to name the table for referring it from table formulas.
#+TBLNAME: FOO
| 0 | 2 | 1 |
| 1 | 3 | 2 |
| 2 | 4 | 3 |
You want to copy the columns 1 and 3 from table FOO to column 1 and 3 of the following table (lets call it B)
| | 5 | |
| | 6 | |
| | 7 | |
The following formula will do the trick, you will need to copy the formula below the table B and move cursor on the formula and do C-cC-c
#+TBLFM: $1=remote(FOO,###$1)::$2=remote(FOO,###$3)
The table B will be converted to the following
| 0 | 5 | 1 |
| 1 | 6 | 2 |
| 2 | 7 | 3 |
You can read about the syntax of the org table formulas here, basically $N refers to Nth column, #N refers to Nth row. ## and $# can be used instead of N to refer the row and column where the current value goes. remote(table-name, #N$N) refers to the Nth row and Nth column of the table table-name. :: concats multiple formulas.

I too had trouble using the standard rectangle operations. When moving to the next column, all of all of the rows between the point and the mark were highlighted. When I tried copying columns by formula as described above and in the org mode hacks, org threw errors if the column's values were non-numeric with more than one word.
But a good hint about cutting and pasting revealed that the problem is the initial direction of motion of the cursor. Moving first rightward to the next column, then down highlights the correct region. Standard rectangle operations then work correctly.

The "native" way in org mode is already covered in the answer by user2053036; I just wanted to add that in the simpler context, "to copy a column within a table": Let's say you have this table
| hello | world |
| is | good |
And want to repeat column 2 in column 3.
Steps:
Place the cursor after the bottom right | of the table
Open a new column to the right using keys Alt-Shift-<right>
| hello | world | |
| is | good | |
Add the "row copy from" formula (for example by putting cursor to row 1 column 3 and typing =$1 C-c C-c; or just type the TBLFM below the table and jump to step 4)
| hello | world | hello |
| is | good | |
#+TBLFM: $3=$1
Place the cursor on the TBLFM and type C-c C-c
| hello | world | hello |
| is | good | is |
#+TBLFM: $3=$1
That will copy column 1 to column 3.

Related

Problem with Postgresql, LIKE gives double results

I am currently working with Postgresql and I am facing a problem.
I have two tables "question" and "question_detail" in which there are codes. In "question_detail" are the codes including subcode so e.g. TB01Q07, TB01Q07a, TB01Q08_SQ002. Now I wanted to use the command LIKE to see if the table "question" also contains these records. But in "question.code" there are only codes without the following underscore. This is a table that was given to me, I find this somehow very stupid.
The problem is that when I search with LIKE the value TB01Q07a is listed twice. This is also understandable to me, since searching for TB01Q07% also returns the value TB01Q07a.
Does anyone know of a way to search only for TB01Q07a without it resulting in TB01Q07% as TB01Q07a?
Command
SELECT qd.code, qd.label, q.type
FROM public.question q,
public.question_detail qd
where CASE
WHEN qd.code = q.code THEN qd.code = q.code
ELSE qd.code like CONCAT(q.code,'%')
END;
question
| code | type |
| ---------|-------- |
| TB01Q07 | comment |
| TB01Q07a | comment |
| TB01Q08 | option |
**question_detail**
```none
| code | label |
| -------------- | ------|
| TB01Q07 | AB01 |
| TB01Q07a | AB02 |
| TB01Q08_SQL002 | AB03 |
I ran the SQL and wanted the TB01Q07a value to appear only once and not be listed twice.
I think I have found a solution with distinct on.
SELECT distinct on (qd.code) q.id_question,qd.code, q.question, q.question_type
FROM public.question q, public.question_detail qd
where qd.code like CONCAT(q.code,'%');
like('TB01Q07%') matches both TB01Q07 and TB01Q07a, so you get two rows for TB01Q07 and one row for TB01Q07a.
You need to be more precise and include the underscore. Also make sure it's escaped, _ means any one character in a like.
There is no need for a case, use or. Avoid using multiple from statements, use an explicit join with an explicit on. This is clearer and gives you more control over the join.
select qd.*, q.*
from public.question q
join public.question_detail qd
on qd.code = q.code OR qd.code like q.code || '\_%'
Demonstration.
Note: this problem doesn't exist if you use foreign keys. Assign unique IDs to question and reference them in question_detail. This is faster, shields you from changes to the question code, and ensures the referred to question exists.

How to re-align column of text table in emacs?

When I insert a table using M-x table-insert
I get the following table:
+-----+-----+-----+
| | | |
+-----+-----+-----+
| | | |
+-----+-----+-----+
| | | |
+-----+-----+-----+
But When I add content the | become disaligned like this
+-----+-----+-----+
| content | | |
+-----+-----+-----+
| | | |
+-----+-----+-----+
| | | |
+-----+-----+-----+
How do you realign the column?
When I press enter it just creates that:
+-----+-----+-----+
| contecnt | | |
| | | |
+-----+-----+-----+
| | | |
+-----+-----+-----+
| | | |
+-----+-----+-----+
You enter the corresponding cell and start editing the text with C-c' it opens the edit mode and automatically wraps the text accordingly. Once done type C-c' again and you're back in the nicely formated table.
The table should expand as needed for words longer than the width of the column, and the entire column should widen when this happens. Multiple words will wrap appropriately when the width is reached if there is space to break the line. Remember to use tab to move from cell to cell in the table.
Column width can be changed manually with M-x table-widen-cell and M-x table-narrow-cell.
Sometimes Emacs gets confused for some reason, and something like what you describe happens. One way to fix it is to exit table mode with M-x table-unrecognize-table, manually edit the table, then do M-x table-recognize-table. It's not ideal, but sometimes is easiest.
Yes, org-mode table and table-mode table functionality are interfering with one another and preventing the table from realigning properly. I'm not sure how to fix that, but you mentioned in a comment that you are using table-mode specifically because you want multi-lined cells in the table.
I suggest that instead you use an org table (with M-x org-table-create) and then you can split long lines over multiple lines within the same cell by calling M-x org-table-wrap-region at the point where you want the split.
Just press TAB within the table and it will autoalign.

Empty column header in table using github markdown?

Currently trying to format a table using github markdown. Would like to have something like:
| col1 | col2
-- | ---- | ----
r1 | r2 | r3
However markdown is forcing me to place text in the first column for the table to render. Any thoughts on how I could maintain an empty first column header?
So as this question can be marked as solved, I'll put JB Nizet comment below:
You could just use . That would add a non-breaking space (which is not strictly empty, but is at least invisible). Or you can start your heading row with a | (not sure it's supposed to work, but it does).

In MS Word, how do you create a list in table that separate bullet and content?

So I have this table with 2 columns and I want to put a list in it such that
+-------------------------------+
| * | Content 1 |
| * | Content 2 |
| * | Content 3 |
| * | Content 4 |
| * | Content 5 |
+-------------------------------+
the bullet is in the left cell, and the content is on the right cell, is this possible? I am using MS Word 2010.
You create a two-column table, set column widths to automatically adjusted (or something else, as you like) and you enter the bullet character “•” in the cells of the first column, e.g. by typing Alt+0149. I don’t think there’s a way to generate the bullets automatically in the same sense as you can control the bullets in a bulleted list.

How to use default org-mode spreadsheet auto-sum behavior ?

In this example:
| | num |
|---+-----|
| | 5 |
| | 6 |
| | 4 |
|---+-----|
| # | 15 |
| ^ | sum |
#+TBLFM: $sum=vsum(#2..#-1);
Every time you press "tab" in the sum area or C-c C-c in the table formula area below the table it recalculates the sum area. The problem is after it recalculates this area it adds this new sum to the current sum that's already in the area. (Try it in your emacs).
I've created this basic test to show issues I've encountered in creating complex spreadsheets in org. Until I find a solution I can't trust the sums emacs gives me because I may occasionally press C-c C-c twice and it's inconvenient to have to go and zeo out all sum values in my form every time I recalculate.
If anyone knows a solution, please let me know.
Turns out it was a bug with the Org-mode version I was using:
release 5.23a is from Apr 23 10:17:27 2008
Kudos to Thumper_ in freenode #org-mode for pointing it out.
Be sure to update to the newest version of Org-mode, folks! :)
Maybe it's more clear to use vsum(#I..#II) to indicate the rows between the 2 hlines.
I cannot reproduce your accumulation problem; and just read your answer about the bug!!