Selecting entire function definition in Vim

前端 未结 11 676
醉话见心
醉话见心 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:19

    non-visual way:

    d/^}/e
    

    ... delete by searching for } at line beining, including it for deletion.

    without /e (not mentioned in above answers), solution is incomplete.

    with /e - searching goes to end of match, so closing bracket is included, and command is well for yanking too:

    y/^}/e
    
    0 讨论(0)
  • 2020-12-07 14:21

    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.)
    0 讨论(0)
  • 2020-12-07 14:24

    Use this simple way

       1.Go to the function definition 
       2.dd  - delete function definition 
       3.d    -start delete operation 
       4.shift+5(%) - delete the lines between { to }
    
    0 讨论(0)
  • 2020-12-07 14:27

    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.

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

    As is common in Vim, there are a bunch of ways!

    Note that the first two solutions depend on an absence of blank lines.

    • If your cursor is on the line with the function name, try d}. It will delete everything to the next block (i.e. your function body).

    • Within the function body itself, dap will delete the 'paragraph'.

    • You can delete a curly brace block with da}. (If you like this syntax, I recommend Tim Pope's fantastic surround.vim, which adds more features with a similar feel).

    • You could also try using regular expressions to delete until the next far left-indented closing curly brace: d/^}Enter

    • ]] and [[ move to the next/previous first-column curly brace (equivalent to using / and ? with that regex I mentioned above. Combine with the d motion, and you acheive the same effect. In addons like Python-mode, these operators are redefined to mean exactly what you're looking for: move from function to function.

    How to delete the whole block, header included

    If you're on the header/name, or the line before the block, da} should do the trick.

    If you're below a block, you can also make use of the handy 'offset' feature of a Vim search. d?^{?-1 will delete backwards to one line before the first occurrence of a first-column opening curly brace. This command's a bit tricky to type. Maybe you could make a <leader> shortcut out of it.

    Plugins

    I don't do much C programming in Vim, but there are surely plugins to help with such a thing. Try Vim Scripts or their mirror at GitHub.

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

    Another way is to go to the line of the start of your function and hit: Vj% (or V%% if your style puts the opening brace on the same line). This puts you into Visual-Line mode and the percent takes you to the matching closing brace. In the second style, the first % takes you to the opening brace on the line that you selected and the second to its matching closing brace.

    Also works for parentheses, brackets, C-style multi-line comments and preprocessor directives.

    See the manual for more info.

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