timhwang21.gitbook.io
  • Intro
  • Blog
    • 2023
    • 2022
    • 2021
    • 2020
    • 2019
  • CLI
    • Git
      • Bulk resolve merge conflicts
      • git rebase-onto
      • Patch from diff
    • Shell
      • Recipes
    • Vim
      • Batch editing
      • Buffers
      • <Ctrl-r>
      • External commands
      • :global
      • Help
      • Registers
      • Splits
    • Tools
  • Programming
    • React
      • Testing Styled Components
    • Typescript
      • curryRecord
      • Exhaustive conditionals with ADTs
      • newtype
      • OmitTypes
      • Safe JSON clone
      • Type inferrers
      • XOR type
    • Ruby
      • Pry
      • RSpec
    • Rails
      • Attributes
      • Cheap many to many
      • (Don't use) counter cache
    • Databases
      • Metrics
      • Testing indexes
  • Personal
    • Uses
    • Github
    • Medium
    • LinkedIn
    • Photography
Powered by GitBook
On this page
  • Commands
  • :g (:global)
  • :v (:vglobal) (equivalent to :g!)
  • Use cases
  • Delete lines containing pattern
  • Conditional find/replace
  • Yank all matches
  • :norm
  • :exe
  1. CLI
  2. Vim

:global

:s (:substitute) is one of the first commands any Vim user learns. There are several similar commands that are more generic. Like :s, these commands can take a range; however, unlike :s, there is no % variant, as these commands are global by nature.

These commands can be seen as a simpler, less general-purpose alternative to macros that takes less keypresses.

Commands

:g (:global)

Instead of substituting strings matching the first part with the second, runs the (command mode) command on strings matching the first part.

:v (:vglobal) (equivalent to :g!)

Same as :g, but targets strings that do NOT match.

Use cases

Delete lines containing pattern

g/pattern/d

Practical usage:

g/TODO:/d
g/debugger/d
g/binding.pry/d

Optimization: g/pattern/_d puts deleted lines in the null register, which has no performance cost.

Conditional find/replace

g/pattern/s/word/newword

This "scopes" the substitution to matching lines (for example, to scope your substitution to comments).

Yank all matches

g/pattern/y A

Note that we are yanking to an uppercase register, meaning we append matches. If we did y a, each yank would overwrite the previous yank.

:norm

g/pattern/norm ^ihello

This applies the normal mode instruction (here, "go to start and insert hello") to matching lines.

Practical usage:

g/debugger/norm gcc
g/pattern/norm @q

:exe

g/pattern/exec "norm!" "A\r"

When using :norm, you might notice that you can't insert linebreaks -- and <CR> is literally printed as the strings "\r" and "". :exec(ute) lets us actually pass in commands as strings to be executed, like eval().

This is a somewhat meta pattern that follows the last example. It executes the two separate commands "norm!" and "A\r" to the pattern. Before execution, the second is evaluated to "append carriage return."

PreviousExternal commandsNextHelp

Last updated 3 years ago