-1

We know that in Linux vim, we can use

:%s/character/symbol/g

to replace any occurrence of 'character' with 'symbol' in a file.

But what if I want to replace patterns like:

defined($opt_ws_parser)
defined($opt_client)
defined($opt_server)
 ...

with only the part in the parentheses? That is:

$opt_ws_parser
$opt_client
$opt_server
...

How can I achieve that? I tried using "%s/defined($.)/$./g". It turned out that all the occurrences became $.*, its literal form, not retaining their original letters.

li_jessen
  • 49
  • 6
  • Please refer to the [`test` man page](https://man7.org/linux/man-pages/man1/test.1.html). – G.M. Dec 28 '22 at 10:20
  • The important thing to remember is that makefile recipes are not written in "makefile syntax". They are shell scripts. So if you want to understand the syntax of a makefile recipe you need to read the shell documentation not the make documentation. – MadScientist Dec 28 '22 at 17:29
  • The title of the proposed duplicate is disingenuous, but the answers explain exactly how to RTFM. – tripleee Dec 28 '22 at 18:53

1 Answers1

1
:%s/defined(\(\$.*\))/\1
  • % - Repeat for every line
  • s - Substitute
  • / - Start of pattern
  • defined( - Match the string "defined(" literally
  • \( - Beginning of capture group
  • \$ - Match "$"
  • .* - Match anything
  • \) - End of capture group
  • ) - Literally match ")"
  • / - End of pattern, start of substitution string
  • \1 - Reference to the first capture group (first expression between round brackets)

Sources:

Terra
  • 100
  • 1
  • 8