.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "STRING-REPLACE" "1" "Nov 13, 2025" "4.2" "fish-shell" .SH NAME string-replace \- replace substrings .SH SYNOPSIS .nf \fBstring\fP \fBreplace\fP [\fB\-a\fP | \fB\-\-all\fP] [\fB\-f\fP | \fB\-\-filter\fP] [\fB\-i\fP | \fB\-\-ignore\-case\fP] [\fB\-r\fP | \fB\-\-regex\fP] [(\fB\-m\fP | \fB\-\-max\-matches\fP) \fIMAX\fP] [\fB\-q\fP | \fB\-\-quiet\fP] \fIPATTERN\fP \fIREPLACEMENT\fP [\fISTRING\fP \&...] .fi .sp .SH DESCRIPTION .sp \fBstring replace\fP is similar to \fBstring match\fP but replaces non\-overlapping matching substrings with a replacement string and prints the result. By default, \fIPATTERN\fP is treated as a literal substring to be matched. .sp If \fB\-r\fP or \fB\-\-regex\fP is given, \fIPATTERN\fP is interpreted as a Perl\-compatible regular expression, and \fIREPLACEMENT\fP can contain C\-style escape sequences like \fBt\fP as well as references to capturing groups by number or name as \fI$n\fP or \fI${n}\fP\&. .sp If you specify the \fB\-f\fP or \fB\-\-filter\fP flag then each input string is printed only if a replacement was done. This is useful where you would otherwise use this idiom: \fBa_cmd | string match pattern | string replace pattern new_pattern\fP\&. You can instead just write \fBa_cmd | string replace \-\-filter pattern new_pattern\fP\&. .sp If \fB\-\-max\-matches MAX\fP or \fB\-m MAX\fP is used, \fBstring replace\fP will stop all processing after MAX lines of input have matched the specified pattern. In the event of \fB\-\-filter\fP or \fB\-f\fP, this means the output will be MAX lines in length. This can be used as an \(dqearly exit\(dq optimization when processing long inputs but expecting a limited and fixed number of outputs that might be found considerably before the input stream has been exhausted. .sp Exit status: 0 if at least one replacement was performed, or 1 otherwise. .SH EXAMPLES .SS Replace Literal Examples .INDENT 0.0 .INDENT 3.5 .sp .EX >_ string replace is was \(aqblue is my favorite\(aq blue was my favorite >_ string replace 3rd last 1st 2nd 3rd 1st 2nd last >_ string replace \-a \(aq \(aq _ \(aqspaces to underscores\(aq spaces_to_underscores .EE .UNINDENT .UNINDENT .SS Replace Regex Examples .INDENT 0.0 .INDENT 3.5 .sp .EX >_ string replace \-r \-a \(aq[^\ed.]+\(aq \(aq \(aq \(aq0 one two 3.14 four 5x\(aq 0 3.14 5 >_ string replace \-r \(aq(\ew+)\es+(\ew+)\(aq \(aq$2 $1 $$\(aq \(aqleft right\(aq right left $ >_ string replace \-r \(aq\es*newline\es*\(aq \(aq\en\(aq \(aqput a newline here\(aq put a here .EE .UNINDENT .UNINDENT .SH COPYRIGHT fish-shell developers .\" Generated by docutils manpage writer. .