When we write a page of text, that page is typically made up of several paragraphs of text. Each of those paragraphs is made up of several sentences, and finally each of those sentences is made up of words. Likewise when we code, depending on the programming language we are using we might have blocks of code, chunks of code wrapped in parentheses, etc.
While our brains are able to organize those logic groupings of text, many conventional text editors are not and therefore, for example, limit cursor movements to characters (horizontal movements) or lines (vertical movements), which can be slow and frustrating.
Neovim defines a variety of text objects that represent entire words, sentences, paragraphs, blocks of text, HTML tags, etc, bringing a "higher-level" abstraction that Neovim leverages to provide some pretty powerful capabilities. Moreover, Neovim's excellent Tree-sitter integration extends the available text objects even further, and you can even define custom blocks that suit your unique needs. Learning to leverage these capabilities can provide a significant efficiency boost to both navigating and editing documents in Neovim, as well as make your workflow far more fluid.
The Basics
Basic text object usage follows the pattern:
{command} {text object}
which is similar to that used for commands in general. In short, we specify the command that performs the action that we want to take, then the text object over which we want that action to be taken.
Commands that work with text objects are mostly that we already know and use every day, for example yank y, delete d, change c, and specifying whether we want those actions to be taken inside i, or around a the specified text object
The consistency with which you can use text objects is one of their great benefits - once you learn the basics of working with text objects that knowledge (and muscle memory) can be applied consistently across other commands and text objects.
Going Further
We will discuss many of the available text objects in the coming sections, but before we get to those we want to briefly discuss strategies for learning text objects. While the concept of text objects is relatively straightforward, there are a number of text objects available, and mastering them requires a bit of time and practice. We mentioned previously that there is a great deal of consistency among the working with the various text objects, so we wanted to recommend a strategy that has worked well.
Rather than try to learn all of the text objects at the same time, start by focusing on a few of
the most common text objects, such as w
word and s
sentence. Develop muscle
memory working with these every-day text objects, and get a feel for using the i
and a
prefixes as well so that they become second-nature. After you have mastered these simple text
objects you will be able to extend those capabilities to the other text objects with little
effort.
Second, we want to mention a little trick that can help while you learn to use text objects - if you want to get a quick "preview" of the text contained within a text object you can specify the v to visually-select the text contained within the text object, rather apply the command to it. Although this initially adds a few keystrokes, it is a great way to double-check you expectations and build confidence in your ability to leverage text objects effectively.
With that, let's proceed to the following sections and start learning the text objects.