Vim Tips Wiki
Register
Advertisement
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).
Advertisement