Here's one way:
(defun doit ()
(interactive)
(while (not (eobp))
(beginning-of-line)
(when (looking-at "[A-Z]")
(insert "export "))
(next-line)))
This will walk through the lines starting at the cursor until the end of the buffer, and if the line starts with a capital letter (per the given regular expression range), then it will prepend with the string.
To use, paste this into *scratch*, position your cursor after the last close paren, and hit C-x C-e to load the definition. Then go to the buffer with your text, position your cursor on the first line you want to change, and invoke with M-x doit
.
Of course this could be enhanced to only work in a region, parameterize in various ways, etc.
Another option, if you don't want to define a function, is to adapt the approach into a macro like this:
- Go to the first line you want to change in your buffer
- Type C-x ( to start recording macro
- Type C-a to go to beginning of line (don't skip this step even if you're already at the beginning of the line)
- Type M-: to bring up the "Eval:" prompt and enter
(when (looking-at "[A-Z]") (insert "export "))
- Type C-n to go to the next line
- End the macro with C-x )
- Repeat the macro a bunch of times: C-u 10 C-x e
C-x ( string C-a C-n C-x )
, thenC-u 99 C-x e
– Drew Dec 12 '19 at 5:32#
. A non macro way to do the same would be to use C-x C-r t, string-rectangle once the start and end positions were marked. – icarus Dec 12 '19 at 6:27