(Vim) commands :s, :g, :v
:s
: Substitution- Syntax:
:[range]s/pattern/relacement[/gci]
where - Flags:
gci
g
: Catch & substitute all matches in a given line, not just the first (“Global”)c
: Ask user whether to substitute or not (“Confirm”)i
: Match pattern in case-Insensitive manner
- e.g.
:s/apple/banana/c
substitutes the first “apple” in each line to “banana” after user confirmation
- Syntax:
:g
: Global command- Syntax:
:[range]g/pattern/cmd
wherepattern
: (Regex) pattern to match a given linecmd
: Command to execute on each matched line
- e.g.
:g/^\t\t/d
deletes all lines starting with 2 tabs
- Syntax:
:v
: Global non-match command- Useful example:
:v/./d
to remove any unwanted empty line (since any non-empty line would be matched)
- Useful example:
- Other tips
- Wrapping a word or phrase with
\<
,\>
forces to match that word or phrase exactly (occurrence as partial words are ignored)
- Wrapping a word or phrase with
Reference:
Written on February 9, 2021