Skip to content

Instantly share code, notes, and snippets.

@weshouman
Last active April 17, 2020 09:39
Show Gist options
  • Save weshouman/33440ecda3777f0163a99e4711a42733 to your computer and use it in GitHub Desktop.
Save weshouman/33440ecda3777f0163a99e4711a42733 to your computer and use it in GitHub Desktop.
vim regex guide
  • Half the even numbered spaces :% s/\(\( \)\{\-2\}\)/\2/g by matching the the consecutive 4 spaces, source
    • origin: s/(( ){-2}/$2/g
  • Half the even numbered spaces :% s/\^\(\( \)\+\)\\1/\1/g by matching the whole spaces and halving them, source
    • origin: s/^(( )+)\1/$1/g
  • Encapsulate all the small caps strings into double quotes: :% s/\([a-z]\+\)/"\1"/g
    • origin: s /([a-z]+)/"$1"/g
  • Replace [:abc] with "abc": :% s/\[:\([a-z]\+\)\]/"\1"/g
    • origin: s /[:([a-z]+)]/$1/g or s /\[:([a-z]+)\]/$1/g I have to double check
  • Replace Model.enum[:abc].to_s with "abc": :% s/Model\.[a-z]\+\[:\([a-z]\+\)\]\.to_s/"\1"/g
    • origin: s /Model\.[a-z]+[:([a-z]+)]\.to_s/$1/g or s /Model\.[a-z]+\[:([a-z]+)\]\.to_s/$1/g I have to double check
  • Remove trailing spaces: :% s/\s\+$//g
    • origin s/\s+$//g
    • NOTE: to keep tabs and other whitespace chars, use :% s/[ ]\+$//g

Notes:

  • The variable grouping takes place through an escaped ()
  • However letter bags take place through non-escaped []
  • Using escaped \[\] will match the brackets exactly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment