8

Is there an easy way to use replace-regexp to do something like:

XYZ001
XYZ002
XYZ003

to

'XYZ001'
'XYZ002'
'XYZ003'

i.e. replace-regexp XYZ* 'XYZ*'

Is replace-regexp the the wrong tool for the job? Or is there some special regex syntax to replace with the item that was matched?

3
  • While you can do this by matching the whole line in a group and then wrapping it, you might be better off with a simple macro.
    – user12563
    Commented Apr 29, 2019 at 19:07
  • @DoMiNeLa10 could you give an example of "matching to whole line in a group"? I was originally thinking about just writing my own matching function (i.e. calculate the difference between the match and the "replacement with match regex in it", then find each match manually and do the replace manually). It would probably be better to write a wrapper for however you do it in regex land, though... Commented Apr 29, 2019 at 19:23
  • The regexp that matches the entire line and capture it into a group can be written like this: \(^.*$\) then, you can wrap the matched group in quotes like this: ` '\1'`.
    – user12563
    Commented May 2, 2019 at 5:56

2 Answers 2

9

replace-regexp is perfect for the job. You could also use query-replace-regexp, which is more visual.

You want to replace XYZ\(...\) with 'XYZ\1'.
Explanation:
. stands for a single character, you could also use [0-9] if you know it is a number.
\( and \) groups those 3 characters in between and saves them.
\1 recalls the saved character group number one.

For more regular expression info, you can have a look at the emacs wiki.

Edit: If your string is on an entire line you could use ^XYZ\(...\)$ as search string.
^ means beginning of line. $ means end of line.

You can use '\&' as replace string. The \& replaces the entire match, which was found.

3
  • 3
    I'd use '\&' for the replacement: \& denotes the entire match.
    – Omar
    Commented Apr 29, 2019 at 19:34
  • thanks, I will edit my answer
    – jue
    Commented Apr 29, 2019 at 19:36
  • Yes!!! That did it. Exactly what I was looking for. Thank you!!! Commented Apr 29, 2019 at 20:27
0

In stock Emacs, I would break it into two steps.

Step 1: Insert leading quote

Define a (empty) rectangle consisting of the start of each line. Replace the contents of that (empty) rectangle with a '.

  1. At the top of the block, press C-<SPC> to set mark
  2. Navigate to the beginning of the last line
  3. Use C-x r t (string-rectangle) to insert a ' to each of the lines defined in the rectangle

Step2: Insert a trailing quote

Define a region and replace all line endings in the region with a '.

  1. Navigate to the end of the last line with C-e
  2. Set mark with C-<SPC>
  3. Navigate to the beginning of the first line
  4. Call M-x replace-regexp with $ for the end of the line and ' to replace the end of the line with a trailing quote

What I do in my day-to-day is use evil with evil-surround. In that case, it simply becomes ysiw'. The ys is the evil-surround command used to operate on a text object iw (i.e. inner word). The quote is then what you surround the inner word with. Altogether, I think of it as "you surround inner word (with) quote".

1
  • I've done something similar to this before, so not exactly the answer I was looking for. This doesn't work well if the item to match doesn't start/end the line. The example above is just a simple MCVE, and doesn't really represent what I want to do in general Commented Apr 29, 2019 at 19:18

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.