Vim Tips Wiki
No edit summary
 
(Change <tt> to <code>, perhaps also minor tweak.)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1130
 
|id=1130
  +
|previous=1129
|title=Format a code block: =i__OPENCURL__
 
  +
|next=1131
|created=February 13, 2006 8:47
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Matt Zyzik
 
|author=Matt Zyzik
|version=5.7
+
|version=6.0
 
|rating=102/51
 
|rating=102/51
  +
|category1=Indenting
|text=
 
  +
|category2=
= is the formatting command, which has a default behavior of using Vim's internal formatting/indentation features.
 
 
}}
  +
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==
i{ is a text object selection which selects a code block: vi{
 
  +
Here are some basic formatting commands:
 
  +
:'''<code>=</code>''' is an operator (by default, it formats/indents text).
 
  +
:'''<code>i{</code>''' is a text object that specifies the surrounding code block.
 
  +
:'''<code>vi{</code>''' visually selects the inner code block around the cursor.
combine the two, and you can format a code block by typing =i{
 
  +
:'''<code>=i{</code>''' formats the code block.
 
 
:'''<code>=2i{</code>''' formats the current block and the block around it.
 
 
learn more about text object selections by typing :h text-objects
 
 
 
 
more and more of these text objects keep getting added (by Vim7 I know there are some really useful ones: it (inner tag, for xml editing) and i" (inside quotes) and more)
 
}}
 
   
  +
You can format the entire buffer with <code>gg=G</code>.
== Comments ==
 
... also...
 
if you want to format the current block and the block around it, do =2i{
 
you could do =3i{ for 3 levels, =4i{, and so on...
 
   
  +
==Selecting a function==
Matt Zyzik
 
  +
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):
, February 16, 2006 11:18
 
  +
<pre>
----
 
  +
V/{<CR>%
unfortunately it seems not to indent comments inside the { } (perl) :-(
 
  +
</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.
roland--AT--moriz.de
 
, February 16, 2006 15:29
 
----
 
Please refrain from inflating your own ratings. It nullifies the ratings system for everyone else.
 
   
  +
Having selected the function, you could press <code>x</code> to delete it, or <code>y</code> to copy it.
   
  +
==See also==
'''Anonymous'''
 
  +
*[[VimTip597|597 Indent a code block]] (consider merging)
, February 16, 2006 18:13
 
  +
*[[VimTip945|Run Vim command on current C/C++/Java function]]
----
 
  +
*{{script|id=2619|text=textobj-function}} plugin to make a function a text object
please refrain from taking up space in my "Additional Notes" area; thank you
 
   
  +
==References==
Matt Zyzik
 
  +
*{{help|text-objects}}
, February 17, 2006 17:26
 
----
 
Just today I have had the following idea:
 
:noremap ,5 1GvG=
 
This seems to work very well onto Perl-code.
 
   
 
==Comments==
shindojin
 
  +
{{todo}}
, May 3, 2006 14:43
 
  +
*Change theme of whole tip to be working with code blocks via text objects (an introduction to <code>viB</code> and friends).
----
 
  +
*Perhaps talk about {{script|id=2619|text=textobj-function}} plugin (it's in 'See also'; thanks for the comment letting us know about it).
<!-- parsed by vimtips.py in 0.502225 seconds-->
 

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).