Vim's folding, smart shrink

Source: Internet
Author: User
Tags case statement manual

Folding:

The folding commands in Vim start with Z and have 6 folding methods: Manual,indent,expr,syntax,diff,marker. Among them, manual the most basic, its command is universal, mastered the basic command of manual, folding is almost. To confirm that the collapse function is on: set foldenable.

To collapse the basic operation (the current cursor is collapsed): za (toggle folding State), ZC (folding off, is folded up), ZD (delete folding), when a, C, D capitalization, the action is recursive. Create folding is the ZF combined with the cursor Move command (place the cursor on the opening parenthesis of the function, with zf%, which conveniently folds the function). Z0 recursively opens the fold.

Folding inside there is a basic concept, that is, the folding level, is set the value of the Foldlevel option, ZM will foldlevel 0,zm, ZR respectively make foldlevel decrement and increment. Foldlevel with the indent type of folding, even if the so-called outline folding (set foldmethod=indent), it according to the automatic indentation shiftsidth the value of folding, the outer layer of the indentation is the No. 0 level, thus to the number of miles, Outline folding collapses The indentation in the Foldlevel hierarchy. This folding with ZR, ZM, see the code is more convenient.

: The Set foldcolumn=n command defines the width of the left folding bar, which is 0 by default. This column uses the "|" Displays the collapsed hierarchy.

: Set Foldmethod=syntax, which is a syntax-defined collapse, collapses the corresponding curly braces in the C code.

Indent

VIM offers 4 types of indentation: Set Indenttype. Indenttyep can be autoindent, smartindent, Cindent, indentexpr. Among them, autoindent and VI autoindent similar, smartindent is to do a bit of strengthening, can recognize the basic C syntax, as follows:

A, the next line after {

b, lines starting with the keywords contained in the cinwords option

C, new line ending with}

D, the cursor on the line containing the}, with the capital Letter O command, you can create a new row, automatic indentation

Cindent and indentexpr are better suited for C programmers. Cindent defines the indentation required in general C code, while indentexpr is more powerful, more flexible, and can arbitrarily define its own indentation style. The cindent action is defined by three options:

1, Cinkeys, defines a number of keyboard key combinations, when Vim encounters them will re-estimate the current row of indentation, the value of the option is separated by commas: for example, 0{,0},0),:, 0#,!^f,o,o,e, where 0x means the beginning of a line is the character X is, re-estimate the current indentation, for 0#, The indentation distance is the same as the line at the beginning of the previous #, #define就很方便了; for a colon, if it is a segment label or Case statement, the indentation is recalculated.!x indicates that when the X key is pressed, the current row indent is recalculated, note that the key is in insert mode, and the default is Ctrl+f o indicates that when any row is created, the indentation of the new row is estimated, whether it is the carriage return of the insert mode or the command mode o command, O represents a new row created on top of the current line, and E is the else until the last e of else, VIM does not recognize the situation.

In addition, Cinkeys has a dedicated keyword, such as =word and =~word, which, once matched string word appears in a new line, re-estimates the indentation, =~word and =word, which is not present, estimates the indentation and ignores the case.

2, cinwords, define some keywords, enter these keywords, the next line will trigger additional indentation, the default value is: If,else,while,do,for,switch. These keywords are case-sensitive.

3. cinoptions, this option controls Vim's behavior of re-indenting lines of text in the context of C. : Set cinoptions= option value, it has more option values, the following list some common.

Fn,{n, set to: Set CINOPTIONS=FN,{N,FN is defined as a non-nested curly brace "{" The indentation distance, by default, 0,{and F's behavior is the same, but it applies to the nested opening curly brace "{", the default is also 0.

}n, which defines the offset of the curly brace "}" at the end, defaults to 0.

: N,=n,bn, this indentation of the three control case statements: N causes Vim to indent the label of the case by N characters (starting with the corresponding switch statement), and by default a shiftwidth,=n is the row of cases with respect to the indentation, By default, SHIFTWIDTH,BN defines the position of the break statement, which defaults to 0, which is aligned with the corresponding case.

N,*n, these 2 settings scan lines,) n set vim to find the number of lines with non-closing parentheses, by default 20,*n is set to look for the end of the comment scan line, the default is 30.

Cinoptions already has the comparison full default value, basically does not need us to revise.

Summary: Cinkeys and Cinwords define the keys and actions that trigger vim to re-estimate the indentation, and Vim makes or re-indents according to the settings in the cinoption.

Indentexpr the settings back to overwrite cindent. Directory $vimruntime/indent, there are a number of programming languages corresponding to the indentation format files, loaded in the. vimrc file are available. command to turn off indentation: filetype indent off

When pasting text, automatic indentation may make the text very messy, as long as: Set paste, you can paste plain text, not be indented, with: Set nopasete Restore Auto-indentation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.