1

I have this

os.environ.get("DB_NAME")

and I am trying to use the vi regex command

% s/os\.environ\.get\("DB_NAME"\)/db_name/gc

to match that. I used it to change flaskdb to os.environ.get("DB_NAME"), but this won't match to change it to db_name.

On further investigation I was able to match it by removing the parenthesis.

How come I was able to escape the parenthesis to match the first change but not the second set of changes?

1 Answer 1

2

Vi uses Ex patterns, which are a superset of POSIX Basic (obsolete, according to man re_format) REs. This means that \( and \) are used for grouping, not to represent literal parentheses (as in "modern", or Extended, REs).

You can also find this information in Vim at :help pattern, which explains that with the default magic settings ( is literal and \( opens a group. With \v very-magic, this is reversed to make grouping easier (among other changes).

So, I would write

:%substitute/os\.environ\.get("DB_NAME")/db_name/gc

to change the entire os.environ.get("DB_NAME") expression, or

:%substitute/DB_NAME/db_name/gc

to only affect the DB_NAME.

With Vim, I might use a pattern like os\.environ\.get("\zsDB_NAME\ze") to match DB_NAME within os.environ.get(""), and I might use a replacement like \L& to lowercase the match (change DB_NAME to db_name).

An alternate idea is to /DB_NAME, or press * if you are already on the word, then gugn to lowercase the match. Then . jumps to the next match and lowercases, while n only jumps to the next match (so you can skip and act by combining n and .).

1
  • Excellent very informative Jul 12, 2022 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.