9

I found it hard to understand these text object motions:

                            *]]*
]]          [count] sections forward or to the next '{' in the
            first column.  When used after an operator, then also
            stops below a '}' in the first column.  |exclusive|
            Note that |exclusive-linewise| often applies.

                            *][*
][          [count] sections forward or to the next '}' in the
            first column.  |exclusive|
            Note that |exclusive-linewise| often applies.

                            *[[*
[[          [count] sections backward or to the previous '{' in
            the first column.  |exclusive|
            Note that |exclusive-linewise| often applies.

                            *[]*
[]          [count] sections backward or to the previous '}' in
            the first column.  |exclusive|
            Note that |exclusive-linewise| often applies.

I can understand that "]" means forward, just like "]}" go to the next unmatched "}", and the second part "}" describe the target, which make sense.

But what does the second brackets in "]]", "][", "[[", "[]" means? If second "]" in "]]" means "'{' in the first column", then what about the second "]" in "[]", which means '}' rather than '{'. I just cannot make sense of these 4 text object motions.

Leira Hua
  • 91
  • 1
  • 4
    The meaning of "section" is explained 3 paragraphs below your quote. – romainl Feb 13 '16 at 08:59
  • 2
    @romainl That states: "the second character [determines] the type of brace found" which implies that ] should always match a }. This, as the OP points out, is not the case. They're asking for clarification on this point, not for the definition of a section. – Rich Feb 17 '16 at 10:24
  • see also https://vi.stackexchange.com/questions/17774/how-to-delete-entire-function-definition-c-style/17775#17775 and Section Movement Theory by Steve Losh – Hotschke Jan 24 '19 at 13:08

2 Answers2

6

It helps to test these out on a file. You can think of these as "forward" and "backward" X "to start" and "to end"

Try using the four motions on this block of text:

function one
{#1

}#2

function two
{#3

}#4

function three
{#5

}#6

I don't know how to do those often helpful movement gifs, but basically e.g. ]] ]] ]] will move from positions #1->3->5, and [[ in reverse; similarly move among #2<->4<->6 using the ][ and [] motions.

Here is a picture: enter image description here

saginaw
  • 6,756
  • 2
  • 26
  • 47
Philip
  • 301
  • 2
  • 8
5

These are to move to the top and bottom of functions, where the assumption is that functions' opening and closing braces are always in the first column. (Editorial: this seems to not often be the case these days so these motions will be vestigial for many users.)

The leading [ or ] indicate the direction you're going to go in the file, up or down respectively. Then the second [ or ] means to either go to an open brace at the function prologue or the close brace at the function epilogue; if it matches the leading character you go to the open brace, if it is different from the leading character you go to the close brace.

I use [[ and ]] quite regularly although I'd normally prefer to be put on the function definition. To me [] and ][ are non-intuitive while editing and I never use them; I wish I could figure out how to make them work in my brain because this is an operation I often accomplish via other means (e.g. the a} motion often covers me).

dash-tom-bang
  • 886
  • 6
  • 7