5

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!

modulitos
  • 2,432
  • 1
  • 18
  • 36
  • 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](http://lists.gnu.org/archive/html/emacs-devel/2015-11/msg01238.html). – Drew Nov 13 '15 at 16:19
  • @Lucas: Why can't you just replace the commas with C-x r t then? – Dieter.Wilhelm Nov 14 '15 at 07: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 07:44
  • @Lucas: And when you just select the commas, isn't that what you want? – Dieter.Wilhelm Nov 14 '15 at 07: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 07:58
  • @Lucas: Please put your cursor befor the most upper comma and press C-x SPACE then position the cursor on the most lower comma. – Dieter.Wilhelm Nov 14 '15 at 08:14
  • @Dieter.Wilhelm I think I see what you are getting at, where I delete the entire *character* column (previously I was referring to the column in the table). This is a nice workaround, but it doesn't solve my problem of replacing text within a rectangular selection. Thanks anyway! – modulitos Nov 14 '15 at 08:35
  • @Lucas : With C-x r t you are also able to replace the selected column text! ;-) – Dieter.Wilhelm Nov 14 '15 at 08:39
  • You could always rectangle kill it into a temporary buffer, do your replace and rectangle-kill back to the first buffer. I bet someone with better elisp skills than me could write a function in no time. – Alan Third Nov 21 '15 at 00:07

3 Answers3

2

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.

Aaron Harris
  • 2,664
  • 17
  • 22
2

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.

pyrocrasty
  • 203
  • 1
  • 7
1

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.

phils
  • 48,657
  • 3
  • 76
  • 115