Block Text Objects

In many ways the blocks text object can be considered a logical extension of quotes. Just as specific chunks of text can be surrounded by quotation marks, other chunks of text can be surrounded by parentheses, brackets, etc. In Neovim, these chunks of text are called blocks:

block
sequences of text contained within {}, [], <>, or ()

While parentheses are common in prose, the various types of brackets are common in programming languages, and convey different meanings depending on the specific language being used. In any case, Neovim has the ability to isolate those blocks of text. As a simple example, let's look at how we can work with a few of the various block text objects the using a simple code snippet.

Selecting Inside Blocks

In this example, the cursor starts at the top of the buffer, which contains the beginning of a function declaration. Let's start by selecting the function arguments, which are contained inside the parentheses:

Initial Conditions
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
NORMAL
Top
1:1
 
Move the cursor
vi(
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
VISUAL
Top
1:21
 

As we saw with the quotes text object, the cursor first jumped to the parentheses, then selected the content inside them. Next, let's select the entire function body, which is encloses inside curly-brackets:

Move the cursor (again)
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
NORMAL
Top
1:22
 
Move the cursor (again)
vi{
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
VISUAL
60%
3:22
 

Finally, let's select the content contained within the list:

Move the cursor (again)
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
NORMAL
40%
2:1
 
Move the cursor (again)
vi[
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
VISUAL
40%
2:26
 

Selecting Around Blocks

Now, let's repeat each of the previous examples, except let's select around the blocks. In the first example we select around the parentheses, which includes both the content inside the parentheses as well as the parentheses themselves:

Initial Conditions
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
NORMAL
Top
1:21
 
Move the cursor
va(
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
VISUAL
Top
1:22
 

In the next example, we select the entire function body, including the curly-brackets:

Move the cursor (again)
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list); 
NORMAL
60%
3:21
 
Move the cursor (again)
va{
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list); 
} 
VISUAL
80%
4:1
 

And, finally, we select the list that is contained within the function body:

Move the cursor (again)
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
NORMAL
40%
2:26
 
Move the cursor (again)
va[
function·add(one,·two)·{
····const·list·=·[one,·two];
····return·sum(list);
}
VISUAL
40%
2:27
 

Note that slightly differently than we saw previously, selecting around block text objects does not include the whitespace. Whereas earlier text objects focused on prose, where whitespace is critical, the block text objects are more focused on code, and therefore the various block characters themselves, rather than the surrounding whitespace.