2

I want to use search/replace (string or regexp) inside a rectangular region (aka column selection?).

Consider this question: Search and replace inside a rectangle in emacs

That's what I want, except I'm not using CUA mode. Maybe I should, but I'd have to disable C-v stuff and the changes to C-x ...

The rectangular region selection I use came straight from this question/answer: Enable Emacs column selection using mouse

So, how may I search/replace within the type of rectangle created by that mouse-start-rectangle function?

4 Answers 4

1

If you use library Isearch+ then you can do this immediately.

Whenever option isearchp-restrict-to-region-flag is non-nil, which it is by default, searching is limited to the active region. And this applies even if the region is rectangular.

Just load isearch+.el, turn on rectangle-mark-mode to create a rectangular region, and then use C-s etc. to search normally.

For replacing, you can use query-replace or replace on demand using C-M-RET during Isearch.

1

Consider this question: Search and replace inside a rectangle in emacs

That's what I want, except I'm not using CUA mode.

I'm confused by your question, because the bulk of the accepted answer to the question you have linked to is dedicated to achieving that very goal -- search and replace within a rectangle without using CUA.

You ought to revisit that Q&A and try out the pair of commands which were written for it.

1

cua-rectangle-mark-mode is pretty much independent of cua-mode. It's kind of unfortunate that they are associated, because cua-mode is kind of weird and cua-rectangle-mark-mode is IMO much better than the other rectangle marking system. cua-rectangle-mark-mode won't change any of your keybindings.

(global-set-key (kbd "C-<return>") #'cua-rectangle-mark-mode)

cua-rect will be autoloaded the first time it's invoked. C-<return> is the default when you enable cua-mode (but again, you don't need to bother with cua-mode at all).

The M-r binding is in cua--rectangle-keymap, and will do a regexp replace in the rectangle.

0

If you use evil, then you can just

(setq evil-ex-visual-char-range t)

and ordinary ex search and replace with a region selected in visual state will behave as you require.

That is, you can just visually select a region, then type : which will bring up the minibuffer pre-filled with

:`<,`>

which designates the region you selected, and you can complete it with your search command. For example:

:`<,`>s/foo/bar/g

which will replace all occurrences of "foo" with "bar" just inside the region you had selected.

Your Answer

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

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