Skip to Content

Vim - The power of `g`

Posted on

Some interesting command with g

  • ggg?G will root13 whole file :v
  • ga display hex, ascii of character under the cursor
  • g8 display hex value of utf-8 character.
  • G=gg auto (re)indent entire document.

:[range]g[!]/pattern/cmd

  • Use ! means do not match pattern
  • Cmd list:
    • d: Delete
    • m: Move
    • t: Copy
    • s: Replace

Example:

# Delete all line matching with pattern
:g/pattern/d
# Delete all blank lines
:g/^\s*$/d
# Move all line matching pattern to end of file
:g/pattern/m$
# Add text to end of line matching pattern
:g/^pattern/s/$/mytext
# Run a marco on matching lines
:g/pattern/normal @q

You can find more here

comments powered by Disqus