Vim Tips Wiki
(possibly merge)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
  +
{{TipImported
Whenever you complete a code block in a curly braces language by typing '}', this mapping automatically indents said block.
 
  +
|id=597
<pre>imap } }<Esc>=%``a</pre>
 
  +
|previous=595
It works by typing out the '}', indenting with % and then returning to the last cursor position. It then puts vim back in insert mode (with a).
 
  +
|next=598
  +
|created=2003
  +
|complexity=intermediate
  +
|author=Marcel Preda
  +
|version=6.0
  +
|rating=358/114
  +
|category1=Indenting
  +
|category2=
  +
}}
  +
This tip contains information about adjusting the indent of source code in a block formed by braces (<code>{...}</code>), such as is used in the C language.
   
  +
For related information, see:
This doesn't work cleanly if the closing curly brace has to be indented outwards (the cursor goes to where it was originally typed). I don't believe there is a clean way to handle this.
 
  +
*[[VimTip83|Indenting source code]] settings that control indentation
  +
*[[VimTip224|Shifting blocks visually]] commands to change indents
  +
  +
==Commands to indent blocks==
  +
Suppose the indent options are correctly defined and we find this badly indented code:
  +
<pre>
  +
int myfunction(int a)
  +
{
  +
if ( a == 1 ) {
  +
printf("one");
  +
return 1; // the cursor is in this line
  +
}
  +
return 0;
  +
}
  +
</pre>
  +
  +
These commands will fix the indents:
  +
*<code>=i{</code> reindents "inner block" (inside the braces).
  +
*<code>=a{</code> reindents "a block" (including the braces).
  +
*<code>=2a{</code> reindents 2 blocks (this block and containing block).
  +
  +
Instead of "{", you can use "}" or "B", for example, <code>=aB</code> indents a block.
  +
  +
These commands will decrease or increase indents:
  +
*<code>>i{</code> increase indent for inner block.
  +
*<code>.</code> repeat last change (increase indent of block again).
  +
*<code><i{</code> decrease indent for inner block.
  +
  +
With the cursor on <code>{</code> or <code>}</code>:
  +
*<code>=%</code> indents the block (including matching brace).
  +
*<code>>%</code> or <code><%</code> indents or unindents the block.
   
 
==Comments==
 
==Comments==
  +
{{todo}}
Why is this mapping needed? Don't the Vim defaults automatically indent C programs as you type, with no need to do anything special when <tt>}</tt> is pressed? [[User:JohnBeckett|JohnBeckett]] 09:08, 28 March 2009 (UTC)
 
  +
*Check advice.
----
 
  +
*Perhaps add more <code>vaB</code>, <code>viB</code>, <code>>aB</code>, <code>>iB</code>.
Perhaps this isn't for C code, in which case 'cindent' would not be set and 'cinkeys' would have no effect? 'indentkeys' would still be the correct way to do this, however, as far as I can tell.
 
*{{help|'cindent'}}
 
*{{help|'cinkeys'}}
 
*{{help|'indentkeys'}}
 
--[[User:Fritzophrenic|Fritzophrenic]] 17:28, 30 March 2009 (UTC)
 
----
 
(Hope I'm adding this comment the right way...) Both the indenting and indentkeys probably cover most people's needs, but I find the feature useful myself. As an example, I was refactoring a bit of code just now, and I put an existing if-statement (consisting of several lines) inside a for loop. The whole body gets indented properly, whereas normally I would have to do it manually. It also helps in editing other people's code.
 
----
 
OK, but each new tip has to make sense to someone who is browsing the tip collection, and has to contain advice that is useful in most cases. In general, one should be using the standard indent procedures, and tips should explain those. I'm not sure if we have a better tip, but [[Indent a code block]] is a weak tip that should be enhanced. The above information could possibly be added, if the circumstances where it is helpful could be made clearer (I don't see how the tip would help pasting existing lines, unless you're using a console Vim where the paste is performed by the operating system feeding keys and you have forgotten to set the 'paste' option). My inclination is to merge tips, particularly short ones, to keep related information in one place. [[User:JohnBeckett|JohnBeckett]] 22:53, 30 March 2009 (UTC)
 
----
 

Latest revision as of 05:37, 13 July 2012

Tip 597 Printable Monobook Previous Next

created 2003 · complexity intermediate · author Marcel Preda · version 6.0


This tip contains information about adjusting the indent of source code in a block formed by braces ({...}), such as is used in the C language.

For related information, see:

Commands to indent blocks[]

Suppose the indent options are correctly defined and we find this badly indented code:

int myfunction(int a)
 {
 if ( a == 1 ) {
 printf("one");
return 1;        // the cursor is in this line
 }
 return 0;
   }

These commands will fix the indents:

  • =i{ reindents "inner block" (inside the braces).
  • =a{ reindents "a block" (including the braces).
  • =2a{ reindents 2 blocks (this block and containing block).

Instead of "{", you can use "}" or "B", for example, =aB indents a block.

These commands will decrease or increase indents:

  • >i{ increase indent for inner block.
  • . repeat last change (increase indent of block again).
  • <i{ decrease indent for inner block.

With the cursor on { or }:

  • =% indents the block (including matching brace).
  • >% or <% indents or unindents the block.

Comments[]

 TO DO 

  • Check advice.
  • Perhaps add more vaB, viB, >aB, >iB.