The answer by Mass suggesting [{
and ]}
works perfectly for unmatched curly brackets but only for curly brackets. This should be considered as an advantage. However, not all blocks are defined by curly brackets and the wish to handle other situations is also important.
Parentheses
Vim provides additionally [(
and ])
to do the same for parentheses (e.g. useful for lisp):
3 (
2 (
1 ( )
2 )
3 )
Note also the text objects ib
and ab
(analogue to iB
and aB
for curly brackets).
Square Brackets
Unfortunately, the situation for square brackets is different. Consider following python matrix using nested lists
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
One might guess [[
and ]]
would move to an unmatched square bracket, but they do something different (see :h ]]
and https://vi.stackexchange.com/a/14115/1292). There is actually no builtin motion for unmatched square brackets. However, the builtin text objects i[
and a[
to operate on blocks defined by square brackets exist.
Blocks specific to languages
Also if you want to move to other types of blocks often specific to languages (e.g. nested xml/html tags, nested if-blocks, nested LaTeX environments), there is no builtin motion for this. Again note the existence of the dedicated text objects for xml/html tags it
and at
.
Swiss Army knife: [%
and ]%
The plugins matchit and match-up provide the motions [%
and ]%
which can handle all situations and IMHO perfectly enhance the builtin set of motions together with the text objects i%
and a%
.
A drawback is that they consider all types of blocks and also matched brackets which can mean that you have to press [%
more often and is less precise than [{
and [(
which therefore have their place.
Note matchit is distributed with vim and can be loaded with :packadd! matchit
(:h matchit-install
). match-up has to be installed from the internet available on github and vim.org.
Ninja-feet
Another interesting vim plugin for this situation is https://github.com/tommcdo/vim-ninja-feet. If you want to add a new first/last row to the python matrix, you can press z[2i[
/z]2i]
to immediately jump to the outer square bracket block and open a new line.
Summary
========================================================================================
Block Type Move to previous Move to next Inner text obj. Outer text obj.
========================================================================================
curly bracket [{ ]} iB aB
----------------------------------------------------------------------------------------
parentheses [( ]) ib ab
----------------------------------------------------------------------------------------
square bracket i[ a[
----------------------------------------------------------------------------------------
xml/html it at
----------------------------------------------------------------------------------------
paragraph { } ip ap
----------------------------------------------------------------------------------------
section [[ [] ]] ][
----------------------------------------------------------------------------------------
method [m [M ]m ]M
----------------------------------------------------------------------------------------
matchit/-up [% ]% i% a%
----------------------------------------------------------------------------------------
ninja-feet z[<text object> z]<text obj>