0

How to add the string 'export ' to the beginning of each line of this text, with emacs:

RBENV_VERSION="2.n.0"

AUTHORIZE_NET_API_LOGIN_ID=example
AUTHORIZE_NET_TRANSACTION_KEY=example

# Google Calendar Integration

# Google Calendar
GOOGLE_CLIENT_ID=example
GOOGLE_CLIENT_SECRET=example

# Live Mailchimp keys
MAILCHIMP_API_KEY=example
  • 1
    C-x ( string C-a C-n C-x ), then C-u 99 C-x e – Drew Dec 12 '19 at 5:32
  • @Drew I agree this answers the question in the text, but not the question in the title. I further suspect that the OP would like to skip lines starting with #. 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
  • in my experience, C-x C-r t adds to the blank lines too – american-ninja-warrior Dec 12 '19 at 13:40
  • Yes, C-x C-r t will have the same, undesired effect of adding to blank and comment lines that the keyboard macro does. – icarus Dec 12 '19 at 14:16
0

An answer to a different question would be to add

 _oldflags=$- ; set -a

as the first line of the file and

 [[ "$_oldflags" =~ a ]] || set +a ; unset _oldflags

which tells bash to export all the variables (the set -a) and restore the state of the "set -a" flag at the end. If you never use "set -a" then this can be simplified to adding "set -a" at the start and "set +a" at the end.

|improve this answer|||||
0

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:

  1. Go to the first line you want to change in your buffer
  2. Type C-x ( to start recording macro
  3. 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)
  4. Type M-: to bring up the "Eval:" prompt and enter (when (looking-at "[A-Z]") (insert "export "))
  5. Type C-n to go to the next line
  6. End the macro with C-x )
  7. Repeat the macro a bunch of times: C-u 10 C-x e
|improve this answer|||||

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.