I am trying to replace positional arguments with variables in my bash script using Vim's substitute :scommand.

Example: I would like to replace the following text in a bash script:

python3 mymodule.py --dataversion $1


python3 mymodule.py --dataversion "${DATAVERSION}"

I have tried :s/\$1/"${DATAVERSION}" resulting in E486: Pattern not found: $1.

When I search the pattern instead of substituting, /\$1, the instances are detected. Why is this pattern different in Vim's search and susbstitute commands?

  • 3
    your vim command will only do the replacement on the current line -- prepend it a % if you want to do the search and replace in the whole file.
    – user313992
    Commented Dec 30, 2019 at 17:33
  • 2
    What mosvy said, also, you don't have to escape the $ as it is not the last character in the pattern.
    – Kusalananda
    Commented Dec 30, 2019 at 19:10

1 Answer 1


When you search, Vim will automatically consider the entire buffer (provided you haven't disabled the 'wrapscan' option - then it would only search until the end). Your :substitute command only considers the current line, as you've left out the :help :range, and this command (like most others) defaults to the current line only. To cover the entire buffer for your substitution, just prepend %, which is an abbreviation for 1,$:


Also, if there were multiple occurrences of $1 in a line, only the first one would be replaced. Append /g for a global substitution.

As mentioned in the comments, the $ does not need to be escaped (it only has special meaning at the end of the pattern), but it's fine to escape it, too (and avoids some thinking).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .