rwblickhan.org
Fiction
Essays
Technical
Logs
Miscellaneous
Gallery

TIL: Vim Text Objects

Most vim commands take text objects. For instance, we can do diw for delete in word or daw for delete around word, which includes the delimiters as well. I use w pretty heavily to select alphanumeric words, but there’s actually a lot of other useful options:

  • W: whitespace-delimited word
  • s: sentence
  • p: paragraph
  • ": contents within double quotes
  • ': contents within single quotes
  • ```: contents within backticks
  • ( or ): contents within parentheses
  • [ or ]: contents within square brackets
  • { or }: contents within curly brackets/braces
  • < or >: contents within angle brackets
  • t: contents within HTML tags like <a></a>

Two plugins supported by VSCodeVim implementation make this even more powerful:

  • CamelCaseMotion.vim: Adds \w for camel-case and snake-case words.
  • surround.vim: Adds options for changing surrounding delimiters in addition to the contents inside.
  • targets.vim: Adds smarter quote selection and separators like * (only partially supported by VSCodeVim).

References

  • “Vim Text Objects: The Definitive Guide”
  • “Text object selection”