How can I delete the block

int Test()
{
//
}

when my cursor is before int (or even better when it is anywhere on that line)?

The shortest I could come up with is d<line>G where <line> is the line number of the closing brace, or d<diff>j where <diff> is the difference of the line number. I am not happy with either.

  • 1
    What about V][d? – Hotschke Oct 30 at 8:04
  • @Hotschke I like it – Bananach Oct 30 at 8:37
  • An alternative: Vj%d – Rich Oct 30 at 11:16
up vote 4 down vote accepted

Motion ][: next } in the first column

V][d does what you want. More details can be found under :h ][ and :h section:

                            *section*
[...]
The "]" and "[" commands stop at the '{' or '}' in the first column.  This is
useful to find the start or end of a function in a C program.  Note that the
first character of the command determines the search direction and the
second character the type of brace found.

Memorizing

If you are not familiar with the set of square bracket motions [[, [], ]], ][ and you struggle memorizing, maybe following grouping and hints help:

  1. [[ and ]] jump to the beginning (i.e. {) of the current/previous and next function
  2. [] and ][ jump to the end (i.e. }) of the previous and current/next function

As mentioned in the vim help page: the first square bracket you type determines the direction you move and the second bracket the type you are looking for. The second bracket is more difficult to grasp because the meaning can be seen as inconsistent: [ relates not to { and ] not to }.
I memorize it as

  1. beginning of function: square bracket must be repeated
  2. end of function: square bracket must be alternated

I think it is worthwhile to learn them: many languages use curly braces. Additionally, ftplugins in $VIMRUNTIME/ftplugin override them for many languages, which are not curly brace based, to provide semantic equivalents for the respective languages; most prominently python, ruby, sql, and vimscript.

Also third party ftplugins for latex (vimtex), markdown (vim-markdown by plasticboy), html/xml (xml.vim) and probably more remap them.

Note there are different expectations of what the equivalents should be. For instance, pythonsense provides alternatives for python. For markdown there are also several plugins which offer different versions: plasticboy/vim-markdown, vim-pandoc, gabrielelana/vim-markdown, pandoc-sections.vim.

Egyptian Brackets

For code which uses Egyptian Brackets for functions and classes, vim help suggests to customize [[, [], ]], ][:

If your '{' or '}' are not in the first column, and you would like to use "[["
and "]]" anyway, try these mappings: >
   :map [[ ?{<CR>w99[{
   :map ][ /}<CR>b99]}
   :map ]] j0[[%/{<CR>
   :map [] k$][%?}<CR>
[type these literally, see |<>|]

Typical languages are java, rust and go. Actually, the end of function motions could be left unchanged. $VIMRUNTIME/ftplugin/rust.vim and the third-party plugin vim-go remap only [[ and ]] for you. Also $VIMRUNTIME/ftplugin/php.vim remaps only [[ and ]] to jump to keywords like function or class regardless whether Egyptian style is used.

Also the plugin lh-dev by Luc Hermitte tries to help here.

The paragraph (ip, ap) and curly brace (i{, a{) motions are good for this. The curly braces may need to be forced linewise with Va}, but they still wont capture the method declaration.

If, like me, you put blank lines between methods, the paragraph motion is suitable and succinct (and does not require memorization of which square bracket does what). dap rolls off my fingers because I use it so frequently.

  • I also quite like ip/ap. However, usually you cannot be sure that there are no blank lines within a function/method. Also iB/aB (= i{/a{ = i}/a}) do not select the 'header' line of the function (int Test()). – Hotschke Oct 30 at 13:58
  • @Hotschke agreed, i hadnt thought about the inner body much. Though id argue blank lines in the body is an opportunity for refactoring :P i noted in answer about the declaration line. – D. Ben Knoble Oct 30 at 14:21

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.