Search code examples
vim

Select contents of next { } when already in another function using Vim


Let's say I'm working in the following file:

int main() {
    foo() {
        // do thing
    }
}

If my cursor is currently sitting anywhere in the text foo, and I want to enter Visual mode, select all text within the body of the Foo function, what commands would I do?

If I use vi{ this will cause the entire contents of the main() {} block to be highlighted (due to it being the enclosing block of my cursor), when all I want is the contents of the foo() {} block (the next block forward from my cursor) to be highlighted.

Is my only option to first use f{ to put my cursor on the { of the foo() {} block before running vi{?


Solution

  • Yes, moving your cursor to the opening { and then doing vi{ (or vi}, or viB) is how you do it, there is no better built-in method. Note that $ would be more efficient than f{ in this case.

    If you really don't like that extra motion, you can try target's "next pair" text objects but, honestly, vin{ is only marginally better than $vi{.