Vim Tips Wiki
(rough merge in from 200902 by anon; add todo)
(Add a comment on textobj-function)
Line 79: Line 79:
 
{{todo}}
 
{{todo}}
 
*The "Deleting a C function" section is a rough merge in from a 200902 tip by anon, with improvements by Fritzophrenic.
 
*The "Deleting a C function" section is a rough merge in from a 200902 tip by anon, with improvements by Fritzophrenic.
  +
**How about using [http://www.vim.org/scripts/script.php?script_id=2619 textobj-function]?
 
*Clean it up; state premise of initial cursor position.
 
*Clean it up; state premise of initial cursor position.
 
*Change theme of whole tip to be working with code blocks via text objects (an introduction to viB and friends).
 
*Change theme of whole tip to be working with code blocks via text objects (an introduction to viB and friends).

Revision as of 01:43, 24 June 2009

Tip 1130 Printable Monobook Previous Next

created February 13, 2006 · complexity basic · author Matt Zyzik · version 6.0


Here are some basic formatting commands:

  • = is the formatting command.
  • vi{ is a text object selection which selects a code block.
  • =vi{ combines the two.
  • =2vi{ formats the current block and the block around it.
  • :noremap ,5 gg=G format entire buffer.

Deleting a C function

Add this map to your vimrc to have one command to delete an entire C function. To use, place your cursor on the first line of the prototype, and then type \df (assuming the default leader key of backslash):

map <silent><leader>df 0d/{<Enter>daB

This works for both traditional and modern C functions, and it should work for C-like languages too, like C++ and Java.

int
func(a,b)
    int a;
    int b;
{
    return a + b;
}

and

int func(int a, int b) {
    return a + b;
}
Comments

I don't think think this mapping will work very well for functions of any complexity. For example, it will fail in the following function:

int
func(a,b)
    int a;
    int b;
{
    if (a)
    {
      return a + b;
    }
}

Additionally, have you considered using text objects or even the % operator to avoid using marks? The following sequence, for example, should work to delete an entire C function, if the cursor is on the first line of the prototype:

  1. go to the beginning of the line with 0
  2. delete up until the opening brace with d/{
  3. delete everything until the matching brace with daB or d%

This could be made into a mapping as you have done:

map <silent><leader>df 0d/{<Enter>daB

You'd certainly need to tweak it if you want to keep the function around for later pasting.

Finally, I have replaced your ',' with a '\' in your description of how to invoke the mapping. Although <leader> could very well be mapped to ',' for you, the default is '\'.

I replaced my original with yours, it's much better. Thanks!

References

Comments

 TO DO 

  • The "Deleting a C function" section is a rough merge in from a 200902 tip by anon, with improvements by Fritzophrenic.
  • Clean it up; state premise of initial cursor position.
  • Change theme of whole tip to be working with code blocks via text objects (an introduction to viB and friends).
  • Fix tip at top: the 'v' is redundant.