1

I am working on a Legacy Code, where I am having statements like:

int array[MAX_SIZE_FOR_THIS_ARRAY];

In VIM, I want to replace the Text between [ & ] (including [ & ]) with ARRAY as follows:

int array_ARRAY_;

Please NOTE: 1. The Array Size Macro will be different for Different Arrays, hence we cannot use MAX_SIZE_FOR_THIS_ARRAY in our search pattern. 2. The name of variable "array" is also not fixed.

I need to search for [ ] only & replace [...] with ARRAY everywhere, in thousands of lines of code.

Can somebody please help me with this?

4

3 回答 3

8

While the cursor is anywhere inside the square brackets, you can use:

ca[_ARRAY_

To help you remember:

  • c change
  • a around
  • [ square brackets
  • _ARRAY_ with _ARRAY_
于 2013-08-06T12:29:29.780 回答
6

ca[ would be the answer if you are doing it with vim.

since you tagged the question with sed too, here is the way with sed:

kent$  echo "int array[MAX_SIZE_FOR_THIS_ARRAY];"|sed 's/\[[^]]*\]/_ARRAY_/'
int array_ARRAY_;

with your example,

sed 's/\[.*]/_ARRAY_/'

this works too.

于 2013-08-06T12:38:50.530 回答
4

You can do a substitution:

:%s/array\[MAX_SIZE_FOR_THIS_ARRAY\]/array_ARRAY_/g
于 2013-08-06T12:34:55.060 回答