3

I have a mediawiki table with three columns and some rows, eg.:

{| class="sortable"
! Column header (one) !! Middle column !! Last column
|-
| value 1 || loong value ... and some more || another value
|-
| some|| more ||rows
|}

How do I efficiently swap the second column (the one with the Middle column heading) with the last one?

I just did it with query-replace-regexp and it wasn't so bad, but I suppose there must be a nicer way. Answers either for stock GNU Emacs 24.3, or using some generally useful packages (eg. multiple-cursors) will be appreciated.

4

Solution

You can easily do with with align-regexp, kill-rectangle and yank-rectangle.

Explanation

  • Select the region you want to align (rows with !! and ||).
  • C-uM-xalign-regexp RET
    • In Complex align using regexp: field: [|!][|!] RET NOTE: Just append this to the auto-inserted \(\s-*\) expression; don't replace!
    • In Parenthesis group to modify..: Hit RET
    • In Amount of spacing..: Hit RET
    • In Repeat throughout the line? (y or n): y

If everything went alright, you should get this (based on the original example in your question):

{| class="sortable"
! Column header (one) !! Middle column                 !! Last column
|-
| value 1             || loong value ... and some more || another value
|-
| some                || more                          ||rows
|}
  • Now select the region between points as shown below, between the point and mark represented by black rectangles:
{| class="sortable"
! Column header (one)▮!! Middle column                 !! Last column
|-
| value 1             || loong value ... and some more || another value
|-
| some                || more                         ▮||rows
|}
  • C-xrk - Kill/cut that rectangular region
  • Now bring the cursor to the point where you want to move that cut rectangular region:
{| class="sortable"
! Column header (one) !! Last column   ▮
|-
| value 1             || another value
|-
| some                ||rows
|}
  • .. and paste that area. C-xry

Result

{| class="sortable"
! Column header (one) !! Last column   !! Middle column                 
|-                                                                      
| value 1             || another value || loong value ... and some more 
|-                                                                      
| some                ||rows           || more                          
|}

This whole operation takes only about 10-15 seconds. So don't be discouraged by the length of this post.

  • Quite nice. I already knew about rectangle kill/yank, so I guess the lesson for today is align-regexp. This method changes whitespace much more than search/replace, but probably it usually won't be a problem. While testing the solution, the most irritating part turned out to be the need to put the cursor in the right place for yank (when some values in the last column are really really long). – Michał Politowski Oct 21 '14 at 14:12
  • To prevent the headache with long values, you can temporarily append || to each row including the header row and remove them at the end once you are happy with the table reorg. Both adding and removing those || character will be very straightforward using multiple-cursors. And I would use isearch to properly set the marks for rectangle killing – Kaushal Modi Oct 21 '14 at 14:16
  • Yes, appending "||" looks like a good trick. Answer accepted, I hope it doesn't discourage others from answering. – Michał Politowski Oct 21 '14 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.