I was lately being teased by a vi user. He said that within few commands, one can do search-replace (both regexp and simple string ), on lines range. And one does not need to do highlight. One inputs start, end, and the string and replacement, pretty much.
Is there a way to do that in emacs? I'd hate to lose in a religious war ;-)
In emacs it's not that hard to write your own.
(defun my-query-regexp-replace-lines (start end) (interactive "nStart line (inclusive): \nnEnd line (inclusive): ") (let (start-point end-point) (save-excursion (goto-char (point-min)) (forward-line (- start 1)) (setq start-point (point)) (forward-line (1+ (- end start))) (setq end-point (point))) (save-restriction (narrow-to-region start-point end-point) (call-interactively 'query-replace-regexp))))
As the other answer says evil-mode is great too
I'm not sure I understand what's so great about not highlighting the lines first.
Anyway, selecting them is easy. The vi command
:20,2003s/foo/bar/g<CR> would be something like:
M-g M-g 20 RET C-SPC M-g M-g 2004 RET M-% foo RET bar RET !. Yes, the vi version is a full 3 keypresses shorter, and easier to type, but the Emacs one is not too bad. If you plan on doing lots of stuff to that same range of lines besides the search and replace, I recommend using
You can switch to evil-mode and use commands similar to the commands in vi.
- Precondition: Have Emacs package "evil" installed. (See https://melpa.org/#/evil)
Let the buffer be
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo
Activate evil-mode with
To replace "foo" with "bar" in lines 2 to 4 in a buffer
The result of the replacement:
foo bar bar bar bar bar bar bar bar bar foo foo foo foo foo
Deactivate evil with a further