Improve replacing text within selection

Use case or problem

Replacing text within a selection does not work as expected. Currently, if I select a portion of a note and then use Ctrl/Cmd+h, the Find bar is filled with the entire text of the selected region. If I click in the Find bar and change the text, now the entire note will be searched and replaced.

Proposed solution

I would expect that I could select a part of a note, trigger the search & replace command, enter the text I wish to replace in the Find field, and be able to replace only matches within the selection (similar to MS Word).

As an example: say I have a whole bunch of Dataview key/value pairs that were originally in YAML, but I cut/pasted them into the actual note. I would like to select this region of the note, trigger the replace command, enter : into the Find field and :: into the Replace field, and be able to replace all instances of : within only that selection (not impacting any other part of the note).

Current workaround (optional)

Replacing multiple instances only works manually right now. So in my previous example, I would have to click Next a whole bunch of times until I got to the area where I pasted my Dataview pairs, then continue to click Replace and Next until I have fixed each instance within my desired area.

Related feature requests (optional)

I did a few cursory searches and didn’t find another request for this fix/feature. But let me know if I am just missing how to do this, or if there is already a thread that I have missed.

2 Likes

I wouldn’t want to lose the current behavior, but an “in selection” checkbox would be nice.

For now, another workaround is to copy the selection to a temporary note, use “replace all”, then copy the result back.

I personally don’t have a use for the current function, but maybe there’s a decent middle ground between status quo and switching entirely to my proposed behavior?

Perhaps instead this function could allow editing of the Find field without losing the selection? That way the original behavior (of placing the selection into the Find field) can be maintained while also allowing find & replace of user-entered text within only the selected region. And there would be minimal clicks between the two behaviors.

I have no background in coding at all so I have no clue how feasible this middle ground could be? The workaround is reasonable for a note or two, but I did it for like 20 notes and it got pretty clunky.

My proposed middle ground is to add to the current controls a checkbox labeled “In selection”. When the box is checked, the replacement only applies to the selected text. When the box is unchecked, the replacement applies to the whole note. That’s how I’ve seen it done elsewhere.

3 Likes

Hi,

i also found this limitation. As a developer i propose you a middle ground:

  • Invoking search/replace does not affect selection
  • Selection is pasted in search box, but selected (aka any input overrides it)
  • “In selection” checkbox
  • When replacing, if checkbox is marked, replacement occurs only in selection. Otherwise replacement is done in the whole note.

Thank you

This would be a really cool addition for obsidian.

1 Like

I think there’s a usability problem with the editor search function that underlies this issue with replace. If we could fix the search function, then there’d be a way to address the replace within selection.

Scenario one:

  1. Start with the cursor located in the middle of a file, but with no text selected.
  2. Press control-h to open the search-and-replace box
  3. In the search box, start typing the text you’re looking for.
    Note that the cursor jumps down from where you were, doing incremental, character-by-character matching on your search string till you stop typing. Cursor ends up at the first matching string (after your starting point) and has a “found” box around it.
  4. enter a replacement string and click “replace” button.

the editor found the first matching string (from your starting point) and replaced it with the desired string. Nice!

Scenario 2:

  1. start by manually finding the string you want to match and select it.
  2. Press control-h to open the search-and-replace box.
  3. Observe that the selected text appears in the search box, as expected. Also observe that the selection highlight has been removed from the text you selected. Eh?
  4. Also observe that the cursor has moved to the next occurrence of that search text in the file (assuming there is one).
  5. enter a replacement string and click “replace” button

Now, the editor replaced a second instance of your match, but left the one you originally selected untouched. Not nice!

The search-and-replace box already behaves differently when invoked with and without text being selected. It could be modified to leave the cursor where it is when text is selected and allow you to replace the first instance.
Call that the improved version of scenario 2.

But we could build on that text-being-selected signal to implement replace within selection!
Hypothetical Scenario 3:

  1. start by manually finding the string you want to match and select it.
  2. Press control-h to open the search-and-replace box.
  3. Observe that the selected text appears in the search box, as expected.
  4. (different from scenario 2) Observe cursor stays where it is, text stays selected.
  5. (the trick) enter a new search string in the search box.
  6. enter a replacement string in the replacement box and click “replace”

Now you’re doing replacement within the (still visible) selection! If you click “replace all”, it’s also bounded by the selection.

If you leave the search string unchanged when you click “replace”, you’re doing the improved version of scenario 2, whole-file search and replace.

You don’t need an explicit option to do the replace within selection, though maybe the trick for doing without it is to subtle that it would be hard for users to discover.

I’m pretty new to obsidian. But the “replace only inside selection” would be so helpful right now…