Skip to content

Support replacement string for regex search #111

Closed
@lemoncmd

Description

@lemoncmd

Image

While Edit supports regex-based searching, it currently lacks the ability to utilize captured strings in the replacement target.
Please add support for referencing captured groups in the replacement string using syntax like $1 or \1.

Activity

added
I-taskSmaller tasks. Requires work in the 100s of LOC (usually).
E-help-wantedWe encourage you to jump in on these!
on May 20, 2025
lhecker

lhecker commented on May 20, 2025

@lhecker
Member

Here's the relevant code:

edit/src/buffer/mod.rs

Lines 1040 to 1079 in 514689c

/// Find the next occurrence of the given `pattern` and replace it with `replacement`.
pub fn find_and_replace(
&mut self,
pattern: &str,
options: SearchOptions,
replacement: &str,
) -> apperr::Result<()> {
// Editors traditionally replace the previous search hit, not the next possible one.
if let (Some(search), Some(..)) = (&mut self.search, &self.selection) {
let search = search.get_mut();
if search.selection_generation == self.selection_generation {
self.write(replacement.as_bytes(), true);
}
}
self.find_and_select(pattern, options)
}
/// Find all occurrences of the given `pattern` and replace them with `replacement`.
pub fn find_and_replace_all(
&mut self,
pattern: &str,
options: SearchOptions,
replacement: &str,
) -> apperr::Result<()> {
let replacement = replacement.as_bytes();
let mut search = self.find_construct_search(pattern, options)?;
let mut offset = 0;
loop {
self.find_select_next(&mut search, offset, false);
if !self.has_selection() {
break;
}
self.write(replacement, true);
offset = self.cursor.offset;
}
Ok(())
}

added 2 commits that reference this issue on Jun 18, 2025
70f5b73
eda2337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-help-wantedWe encourage you to jump in on these!I-taskSmaller tasks. Requires work in the 100s of LOC (usually).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @lhecker@lemoncmd

      Issue actions

        Support replacement string for regex search · Issue #111 · microsoft/edit