1

I want to make changes inside a paragraph of a large text file.

If I have the cursor at the word "foo", what key combination would get the cursor at the next occurrences of this word? The cursor could be at the beginning, end, or highlight the word.

I'm looking to replace "foo" with "bar" in a paragraph.

This is similar to Sublime Text's Ctrl-D

  • Yes it was what I said in the second sentence. By "at" I mean "at the beginning" or "at the end". Have rephrased the question so it is clearer and doesn't risk being closed. – Saurabh Sep 29 '19 at 23:21
2
  1. With point at the beginning of the word you want to replace, e.g., foo:

    C-s C-w M-%

  2. Then type the replacement word, e.g. bar, then RET.

  3. Then follow the prompts to query-replace subsequent occurrences of foo. (You can use ! to replace all of them, for instance.)

To limit query-replace to a paragraph, you can narrow to it (C-x n n). Alternatively, just quit query-replace after that paragraph (using q), or quit it at the last replacement (.).

Explanation:

C-s starts Isearch (incremental search). An immediate C-w yanks the text from point to the end of the word at point (e.g. foo) to the search string, so C-s C-w searches for that word.

M-% while searching switches to query-replace, using the search-string as the text to replace.


A minor shortcut for this question: you can use M-% M-n instead of C-s C-w M-% if you use library Replace+ (replace+.el)

With Replace+, query-replace and similar commands provide this behavior out of the box. Just use M-n to insert the word at point - it's the default FROM pattern.

Actually, the default value can be more than just the word at point. (And you have control over it using options search/replace-region-as-default-flag, search/replace-2nd-sel-as-default-flag, and search/replace-default-fn.)

And option replace-w-completion-flag, if non-nil, provides for minibuffer completion while you type the arguments. In that case, to insert a SPC or TAB character, you will need to precede it by C-q.

1

The multiple-cursors package is probably what you're looking for. You can install it using the melpa repository. Once installed, I believe the command mc/mark-next-like-this-work should do what you want. Then you can bind it to some custom keybinding like C-d:

(define-key global-map (kbd "C-d") 'mc/mark-next-like-this-word)

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.