Vim Tips Wiki
(Add a comment on textobj-function)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by one other user not shown)
Line 3: Line 3:
 
|previous=1129
 
|previous=1129
 
|next=1131
 
|next=1131
|created=February 13, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Matt Zyzik
 
|author=Matt Zyzik
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
This tip has some suggestions for operating on blocks of source code, particularly using text objects such as <code>iB</code> (inner block, same as <code>i{</code> or <code>i}</code>).
  +
 
==Formatting a function==
 
Here are some basic formatting commands:
 
Here are some basic formatting commands:
* '''<tt>=</tt>''' is the formatting command.
+
:'''<code>=</code>''' is an operator (by default, it formats/indents text).
* '''<tt>vi{</tt>''' is a text object selection which selects a code block.
+
:'''<code>i{</code>''' is a text object that specifies the surrounding code block.
* '''<tt>=vi{</tt>''' combines the two.
+
:'''<code>vi{</code>''' visually selects the inner code block around the cursor.
* '''<tt>=2vi{</tt>''' formats the current block and the block around it.
+
:'''<code>=i{</code>''' formats the code block.
  +
:'''<code>=2i{</code>''' formats the current block and the block around it.
* '''<tt>:noremap ,5 gg=G</tt>''' format entire buffer.
 
   
  +
You can format the entire buffer with <code>gg=G</code>.
==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):
 
<pre>
 
map <silent><leader>df 0d/{<Enter>daB
 
</pre>
 
   
  +
==Selecting a function==
This works for both traditional and modern C functions, and it should work for C-like languages too, like C++ and Java.
 
  +
A C function may start with a comment followed by the function header, then a body in braces. Assuming the cursor is on the first line of the comment before the function, you can type the following to select the entire function (comment, header and body):
 
<pre>
 
<pre>
  +
V/{<CR>%
int
 
func(a,b)
 
int a;
 
int b;
 
{
 
return a + b;
 
}
 
 
</pre>
 
</pre>
   
  +
The <code>V</code> starts linewise visual selection; the <code>/{</code> searches for the first left brace ("<code><CR></code>" means to press Enter); the <code>%</code> moves to the matching right brace (which should be the end of the function). You may want to then press <code>j</code> to move the cursor down to select any blank lines.
and
 
<pre>
 
int func(int a, int b) {
 
return a + b;
 
}
 
</pre>
 
 
;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:
 
<pre>
 
int
 
func(a,b)
 
int a;
 
int b;
 
{
 
if (a)
 
{
 
return a + b;
 
}
 
}
 
</pre>
 
 
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:
 
 
#go to the beginning of the line with '''<tt>0</tt>'''
 
#delete up until the opening brace with '''<tt>d/{</tt>'''
 
#delete everything until the matching brace with '''<tt>daB</tt>''' or '''<tt>d%</tt>'''
 
 
This could be made into a mapping as you have done:
 
<pre>
 
map <silent><leader>df 0d/{<Enter>daB
 
</pre>
 
   
  +
Having selected the function, you could press <code>x</code> to delete it, or <code>y</code> to copy it.
You'd certainly need to tweak it if you want to keep the function around for later pasting.
 
   
  +
==See also==
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 '\'.
 
  +
*[[VimTip597|597 Indent a code block]] (consider merging)
:I replaced my original with yours, it's much better. Thanks!
 
  +
*[[VimTip945|Run Vim command on current C/C++/Java function]]
  +
*{{script|id=2619|text=textobj-function}} plugin to make a function a text object
   
 
==References==
 
==References==
* {{help|text-objects}}.
+
*{{help|text-objects}}
   
 
==Comments==
 
==Comments==
 
{{todo}}
 
{{todo}}
 
*Change theme of whole tip to be working with code blocks via text objects (an introduction to <code>viB</code> and friends).
*The "Deleting a C function" section is a rough merge in from a 200902 tip by anon, with improvements by Fritzophrenic.
 
  +
*Perhaps talk about {{script|id=2619|text=textobj-function}} plugin (it's in 'See also'; thanks for the comment letting us know about it).
**How about using [http://www.vim.org/scripts/script.php?script_id=2619 textobj-function]?
 
*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.
 

Latest revision as of 06:08, 13 July 2012

Tip 1130 Printable Monobook Previous Next

created 2006 · complexity basic · author Matt Zyzik · version 6.0


This tip has some suggestions for operating on blocks of source code, particularly using text objects such as iB (inner block, same as i{ or i}).

Formatting a function[]

Here are some basic formatting commands:

= is an operator (by default, it formats/indents text).
i{ is a text object that specifies the surrounding code block.
vi{ visually selects the inner code block around the cursor.
=i{ formats the code block.
=2i{ formats the current block and the block around it.

You can format the entire buffer with gg=G.

Selecting a function[]

A C function may start with a comment followed by the function header, then a body in braces. Assuming the cursor is on the first line of the comment before the function, you can type the following to select the entire function (comment, header and body):

V/{<CR>%

The V starts linewise visual selection; the /{ searches for the first left brace ("<CR>" means to press Enter); the % moves to the matching right brace (which should be the end of the function). You may want to then press j to move the cursor down to select any blank lines.

Having selected the function, you could press x to delete it, or y to copy it.

See also[]

References[]

Comments[]

 TO DO 

  • Change theme of whole tip to be working with code blocks via text objects (an introduction to viB and friends).
  • Perhaps talk about textobj-function plugin (it's in 'See also'; thanks for the comment letting us know about it).