Selecting entire function definition in Vim

前端 未结 11 677
醉话见心
醉话见心 2020-12-07 14:14

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 fun

相关标签:
11条回答
  • 2020-12-07 14:35

    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.

    0 讨论(0)
  • 2020-12-07 14:40

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

    dip
    

    which means "delete inner paragraph".

    0 讨论(0)
  • 2020-12-07 14:42

    The simplest and most direct way way is as follows (works anywhere inside function):

    v   enter visual mode
    {   move to first brace in function (may have to press more than once)
    o   exchange cursor from top to bottom of selection
    }   extend selection to bottom of function
    d   delete selected text
    

    The complete command sequence would be v{o}d. Note that you can do other operations besides delete the same way. For example, to copy the function, use y (yank) instead of d.

    0 讨论(0)
  • 2020-12-07 14:43

    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][
    
    0 讨论(0)
  • 2020-12-07 14:44

    If you are willing to install plugins vim-textobj-function will give you vif for Visual select Inside Function and vaf for Visual select A Function.

    daf will delete the function, both the line with the signature and the function body ({})

    The text object defined by this plugin are more specific and they don't rely on the function body being a contiguous block of text or { being placed at the first character on the line.

    The drawback is that you depend on an external plugin.

    0 讨论(0)
提交回复
热议问题