Selecting entire function definition in Vim

Viewed 31946

I've been trying Vim for any text editing work for almost a week now. I want to know the fastest way to select a C function definition.

For example, if I have a function like this:

void helloworlds( int num )
{
    int n;
    for ( n = 0; n < num; ++n ) {
        printf( "Hello World!\n" );
    }
}

How would I be able to delete the whole definition including the function name?

14 Answers

To delete an entire function, including its definition, such as:

function tick() {
        // ... 
}
  • Move to the line with the function name.
  • Move the cursor to the opening brace, f{ should do it, or simply $.
  • Press V%d (Visual line, move to matching pair, delete)

If your functions look like this:

function tick() 
{
        // ... 
}
  • Move to the line with the function name.
  • Press J (join the current line with line bellow. This also puts your cursor at the last character on the resulting line, {, just the one we need for the next command.)
  • Press V%d (Visual line, move to matching pair, delete.)

or

  • Move to the line with the function name.
  • Press V[Down]%d (Visual line, move one line down, move to matching pair, delete.)

You can use this shortcut to delete not only the function, also the lines between curly braces, i.e the code between if-else statements,while,for loops ,etc.

Press Shitf + v [Will get you in visual Mode] at the curly brace start/end.

Then Press ] + } i.e ] + Shitf ] - If you are in start brace.

Then Press [ + { i.e [ + Shitf [ - If you are in end brace.

Then DEL to delete the lines selected.

If your function were separated by the blank lines, just type:

dip

which means "delete inner paragraph".

Pre-condition: be somewhere inside the function. Go to the previous closing curly bracket on the first line using

[]

Then delete down to the next closing curly bracket on the first line using

d][

Most posted methods have a downside or two. Usually, when working withing a class definition of some object oriented language, you might not have an empty line after the function body, because many code formatters put the closing braces of last method and class on consecutive lines. Also, you might have annotations on top of the function. To make matters worse, there might be empty lines within your function body. Additionally you'd prefer a method that works with the cursor anywhere within the function, because having to move it to a specific line or worse, character, takes valuable time. Imagine something like

public class Test {

    /* ... */

    @Test
    public void testStuff() {
        // given
        doSetup();

        // when
        doSomething();

        // then
        assertSomething();
    }
}

In this scenario, vap won't do you any good, since it stops at the first empty line within your function. v{o} is out for the same reason. va{V is better but doesn't catch the annotation on top of the method. So what I would do in the most general case is va{o{. va{ selects the whole function body (caveat: if your cursor is within a nested block, for instance an inner if statement, then you'll only get that block), o puts the cursor to the beginning of the selection and { selects the whole paragraph prepending your selection. This means you'll get the function definition, all annotations and doc comments.

if you use neovim version :>0.5
the modern way is to use treesitter and build your model, then you can be selected or yanked or deleted...

Tree-sitter is a parser generator tool and an incremental parsing library. It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited

I suggested this video on youtube to learn how to use treesitter to build your model : Let's create a Neovim plugin using Treesitter and Lua

I tried all the top answers here, but none of them works except the one by Nick which suggests to press f{ to get to the opening curly brace. Then V%d to delete the whole function.

Note that, the whole function gets yanked, so you can paste it elsewhere. I come across this use-case frequently, especially when moving if blocks inside another.

the most easy way I found is: Get to the line where the function starts and do this: ^^vf{% to mark the entire function and then whatever you like.

  • ^^ - start of the line
  • v - start visual mode
  • f - jump to the next search character
  • { - this is the search character
  • % - jump to the closing brackets

This is also very logical after you have used it a few times.

Related