Wikia

Vim Tips Wiki

Changes: Run Vim command on current C/C++/Java function

Edit

Back to page

(use character entity so renders correctly)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 5: Line 5:
 
|previous=944
 
|previous=944
 
|next=946
 
|next=946
|created=June 5, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Lorenz Wegener
 
|author=Lorenz Wegener
Line 13: Line 13:
 
|category2=C++
 
|category2=C++
 
}}
 
}}
It is often useful to restrict the range of commands like <tt>s</tt> or <tt>g</tt> to the function one is currently editing. For C/C++ and Java,
+
It is often useful to restrict the range of commands like <code>s</code> or <code>g</code> to the function one is currently editing. For C/C++ and Java,
   
 
<pre>
 
<pre>
Line 19: Line 19:
 
</pre>
 
</pre>
   
maps the keystrokes <tt>;tf</tt> ('''t'''his '''f'''unction) to the range of the function in which the cursor is currently located. It works by searching backwards for a '''{''' occurring in the first column, and then for a '''(''' to find the start of the function. A '''}''' in the first column ends the function body.
+
maps the keystrokes <code>;tf</code> ('''t'''his '''f'''unction) to the range of the function in which the cursor is currently located. It works by searching backwards for a '''{''' occurring in the first column, and then for a '''(''' to find the start of the function. A '''}''' in the first column ends the function body.
   
 
For example, suppose you want to change the name of a function argument in the current function from i to ii:
 
For example, suppose you want to change the name of a function argument in the current function from i to ii:
Line 33: Line 33:
 
</pre>
 
</pre>
   
In normal mode, type <tt>:;tf</tt> followed by the subsitution command <tt>s/\&lt;i\>/ii/g</tt>. The command line now reads:
+
In normal mode, type <code>:;tf</code> followed by the subsitution command <code>s/\&lt;i\>/ii/g</code>. The command line now reads:
   
 
<pre>
 
<pre>
Line 55: Line 55:
 
This tip assumes that:
 
This tip assumes that:
 
*Your source code is indented, so that curly braces on the first column always open or close a function body.
 
*Your source code is indented, so that curly braces on the first column always open or close a function body.
*A function name is followed by a <tt>(</tt>. This is true in C, mostly true in C++ and Java, and true to a limited extend in Perl.
+
*A function name is followed by a <code>(</code>. This is true in C, mostly true in C++ and Java, and true to a limited extend in Perl.
   
 
This tip does not work for constructor definitions in C++ and Java, since they can contain many parentheses, for example:
 
This tip does not work for constructor definitions in C++ and Java, since they can contain many parentheses, for example:
Line 76: Line 76:
   
 
----
 
----
This is pretty cool if you forget to do <tt>va{</tt> (or similar) first, but text-object methods should be discussed as well.
+
This is pretty cool if you forget to do <code>va{</code> (or similar) first, but text-object methods should be discussed as well.
   
 
----
 
----

Latest revision as of 05:58, July 13, 2012

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 945 Printable Monobook Previous Next

created 2005 · complexity basic · author Lorenz Wegener · version 5.7


It is often useful to restrict the range of commands like s or g to the function one is currently editing. For C/C++ and Java,

:cmap ;tf ?^{??(?,/^}/

maps the keystrokes ;tf (this function) to the range of the function in which the cursor is currently located. It works by searching backwards for a { occurring in the first column, and then for a ( to find the start of the function. A } in the first column ends the function body.

For example, suppose you want to change the name of a function argument in the current function from i to ii:

int foo(int i,
        int j)
{
    // ...
    i++;
    return i;
}

In normal mode, type :;tf followed by the subsitution command s/\<i\>/ii/g. The command line now reads:

:?^{??(?,/^}/s/\<i\>/ii/g

Press Enter to execute the command. The function changes to:

int foo(int ii,
        int j)
{
    // ...
    ii++;
    return ii;
}

The range can be used with other commands too.

This tip assumes that:

  • Your source code is indented, so that curly braces on the first column always open or close a function body.
  • A function name is followed by a (. This is true in C, mostly true in C++ and Java, and true to a limited extend in Perl.

This tip does not work for constructor definitions in C++ and Java, since they can contain many parentheses, for example:

Foo(int bar, int baz):
    mBar(bar),
    mBaz(baz)
{}

CommentsEdit

I don't know a lot of people that actually put braces on the first column in Java, except for the class.


It is not the case in C++ either if you use namespaces.


It's used in K&R-Style


This is pretty cool if you forget to do va{ (or similar) first, but text-object methods should be discussed as well.


Around Wikia's network

Random Wiki