I have a text like Code1. I want to convert from Code1 to Code2, including parentheses and removing some texts. How do I edit this efficiently?

Code 1

AAA & 0.076 & 0.053 & 0.053\\
AAA & 0.103 & 0.159 & 0.122\\
BBB & 0.036 & 0.037 & 0.037\\
BBB & 0.107 & 0.166 & 0.150\\
CCC & 0.033 & 0.062 & 0.062\\
CCC & 0.099 & 0.154 & 0.115\\


AAA & 0.076 & 0.053 & 0.053\\
 & (0.103) & (0.159) & (0.122)\\
BBB & 0.036 & 0.037 & 0.037\\
 & (0.107) & (0.166) & (0.150)\\
CCC & 0.033 & 0.062 & 0.062\\
 & (0.099) & (0.154) & (0.115)\\

2 Answers 2


There are a number of different tools/approaches that might be helpful for this kind of problem. Which ones work best for you will be a matter of personal preference.

You could do all of this with the built in query-replace-regexp, bound to C-M-% by default. Searching for "(0.[[:digit:]]+)" and replacing it with "(\1)" will start an interactive search. You can then press 'y' for each number you want to wrap in parentheses, and 'n' for each number you want to skip. That's probably the quickest way to solve the problem you describe.

For more general approaches, you might consider:

Wrap region makes it easy to quickly "wrap" a region with delimiters, including (), {}, "", and extendable to include pretty much whatever you need. It can also be tweaked so that different modes will apply a different suite of wrappers.

Expand region makes it easy to quickly select a region. With point on a word or number, expand region makes it easy to select that word, then the sentence, paragraph etc. This also works in code, where you can quickly select the object at point, then the expression, then the containing function etc.

Expand-region combined with wrap-region is a good way to wrap individual numbers in your text. You could use keyboard macros to repeating this for multiple numbers. Keyboard macros let you 'record' a series of key presses, then 'replay' them. In this case, you could record the keypresses that mark your number, wrap it in parentheses, and then move to the next number. Stop recording, and then repeat the macro over and over until you've wrapped everything you want.

  • Thank you for a series of solutions. I tried the first one, but it does not work. In particular, mark set the line, C-M-%, (0.[[:digit:]]+), and (\1). But "Replaced 0 occurrences".
    – hrkshr
    Commented Apr 19, 2021 at 14:15
  • 1
    This should work. The point must be before the text to be replaced. The regexp is \(0\.[[:digit:]]+\) the parenthesis and the dot have to be protected with \
    – gigiair
    Commented Apr 19, 2021 at 15:01
  • Related to the question, how do I select rows 1, 3, 5, and so on?
    – hrkshr
    Commented Apr 19, 2021 at 15:22

If you have to do it once in a while only I would vote for keyboard macros. In your case I would start with the first two lines:

AAA & 0.076 & 0.053 & 0.053\\
AAA & 0.103 & 0.159 & 0.122\\

record macro there and apply it to the following lines. So to say - place cursor at the first A and start recording (eg "C-x ("):

  • Move cursor down
  • delete AAA
  • move forward and insert parenthesis
  • move cursor to the first B stop recording (eg "C-x )")

Select region to apply the macro to and do (apply-macro-to-region-lines).

Your Answer

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

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