(Vim) commands :s, :g, :v
:s: Substitution- Syntax:
:[range]s/pattern/relacement[/gci]where - Flags:
gcig: 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/csubstitutes the first “apple” in each line to “banana” after user confirmation
- Syntax:
:g: Global command- Syntax:
:[range]g/pattern/cmdwherepattern: (Regex) pattern to match a given linecmd: Command to execute on each matched line
- e.g.
:g/^\t\t/ddeletes all lines starting with 2 tabs
- Syntax:
:v: Global non-match command- Useful example:
:v/./dto 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
