views:

62

answers:

2

I had an array in my code that's andI I used an index c[i]. I wanted to change c[i] and write g[i][j] in that place. I tried to use the command %s/c[i]/g[i][j]/g but did something unusual.

How can I do such replacement in Vim? Thanks in advance.

+7  A: 

Since [ and ] are special characters in regular expressions, you need to escape them:

%s/c\[i\]/g[i][j]/g
Lukáš Lalinský
+7  A: 

Put \V into your search expression to go into "very not magic" mode. It can go anywhere in the expression. This makes it so that the only special character in your expression is the backslash. If you're not doing regex matching, or matching begin or end of line, it'll save you a lot of hassle.

%s/\Vc[i]/g[i][j]/g
dash-tom-bang