1

I'm using the built-in table editor of org-mode.

As described in the documentation, I can use M-S-Down to insert a row above my current row. Using the universal argument, I can create my new line below my current line instead of above: C-u M-S-Down.

However, when trying to create multiple lines at the same time, it does not work: C-- C-5 M-S-Down: this only create one line.

How can I insert a specific number of line below my current line?

2 Answers 2

3

AFAICS the function org-shiftmetadown is not implemented to handle the numeric prefix argument as a count of actions.

You can use a keyboard macro to use the prefix argument as a count for the number of repetitions.

One recipe which might do what you want goes like this:

C-x ( M-S-down C-x )

C-x C-k b F5

With the lines above F5 calls the macro (which does the M-S-down.)

The macro can handle numeric prefix arguments. E.g.

C-u 23 F5

calls the macro 23 times.

4
  • 1
    I don't think either C-x C-( or C-x C-) is bound by default.
    – Omar
    Jun 26, 2019 at 5:08
  • Ah, a mistake! Thanks for pointing that out! I meant C-x ( and C-x ).
    – Marco Wahl
    Jun 26, 2019 at 9:53
  • 1
    For a one-off, you might not want to waste a keybinding: in that case, skip the C-x C-k b F5 step and execute the macro 23 times with C-u 23 C-x e. Only the most recently defined macro can be executed with C-x e.
    – NickD
    Jun 26, 2019 at 13:16
  • 3
    By the way, you might want to check out <f3> and <f4> instead of C-x ( and C-x ) (and <f4> can also replace C-x e). They're not just different bindings, they run slightly different commands you might prefer: <f3> starts recording or inserts the macro counter, <f4> stops recording or runs the last keyboard macro.
    – Omar
    Jun 26, 2019 at 13:59
2

For a handful of rows, I wouldn't bother: pressing M-S-<down> a few times is easy. If you are adding rows at the end of the table, pressing RET a bunch of times is even easier.

The next step up in complexity is Marco Wahl's macro-based solution.

The next step up is to write your own function which takes a numeric prefix and does the row insertion that many times:

(defun n-org-insert-row (n)
   (interactive "p")
   (dotimes (i n) (org-table-insert-row 'below)))

You call it like this: C-u 23 M-x n-org-insert-row. If you leave out the numeric prefix, n defaults to 1. You can also bind the function to a key but I won't go into that here: you can find out about it elsewhere on this site, e.g. here, although there is no substitute for the documentation.

In the next step up, you can write a generic "do this n times" function and tell it explicitly when you call it what "this" (the thing that you want to repeat) is:

(defun n-do-this (n this)
   (dotimes (i n) (funcall this)))

where this is a function of no arguments. You can do what you want by evaluating this form:

(n-do-this 23 (lambda () (org-table-insert-row 'below)))

where you pass in an anonymous function that inserts a row. You can then define n-org-insert-row like this:

(defun n-org-insert-row (n)
   (interactive "p")
   (n-do-this n (lambda () (org-table-insert-row 'below)))

This is a different implementation from the earlier one, but "it looks the same from the outside": it is called the same way and it behaves the same way.

The nice thing about n-do-this is that it can be asked to do anything n times, not just insert a row in a table.

I'll stop here, but as I hope you can see, the sky's the limit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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