Vim でファイル内の文字列を一括置換する
Published Feb 3, 2025
⋅
Updated Feb 16, 2025
Vim でファイル内の文字列を一括置換したい場合は次のようにコマンドを実行する。
:%s/foo/bar/g
:s
は、:substitute
でも :su
でもよい。 %
は 1,$
と等価でファイル全体を指す。そのあとに置換したいパターン、置換後の文字列、フラグを指定する。 g
を付けることで、行においてマッチしたすべての文字列を置換することができる。
4.2 Substitute :substitute
:s :su
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
For each line in [range] replace a match of {pattern}
with {string}.
For the {pattern} see pattern.
{string} can be a literal string, or something
special; see sub-replace-special.
When [range] and [count] are omitted, replace in the
current line only. When [count] is given, replace in
[count] lines, starting with the last line in [range].
When [range] is omitted start in the current line.
E939 E1510
[count] must be a positive number (max 2147483647)
Also see cmdline-ranges.
See :s_flags for [flags].
The delimiter doesn't need to be /, see
pattern-delimiter.
参考