vim: 折叠指令

折叠指令在查看大文件时比较有用,折叠的模式有三种:

  • 手动折叠
  • 按语法折叠
  • 按缩进折叠

很多时候,我们都按照语法折叠,比较简单,也不叫方便。但行走江湖,总会有些时候,需要手工折叠,怎能不了解下呢

//手动折叠。先按下zf,然后按下范围选择符号。
zf             手动折叠。比如zf iB
zf/string      折叠到指定string
zf[N]j         向下折叠N行
zf[N]k         向上折叠N行


zc             单个折叠当前域
zo             展开当前域折叠
zC             对光标所在范围内的所有嵌套的折叠点进行折叠
zO             对光标所在所在范围内所有嵌套的折叠点展开
zM             折叠文件中的所有代码
zR             取消文件中所有代码的折叠
zj             向下移动。到达下一个折叠的开始处。关闭的折叠也被计入。
zk             向上移动到前一折叠的结束处。关闭的折叠也被计入
zi            切换折叠与不折叠

//fold by indent
set foldmethod=indent

//fold mannually
set foldmethod=manual

//fold by syntax
set foldmethod=syntax

zc, zC和zM的区别

看一下就明白,假设当前代码如下,光标在38行,也就是for的作用域之内:

 27 func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
 28     assets := []Asset{
 29         {ID: "asset1", Color: "blue", Size: 5, Owner: "Tomoko", AppraisedValue: 300},
 30         {ID: "asset2", Color: "red", Size: 5, Owner: "Brad", AppraisedValue: 400},
 31         {ID: "asset3", Color: "green", Size: 10, Owner: "Jin Soo", AppraisedValue: 500},
 32         {ID: "asset4", Color: "yellow", Size: 10, Owner: "Max", AppraisedValue: 600},
 33         {ID: "asset5", Color: "black", Size: 15, Owner: "Adriana", AppraisedValue: 700},
 34         {ID: "asset6", Color: "white", Size: 15, Owner: "Michel", AppraisedValue: 800},
 35     }
 36
 37     for _, asset := range assets {
 38         assetJSON, err := json.Marshal(asset)
 39         if err != nil {
 40             return err
 41         }
 42
 43         err = ctx.GetStub().PutState(asset.ID, assetJSON)
 44         if err != nil {
 45             return fmt.Errorf("failed to put to world state. %v", err)
 46         }
 47     }
 48
 49     return nil
 50 }

zc效果,折叠了for语句:

 27 func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
 28     assets := []Asset{
 29         {ID: "asset1", Color: "blue", Size: 5, Owner: "Tomoko", AppraisedValue: 300},
 30         {ID: "asset2", Color: "red", Size: 5, Owner: "Brad", AppraisedValue: 400},
 31         {ID: "asset3", Color: "green", Size: 10, Owner: "Jin Soo", AppraisedValue: 500},
 32         {ID: "asset4", Color: "yellow", Size: 10, Owner: "Max", AppraisedValue: 600},
 33         {ID: "asset5", Color: "black", Size: 15, Owner: "Adriana", AppraisedValue: 700},
 34         {ID: "asset6", Color: "white", Size: 15, Owner: "Michel", AppraisedValue: 800},
 35     }
 36
 37     for _, asset := range assets {
 38 +---  9 lines: assetJSON, err := json.Marshal(asset)-------------------------
 47     }
 48
 49     return nil
 50 }

zC效果,折叠了整个函数,但没超出函数外:

 27 func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
 28 +-- 22 lines: assets := []Asset{--------------------------------------------
 50 }

zM效果,折叠了文件中所有:

27 func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
 28 +-- 22 lines: assets := []Asset{----------------------------------------
 51
 52
 53 func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, id string, color string, size int, owner string, appraisedValue int) erro    r {
 54 +-- 21 lines: exists, err := s.AssetExists(ctx, id)---------------------------
 75 }
 76
 77 
 78 func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, id string) (*Asset, error) {
 79 +-- 15 lines: assetJSON, err := ctx.GetStub().GetState(id)-------------------
 94 }
 95

范围选择

手动折叠的时候,灵活性非常大,但范围选择就比较复杂。

范围选择符为两部分,第一部由a,或者i表示,表示是否包含选择符号本身,比如说i(,则表示选择小括号的内容,不包括小括号,而a(,则表示包括小号,可见a范围>i范围。

第二部分为符号,默认的为b, B, b标示小括号,中括号,B标示大括号。还可以由其他自定义符号,如>, [, (, }等

两个部分组合起来就是a(,或者ib, iB等:

ab  //选择小括号(,包括小括号
aB  //选择大括号{,包括大括号
ib  //选择小括号(内部,不包括小括号
iB  //选择大括号{内部,不包括大括号
a"  //选择"",包括""
i"  //选择""括号内部,不包括""
])  //从当前选择到括号结束
[(  //从当前选择到括号开始

和折叠指令zf结合使用:

zf iB

Ref

  • https://vimhelp.org/motion.txt.html#ap



发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

About Me

一位程序员,会弹吉他,喜欢读诗。
有一颗感恩的心,一位美丽的妻子,两个可爱的女儿
mail: geraldlee0825@gmail.com
github: https://github.com/lisuxiaoqi
medium: https://medium.com/@geraldlee0825