Chinaunix首页 | 论坛 | 博客
  • 博客访问: 810819
  • 博文数量: 118
  • 博客积分: 2067
  • 博客等级: 大尉
  • 技术积分: 1751
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-17 14:27
文章存档

2016年(1)

2013年(1)

2012年(3)

2011年(26)

2010年(47)

2009年(40)

分类: LINUX

2010-12-17 14:50:29

VIM手册中文版。

1.去除空行:
:%s/^\n//g

2.去除连续多个空行,但保留一个空行:
:%s/^\n$//g

3.替换匹配的部分内容:
:%s/\(abc \)aa\(def\)/\1xx\2/g
结果为:所有abcaadef的字符串被匹配为:abcxxdef
4. 删除多余的空格
行末:$
行首:^
空格:\s

行末空格:\s\+$
行首空格:^\+\s
\+表示一个或多个。
:49,62s/^\s\+//g 删除49,62行之间开头的1个或多个空格。
 
*fold.txt* For Vim version 7.2. Last change: 2007 May 11


VIM REFERENCE MANUAL by



*Folding* *folding*

You can find an introduction on in chapter 28 of the user manual.
||

1. Fold methods ||
2. Fold commands ||
3. Fold options ||
4. Behavior of folds ||

{Vi has no Folding}
{not available when compiled without the feature}

==============================================================================

1. Fold methods *fold-methods*

The method can be set with the option.

When setting to a value other than "manual", all folds are
deleted and new ones created. Switching to the "manual" method doesn't remove
the existing folds. This can be used to first define the folds automatically
and then change them manually.

There are six methods to select folds:
manual manually define folds
indent more indent means a higher fold level
specify an to define folds
folds defined by highlighting
folds for unchanged text
marker folds defined by markers in the text



MANUAL *fold-manual*

Use commands to manually define the fold regions. This can also be used by a
that parses text to find folds.

The level of a fold is only defined by its nesting. To increase the fold
level of a fold for a range of lines, define a fold inside that has the
same lines.

The manual folds are lost when you the file. To save the folds use
the || command. The view can be restored later with ||.



INDENT *fold-indent*

The folds are automatically defined by the indent of the lines.

The foldlevel is computed from the indent of the line, divided by the
(rounded down). A sequence of lines with the same or higher fold
level form a fold, with the lines with a higher level forming a nested fold.

The nesting of folds is limited with .

Some lines are ignored and get the fold level of the line above or below ,
whatever is the lowest. These are empty or white lines and lines starting
with a character in . White space is skipped before checking for
characters in . For C use "" to ignore preprocessor lines.

When you want to ignore lines in another way, use the '' method. The
|| function can be used in to get the indent of a line.



EXPR *fold-expr*

The folds are automatically defined by their foldlevel, like with the "indent"
method. The value of the option is evaluated to get the foldlevel
of a line. Examples:
This will create a fold for all consecutive lines that start with a :
:set foldexpr=getline(v:lnum)[0]==\"\\t\"
This will call a function to compute the fold level:
:set foldexpr=MyFoldLevel(v:lnum)
This will make a fold out of paragraphs separated by blank lines:
:set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
this does the same:
:set foldexpr=getline(v:lnum-1)=~'^\\s*$'&&getline(v:lnum)=~'\\S'?'>1':1

Note that backslashes must be used to characters that "" handles
differently (space, backslash, double , etc., see ||).

These are the conditions with which the is evaluated:
- The current buffer and are set for the line.
- The variable "" is set to the line number.
- The result is used for the fold level in this way:
value meaning
0 the line is not in a fold
1, 2, .. the line is in a fold with this level
-1 the fold level is undefined, use the fold level of a
line before or after this line, whichever is the
lowest.
"" use fold level from the previous line
"a1", "a2", .. add one, two, .. to the fold level of the previous
line
"s1", "s2", .. subtract one, two, .. from the fold level of the
previous line
"<1", "<2", .. a fold with this level ends at this line
">1", ">2", .. a fold with this level starts at this line

It is not required to the start (end) of a fold with ">1" ("<1"), a fold
will also start (end) when the fold level is higher (lower) than the fold
level of the previous line.

There must be no side effects from the . The text in the buffer,
cursor position, the search patterns, etc. must not be changed.
You can change and restore them if you are careful.

If there is some error in the , or the resulting value isn't
recognized, there is no error message and the fold level will be zero.
For debugging the option can be set to "msg", the error will
be visible then.

Note: Since the has to be evaluated for every line, this fold
method can be very slow!

