The syntax for g command (short for global) is shown below:


This command is used to edit lines that are first filtered based on a searchpattern.

  • :g/call/d delete all lines containing call
    • similar to the d Normal mode command, the deleted contents will be saved to the default " register
    • :g/call/d a in addition to the default register, the deleted content will also be stored in the "a register
    • :g/call/d _ deleted content won't be saved anywhere, since it uses the black hole register
  • :g/^#/t0 copy all lines starting with # to the start of the file
  • :1,5 g/call/d delete all lines containing call only for the first five lines
  • :g/cat/ s/animal/mammal/g replace animal with mammal only for the lines containing cat
  • :.,.+20 g/^#/ normal >> indent the current line and the next 20 lines only if the line starts with #
    • Note the use of normal when you need to use Normal mode commands on the filtered lines
    • Use normal! if you don't want user defined mappings to be considered

You can use g! or v to act on lines not satisfying the filtering condition.

  • :v/jump/d delete all lines not containing jump
    • same as :g!/jump/d

info In addition to the / delimiter, you can also use any single byte character other than alphabets, \, " or |.

info See :h :g for more details.

Video demo:

info See also my Vim Reference Guide and curated list of resources for Vim.