Format a code block
Talk0this wiki
Redirected from VimTip1130
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}).
Contents |
Formatting a function
Edit
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
Edit
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
Edit
- 597 Indent a code block (consider merging)
- Run Vim command on current C/C++/Java function
- textobj-function plugin to make a function a text object
References
Edit
Comments
Edit
TO DO
- Change theme of whole tip to be working with code blocks via text objects (an introduction to
viBand friends). - Perhaps talk about textobj-function plugin (it's in 'See also'; thanks for the comment letting us know about it).