Try to avoid the "", "" and "" return values, since Vim often has to search
backwards for a line for which the fold level is defined. This can be slow.

|| can be useful to compute a fold level relative to a previous
fold level. But note that may return -1 if the level is not known
yet. And returns the level at the start of the line, while a fold might
end in that line.



SYNTAX *fold-syntax*

A fold is defined by items that have the "fold" argument. ||

The fold level is defined by nesting folds. The nesting of folds is limited
with .

Be careful to specify proper syncing. If this is not done right, folds
may differ from the displayed highlighting. This is especially relevant when
using patterns that match more than one line. In of doubt, try using
brute-force syncing:
:syn sync fromstart



DIFF *fold-diff*

The folds are automatically defined for text that is not part of a change or
close to a change.

This method only works properly when the option is set for the current
and changes are being displayed. Otherwise the whole buffer will be
one big fold.

The option can be used to specify the context. That is, the number
of lines between the fold and a change that are not included in the fold. For
example, to use a context of 8 lines:
:set diffopt=filler,context:8
The default context is six lines.

When is also set, Vim will attempt to keep the same folds open in
other , so that the same text is visible.



MARKER *fold-marker*

Markers in the text tell where folds start and end. This allows you to
precisely specify the folds. This will allow and putting a fold,
without the risk of including the wrong lines. The option is
normally set such that the text before the marker shows up in the folded line.
This makes possible to give a name to the fold.

Markers can have a level included, or can use matching pairs. Including a
level is easier, you don't have to add end markers and avoid problems with
non-matching marker pairs. Example:
/* global variables {{{1 */
int varA, varB;

/* functions {{{1 */
/* funcA() {{{2 */
void funcA() {}

/* funcB() {{{2 */
void funcB() {}

A fold starts at a "{{{" marker. The following number specifies the fold
level. What happens depends on the difference between the current fold level
and the level given by the marker:
1. If a marker with the same fold level is encountered, the previous fold
ends and another fold with the same level starts.
2. If a marker with a higher fold level is found, a nested fold is started.
3. if a marker with a lower fold level is found, all folds up to and including
this level end and a fold with the specified level starts.

The number indicates the fold level. A zero cannot be used (a marker with
level zero is ignored). You can use "}}}" with a digit to indicate the level
of the fold that ends. The fold level of the following line will be one
than the indicated level. Note that Vim doesn't look back to the level of the
matching marker (that would take too much time). Example:

