3; ~
# endif ~
set_width(a); ~
With the cursor in the last line, "[#" moves to the first line. The "#ifdef"
- "#endif" block in the middle is skipped.
MOVING IN CODE BLOCKS
In C code blocks are enclosed in {}. These can get pretty long. To move to
the start of the outer block use the "[[" command. Use "][" to find the end.
This assumes that the "{" and "}" are in the first column.
The [{ command moves to the start of the current block. It skips over
pairs of {} at the same level. "]}" jumps to the end.
An overview:
function(int a)
+-> {
| if (a)
| +-> {
[[ | | for (;;) --+
| | +-> { |
| [{ | | foo(32); | --+
| | [{ | if (bar(a)) --+ | ]} |
+-- | +-- break; | ]} | |
| } <-+ | | ][
+-- foobar(a) | |
} <-+ |
} <-+
When writing C++ or Java, the outer {} block is for the class. The next level
of {} is for a method. When somewhere inside a class use "[m" to find the
previous start of a method. "]m" finds the next start of a method.
Additionally, "[]" moves backward to the end of a function and "]]" moves
forward to the start of the next function. The end of a function is defined
by a "}" in the first column.
int func1(void)
{
return 1;
+----------> }
|
[] | int func2(void)
| +-> {
| [[ | if (flag)
start +-- +-- return flag;
| ][ | return 2;
| +-> }
]] |
| int func3(void)
+----------> {
return 3;
}
Don't forget you can also use "%" to move between matching (), {} and [].
That also works when they are many lines apart.
MOVING IN BRACES
The [( and ]) commands work similar to [{ and ]}, except that they
work on () pairs instead of {} pairs.
>
[(
< <--------------------------------
<-------
if (a == b && (c == d || (e > f)) && x > y) ~
-------------->
--------------------------------> >
])
MOVING IN COMMENTS
To move back to the start of a comment use "[/". Move forward to the end of a
comment with "]/". This only works for `/* - */` comments.
>
+-> +-> /*
| [/ | * A comment about --+
[/ | +-- * wonderful life. | ]/
| */ <-+
|
+-- foo = bar * 3; --+
| ]/
/* a short comment */ <-+
<
==============================================================================
*29.4* Finding global identifiers
You are editing a C program and wonder if a variable is declared as "int" or
"unsigned". A quick way to find this is with the "[I" command.
Suppose the cursor is on the word "column". Type: >
[I
Vim will list the matching lines it can find. Not only in the current file,
but also in all included files (and files included in them, etc.). The result
looks like this: >
structs.h
1: 29 unsigned column; /* column number */
The advantage over using tags or the preview window is that included files are
searched. In most cases this results in the right declaration to be found.
Also when the tags file is out of date. Also when you don't have tags for the
included files.
However, a few things must be right for "[I" to do its work. First of all,
the 'include' option must specify how a file is included. The default value
works for C and C++. For other languages you will have to change it.
LOCATING INCLUDED FILES
Vim will find included files in the places specified with the 'path'
option. If a directory is missing, some include files will not be found. You
can discover this with this command: >
:checkpath
It will list the include files that could not be found. Also files included
by the files that could be found. An example of the output:
--- Included files not found in path --- ~
<io.h> ~
vim.h --> ~
<functions.h> ~
<clib/exec_protos.h> ~
The "io.h" file is included by the current file and can't be found.