Back to tips

Vim Global Substitution Default

Make /g the default for substitute commands with vim.gdefault

vim intermediate February 5, 2026 · godruoyi
#vim #substitution #settings

Enable vim.gdefault to make /g (replace all matches in a line) the default behavior for Vim substitute commands, eliminating the need to type /g every time.

How It Works

With vim.gdefault enabled, the /g flag behavior is inverted:

Commandgdefault ONgdefault OFF
:s///Replace allReplace one
:s///gReplace oneReplace all
:s///ggReplace allReplace one

Configuration

Add to your settings.json:

{
  "vim": {
    "gdefault": true
  }
}

Or use Vim commands in editor:

  • :set gdefault (short: :set gd) - Enable
  • :set nogdefault (short: :set nogd) - Disable

Why This Matters

Efficiency: Most substitutions replace all matches - make it the default.

Muscle memory: If you’re used to Vim with gdefault, maintain your workflow.

Less typing: Skip /g on every substitute command.

Example

Without gdefault:

:s/foo/bar/g   " Need /g to replace all

With gdefault:

:s/foo/bar     " Replaces all by default
:s/foo/bar/g   " Add /g to replace only first match

This setting is especially useful for users transitioning from Vim/Neovim where they’ve relied on gdefault for years.