diff --git a/internal/action/command.go b/internal/action/command.go index 667de11b..2d9b2fbb 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -764,7 +764,7 @@ func (h *BufPane) ReplaceCmd(args []string) { InfoBar.YNPrompt("Perform replacement (y,n,esc)", func(yes, canceled bool) { if !canceled && yes { - h.Buf.Replace(locs[0], locs[1], replaceStr) + h.Buf.ReplaceRegex(locs[0], locs[1], regex, replace) searchLoc = locs[0] searchLoc.X += utf8.RuneCount(replace) diff --git a/internal/buffer/search.go b/internal/buffer/search.go index 0930ba2c..64bcc48c 100644 --- a/internal/buffer/search.go +++ b/internal/buffer/search.go @@ -155,8 +155,12 @@ func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []b l = util.SliceStart(l, end.X) } newText := search.ReplaceAllFunc(l, func(in []byte) []byte { + result := []byte{} + for _, submatches := range search.FindAllSubmatchIndex(in, -1) { + result = search.Expand(result, replace, in, submatches) + } found++ - return replace + return result }) from := Loc{charpos, i}