{{{1
fold level here is 1
{{{3
fold level here is 3
}}}3
fold level here is 2

You can also use matching pairs of "{{{" and "}}}" markers to define folds.
Each "{{{" increases the fold level by one, each "}}}" decreases the fold
level by one. Be careful to keep the markers matching! Example:

{{{
fold level here is 1
{{{
fold level here is 2
}}}
fold level here is 1

You can mix using markers with a number and without a number. A useful way of
doing this is to use numbered markers for large folds, and unnumbered markers
locally in a function. For example use level one folds for the sections of
your file like "structure definitions", "local variables" and "".
Use level 2 markers for each definition and function, Use unnumbered markers
inside . When you make changes in a function to split up folds, you
don't have to renumber the markers.

The markers can be set with the option. It is recommended to
keep this at the default value of "{{{,}}}", so that files can be exchanged
between Vim users. Only change when is required for the file (e.g.,
contains markers from another editor, or the default markers cause
trouble for the language of the file).


*fold-create-marker*
"" can be used to create a fold defined by markers. Vim will insert the
markers for you. Vim will append the start and end marker, specified with
. The markers are appended to the end of the line.
is used if isn't empty.
This does not work properly when:
- The line already contains a marker with a level number. Vim then doesn't
know what to .
- Folds nearby use a level number in their marker which gets in the way.
- The line is inside a comment, isn't empty and nested
comments don't work. For example with C: adding /* {{{ */ inside a comment
will truncate the existing comment. Either put the marker before or after
the comment, or add the marker manually.
Generally it's not a good idea to let Vim create markers when you already have
markers with a level number.


*fold-delete-marker*
"" can be used to delete a fold defined by markers. Vim will delete the
markers for you. Vim will search for the start and end markers, specified
with , at the start and end of the fold. When the text around the
marker matches with , that text is deleted well.
This does not work properly when:
- A line contains more than one marker and one of them specifies a level.
Only the first one is removed, without checking if this will have the
desired effect of the fold.
- The marker contains a level number and is used to start or end several folds
at the same time.

==============================================================================

2. Fold commands *fold-commands* *E490*

All commands start with "". Hint: the "" looks like a folded piece
of paper, if you look at from the side.


CREATING AND DELETING FOLDS

*zf* *E350*
zf{motion} or
{Visual}zf Operator to create a fold.
This only works when is "manual" or "marker".
The new fold will be closed for the "manual" method.
will be set.
Also see ||.


*zF*
Create a fold for N lines. Works like "".


:{range}fo[ld] *:fold* *:fo*
Create a fold for the lines in {range}. Works like "".


*zd* *E351*
Delete one fold at the cursor. When the cursor is on a folded
line, that fold is deleted. Nested folds are moved one level
up. In mode all folds (partially) in the selected area
are deleted. Careful: This easily deletes more folds than you
expect and there is no .
This only works when is "manual" or "marker".
Also see ||.


*zD*
Delete folds recursively at the cursor. In mode all
folds (partially) in the selected area and all nested folds in
them are deleted.
This only works when is "manual" or "marker".
Also see ||.


*zE* *E352*
Eliminate all folds in the .
This only works when is "manual" or "marker".
Also see ||.


OPENING AND CLOSING FOLDS

A fold smaller than will always be displayed like was open.
Therefore the commands below may work differently on small folds.


*zo*
Open one fold under the cursor. When a is given, that
many folds deep will be opened. In mode one level of
folds is opened for all lines in the selected area.


*zO*
Open all folds under the cursor recursively. Folds that don't
contain the cursor line are unchanged.
In mode opens all folds that are in the selected
area, also those that are only partly selected.


*zc*
Close one fold under the cursor. When a is given, that
many folds deep are closed. In mode one level of folds
is closed for all lines in the selected area.
will be set.


*zC*
Close all folds under the cursor recursively. Folds that
don't contain the cursor line are unchanged.
In mode closes all folds that are in the selected
area, also those that are only partly selected.
will be set.


*za*
When on a closed fold: open . When folds are nested, you
may have to use "" several times. When a is given,
that many closed folds are opened.
When on an open fold: close and set . This
will only close one level, since using "" again will open
the fold. When a is given that many folds will be
closed (that's not the same "" that many
times).


*zA*
When on a closed fold: open recursively.
When on an open fold: close recursively and set
.


*zv*
cursor line: Open just enough folds to make the line in
which the cursor is located not folded.


*zx*
Update folds: Undo manually opened and closed folds: re-apply
, then "": cursor line.


*zX*
Undo manually opened and closed folds: re-apply .


*zm*
Fold more: Subtract one from . If was
already zero nothing happens.
will be set.


*zM*
Close all folds: set to 0.
will be set.


*zr*
Reduce : Add one to .


*zR*
Open all folds. This sets to highest fold level.


*:foldo* *:foldopen*
:{range}foldo[pen][!]
Open folds in {range}. When [!] is added all folds are
opened. Useful to see all the text in {range}. Without [!]
one level of folds is opened.


*:foldc* *:foldclose*
:{range}foldc[lose][!]
Close folds in {range}. When [!] is added all folds are
closed. Useful to hide all the text in {range}. Without [!]
one level of folds is closed.


*zn*
Fold none: reset . All folds will be open.


*zN*
Fold normal: set . All folds will be they
were before.


*zi*
Invert .


MOVING OVER FOLDS

*[z*
Move to the start of the current open fold. If already at the
start, move to the start of the fold that contains . If
there is no containing fold, the command fails.
When a is used, repeats the command N times.


*]z*
Move to the end of the current open fold. If already at the
end, move to the end of the fold that contains . If there
is no containing fold, the command fails.
When a is used, repeats the command N times.


*zj*
Move downwards to the start of the next fold. A closed fold
is counted one fold.
When a is used, repeats the command N times.
This command can be used after an ||.


*zk*
Move upwards to the end of the previous fold. A closed fold
is counted one fold.
When a is used, repeats the command N times.
This command can be used after an ||.


EXECUTING COMMANDS ON FOLDS


:[range]foldd[oopen] {cmd} *:foldd* *:folddoopen*
Execute {cmd} on all lines that are not in a closed fold.
When is given, only these lines are used.
Each time {cmd} is executed the cursor is positioned on the
line is executed for.
This works like the "" command: First all lines that
are not in a closed fold are marked. Then the {cmd} is
executed for all marked lines. Thus when {cmd} changes the
folds, this has no influence on where is executed (except
when lines are deleted, of course).
Example:
:folddoopen s/end/loop_end/ge
Note the use of the "" flag to avoid getting an error message
where "end" doesn't match.


:[range]folddoc[losed] {cmd} *:folddoc* *:folddoclosed*
Execute {cmd} on all lines that are in a closed fold.
Otherwise like "".

==============================================================================

3. Fold *fold-options*


COLORS *fold-colors*

The colors of a closed fold are set with the Folded group ||. The
colors of the fold column are set with the FoldColumn group ||.
Example to set the colors:

:highlight Folded guibg=grey guifg=blue
:highlight FoldColumn guibg=darkgrey guifg=white



FOLDLEVEL *fold-foldlevel*

is a number option: The higher the more folded regions are open.
When is 0, all folds are closed.
When is positive, some folds are closed.
When is very high, all folds are open.
is applied when is changed. After that manually folds can be
opened and closed.
When increased, folds above the new level are opened. No manually opened
folds will be closed.
When decreased, folds above the new level are closed. No manually closed
folds will be opened.



FOLDTEXT *fold-foldtext*

is a string option that specifies an . This
is evaluated to obtain the text displayed for a closed fold. Example:

:set foldtext=v:folddashes.substitute(getline(v:foldstart),'/\\*\\\|\\*/\\\|{{{\\d\\=','','g')

This shows the first line of the fold, with "/*", "*/" and "{{{" removed.
Note the use of backslashes to avoid some characters to be interpreted by the
"" command. It's simpler to define a function and call that:

:set foldtext=MyFoldText()
:function MyFoldText()
: let line = getline(v:foldstart)
: let sub = substitute(line, '/\*\|\*/\|{{{\d\=', '', 'g')
: return v:folddashes . sub
:endfunction

Evaluating is done in the ||. The current is set to
the that displays the line. Errors are ignored.

The default value is ||. This returns a reasonable text for most
types of . If you don't like , you can specify your own
. It can use these special Vim :
line number of first line in the fold
line number of last line in the fold
a string that contains dashes to represent the
foldlevel.
the foldlevel of the fold

In the result a TAB is replaced with a space and unprintable characters are
made into printable characters.

The resulting line is truncated to fit in the , never wraps.
When there is room after the text, is filled with the character specified
by .

Note that backslashes need to be used for characters that the "" command
handles differently: Space, backslash and double-quote. ||



FOLDCOLUMN *fold-foldcolumn*

is a number, which sets the width for a column on the side of the
to indicate folds. When is zero, there is no foldcolumn. A normal
value is 4 or 5. The minimal useful value is 2, although 1 still provides
some information. The maximum is 12.

An open fold is indicated with a column that has a '' at the top and '|'
characters below . This column stops where the open fold stops. When folds
nest, the nested fold is one character right of the fold it's contained in.

A closed fold is indicated with a ''.

Where the fold column is too narrow to display all nested folds, digits are
shown to indicate the nesting level.

The mouse can also be used to open and close folds by clicking in the
fold column:
- Click on a '' to open the closed fold at this row.
- Click on any other non-blank character to close the open fold at this row.


OTHER OPTIONS

: Open all folds while not set.
: Expression used for "" .
: Characters used for "indent" .
: Defined markers used for "marker" .
: Name of the current method.
: Minimum number of screen lines for a fold to be
displayed closed.
: Maximum nesting for "indent" and "" .
: Which kinds of commands open closed folds.
: When the folds not under the cursor are closed.

==============================================================================

4. Behavior of folds *fold-behavior*

When moving the cursor upwards or downwards and when , the cursor
will move to the first line of a sequence of folded lines. When the cursor is
already on a folded line, moves to the next unfolded line or the next
closed fold.

While the cursor is on folded lines, the cursor is always displayed in the
first column. The ruler does show the actual cursor position, but since the
line is folded, cannot be displayed there.

Many commands handle a sequence of folded lines like an empty line.
For example, the "" command stops once in the first column.

When in mode, the cursor line is never folded. That allows you to see
what you type!

When using an , a closed fold is included a whole. Thus ""
deletes the whole closed fold under the cursor.

For commands the range is adjusted to always start at the first line of a
closed fold and end at the last line of a closed fold. Thus this command:
:s/foo/bar/g
when used with the cursor on a closed fold, will replace "foo" with "" in
all lines of the fold.
This does not happen for || and ||.

When editing a buffer that has been edited before, the last used
settings are used again. For manual the defined folds are restored.
For all methods the manually opened and closed folds are restored.
If this buffer has been edited in this , the values from back then are
used. Otherwise the values from the where the buffer was edited last
are used.


阅读(3278) | 评论(0) | 转发(0) |
0

上一篇:ext2 结构

下一篇:linux内存管理总结(转)

给主人留下些什么吧!~~