如何在访问vim时访问operator []访问时转换向量

时间:2016-04-22 19:13:37

标签: c++ vim

假设我要转换

var.at(i*w+j) = something;

进入

var[i*w+j] = something;

在vim中键入的正确命令是什么?我试过

:%s/\.at\(.*\)/[\1]/g

导致

var[(i*w+j) = something;]

有效但如果可以删除括号则会更好。

编辑:显示正确的结果尝试

2 个答案:

答案 0 :(得分:2)

  

在vim中键入的正确命令是什么?我试过

:%s/\.at\(.*\)/[\1]/g

问题是你逃离了parens。由于您的"magic"设置,\(\)适用于分组,而您希望匹配文字()

 :%s/\.at(\(.*\))/[\1]/g

适合我。 (请注意,您仍然需要分组)

答案 1 :(得分:2)

使用递归宏:

qqqqq/\.at(<CR>%"adi)F.c%[<Esc>"apa]<Esc>@qq@q

说明:

qq                                                   record macro `q`
  q                                                  end empty macro
   qq                                                record macro `q`
      /\.at(<CR>                                     search for `.at(`
                %                                    go to the matching closing `)`
                 "adi)                               delete inside the parenthesis into register `a`
                      F.                             go back to the previous `.`
                        c%                           change what’s in the matching parenthesis
                          [<Esc>                     with `[` and leave insert mode
                                "ap                  paste what’s in register `a`
                                   a]<Esc>           append `]` and leave insert mode
                                          @q         call the (currently empty) `q` macro recursively
                                            q        end macro
                                             @q      call the (now non-empty) macro `q`

这也正确处理var.at(foo(bar, baz)) = something;之类的内容。