medakk
medakk

Reputation: 101

In vim how to select "until the end of the function" for C-like languages?

Say the cursor is at the asterisk:

void foo(int n) {
  int i;
  // the cursor is here: *
  for(i=0; i<n; i++) {
    bar(i);
  }
}

What's the easiest way to perform an operator until the end of the function? If I run , when Ω is this operator, I'd expect to see:

void foo(int n) {
  int i;
  // the cursor is here:* 
}

That is, everything from that point to the end of the function is deleted. I know about di{, but that would delete the entire function.

Upvotes: 0

Views: 314

Answers (3)

Luc Hermitte
Luc Hermitte

Reputation: 32936

For C, Vim already provide ways to do that natively.

In C++, there are corner cases with functions defined within a class definitions - in these cases, the function definition is not 0-indented, and Vim doesn't know how to handle the detection function boundaries.

Some scripting is required. On vi.SE, I did describe a solution that I've integrated through 2 plugins I'm maintaining (lh-dev + lh-tags).

Upvotes: 2

Matt
Matt

Reputation: 15091

An alternative is to use a RegEx:

d/^}<CR>

That is: delete until the line starting with "}".

Upvotes: 1

Jorenar
Jorenar

Reputation: 2884

Check in :h object-motions:

Upvotes: 3

Related Questions