Getting to Black Belt in Vim – Part 5 – Red Belt

At blue belt level, we covered visual mode and advanced motions. At red belt level, we’ll introduce a powerful new concept. If you’re a developer, they’re a game changer – you can transform text in a few keystrokes. Let’s learn more.

Red Belt

Text Objects

Text objects are a sequence of commands, that let you edit text very quickly. They do this by allowing you to select the whole object regardless of where the cursor is. They’re different to motions, as motions start from where the cursor is.

Text objects start with either i, which stands for inner, or a, which stands for a or around. i doesn’t include whitespace or punctuation, whereas a does.

We use them with operators such as (y) yank, (c) change or (d) delete. They take the format:

<operator> + <text-object>

Using Text Objects

Let’s look at some common text objects, followed by examples of how they’re used with operators:

  • iw – inner word, not including white space or punctuation. Here are some examples:
    • ciwchange inner word. Will put you in insert mode, ready for new text.
    • diwdelete inner word.
    • yiwyank (copy) inner word.
    • viwvisualise (highlight) inner word:
Highlighting a word using viw. Notice how it only selects the word and not the whitespace after.
  • aw – a word, includes white space and punctuation after the word. For example:
    • dawdelete a word.
    • cawchange a word. Will put you in insert mode to change text.
    • vawvisualise (highlight) around word:
Highlighting a word using vaw. See how it captures the whitespace after the word.
  • i” – inside speech marks. For example, with the visual operator:
    • vi”visualise (highlight) everything inside the (speech marks):
Highlighting speech marks in a sentence using vi”.
  • a” – around and including speech marks. For example, using with the change operator:
    • ca”change around and including the (speech marks). Not that this puts you in insert mode.
  • ib – inside bracket. For example:
    • vibvisualise (highlight) everything inside the brackets.
  • ab – inner bracket and around bracket. Useful when working with text inside brackets:
    • cabchange around bracket, works for ( ).
  • it – inner tag. Useful for HTML / XML elements, for example:
    • citchange inner tag, works with any type of <tag>.
  • at – inner tag and around tag. Useful for HTML / XML elements:
    • vatvisualise (highlight) around tag.

Using Operators with Motions

  • We covered motions previously, and here are more examples of how you can use them with operators:
    • yt(yank (copy) until you reach the first (.
    • df,delete up to and including the first , (comma).

More Useful Commands

  • j – join, useful when want to join two split lines.
  • * (asterisk) – search for next occurrence of word under cursor. You can search for the previous occurrence using the hash key.
  • R to enter replace mode. Now everything you type overwrites existing text. <Esc> to exit replace mode.
  • r to replace one character only.

We’re almost there! In the next section, we’ll get to black belt level.

Published
Categorized as Vim Tagged