I have the following text:

| Row # | Facility                 | Recycled Off-site |
|     1 | 1234, 12th st, jamestown |           216,574 |
|     2 | 1234, 12th st, jamestown |               760 |
|     3 | 1234, 12th st, jamestown |                 0 |
|     4 | 1234, 12th st, jamestown |               464 |
|     5 | 1234, 12th st, jamestown |                 0 |
|     6 | 1234, 12th st, jamestown |             2,280 |

and I want to remove all of the commas in the last column. I am able to select the last column (using a rectangular selection, or C-v in evil-mode), but how do I perform a regex replace only within the selected area? I don't want to replace the commas in the second column.

Any suggestions that use evil-mode would be extra helpful!

  • 3
    Juri Linkov is currently working on an enhancement to Emacs that would offer this feature. Dunno whether it will be in the first release of Emacs 25. A complete patch for (a first version of) it is here.
    – Drew
    Nov 13 '15 at 16:19
  • @Lucas: Why can't you just replace the commas with <kbd>C-x r t</kbd> then? Nov 14 '15 at 7:27
  • @Dieter.Wilhelm I am not sure what you mean - are you referring to the string-rectangle function? If so, what argument could I pass into that function? If I highlight the last column, C-x r t RET deletes the entire column.
    – modulitos
    Nov 14 '15 at 7:44
  • @Lucas: And when you just select the commas, isn't that what you want? Nov 14 '15 at 7:49
  • @Dieter.Wilhelm How do I "just select the commas"? Can you provide an example that would solve my problem above?
    – modulitos
    Nov 14 '15 at 7:58

For this particular case, it looks like you can just replace \([0-9]\),\([0-9]\) with \1\2. That will leave the commas in the second column alone because all of those are followed by spaces.

If you didn't have spaces in the second column, you could still replace \([,0-9]+\) |$ with

| \,(replace-regexp-in-string "," "" \1) |

This hits only the last column (because of the \$ anchor), and then uses the \, construct to replace the commas only there.

Note that this will disrupt your column spacing (because the commas aren't being replaced by anything), but it looks like you're in Org-mode, where C-c C-c will take care of that.

For the general case, I agree that what you really want is a rectangular regexp replace, but I don't know of a really easy way to achieve that.


For evil users, I've written a package which does query-replace and replace-regexp within evil visual blocks: evil-visual-replace.

Unfortunately, this only works with evil-mode (although you could always use evil just for the visual blocks or other cherry-picked functionality). For those who need replace within native rectangles, the patch Drew linked to in the comments above should work.


This looks like a duplicate of Search and replace inside a rectangle in emacs on Stack Overflow.

The answer shows how to do this either using CUA's rectangle editing, or via a custom command.

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.