Wikia

Vim Tips Wiki

Changes: Format a code block

Edit

Back to page

(replace unhelpful (dangerous) mapping with better manual method)
(Change <tt> to <code>, perhaps also minor tweak.)
 
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 <tt>iB</tt> (inner block, same as <tt>i{</tt> or <tt>i}</tt>).
+
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==
 
==Formatting a function==
 
Here are some basic formatting commands:
 
Here are some basic formatting commands:
:'''<tt>=</tt>''' is an operator (by default, it formats/indents text).
+
:'''<code>=</code>''' is an operator (by default, it formats/indents text).
:'''<tt>i{</tt>''' is a text object that specifies the surrounding code block.
+
:'''<code>i{</code>''' is a text object that specifies the surrounding code block.
:'''<tt>vi{</tt>''' visually selects the inner code block around the cursor.
+
:'''<code>vi{</code>''' visually selects the inner code block around the cursor.
:'''<tt>=i{</tt>''' formats the code block.
+
:'''<code>=i{</code>''' formats the code block.
:'''<tt>=2i{</tt>''' formats the current block and the block around it.
+
:'''<code>=2i{</code>''' formats the current block and the block around it.
   
You can format the entire buffer with <tt>gg=G</tt>.
+
You can format the entire buffer with <code>gg=G</code>.
   
 
==Selecting a function==
 
==Selecting a function==
Line 29: Line 29:
 
</pre>
 
</pre>
   
The <tt>V</tt> starts linewise visual selection; the <tt>/{</tt> searches for the first left brace ("<tt><CR></tt>" means to press Enter); the <tt>%</tt> moves to the matching right brace (which should be the end of the function). You may want to then press <tt>j</tt> to move the cursor down to select any blank lines.
+
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.
   
Having selected the function, you could press <tt>x</tt> to delete it, or <tt>y</tt> to copy it.
+
Having selected the function, you could press <code>x</code> to delete it, or <code>y</code> to copy it.
   
 
==See also==
 
==See also==
Line 43: Line 43:
 
==Comments==
 
==Comments==
 
{{todo}}
 
{{todo}}
*Change theme of whole tip to be working with code blocks via text objects (an introduction to <tt>viB</tt> and friends).
+
*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).
 
*Perhaps talk about {{script|id=2619|text=textobj-function}} plugin (it's in 'See also'; thanks for the comment letting us know about it).

Latest revision as of 06:08, July 13, 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 functionEdit

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 functionEdit

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 alsoEdit

ReferencesEdit

CommentsEdit

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

Around Wikia's network

Random Wiki