1

I'd like to find and replace some text with other text which consist the result of the shell command. This is my trial, which is not working:

:40,43s/'height': 650/'height': system('python -c "import random; print(random.randint(500, 600))"')/g

Vim don't want to evaluate system('python -c "import random; print(random.randint(500, 600)') part.

Please help me to fix this

1 Answer 1

4

Your "shell command" is not correct!

  • you should at least check if those quotes (' and ") in your command are paired

  • you should at least check if all brackets (..) in your command are paired.

For the vim :s command, you should use \= to get the result of an expression. Read :h :s\= for details.

Your example:

:40,43s/\v('height':) 650/\=submatch(1)." ".system('python -c "import random; print(random.randint(500, 600))"')/

Your command was fixed in above line.

Sign up to request clarification or add additional context in comments.

1 Comment

Just a little edit to remove trailing newline when command returns a number: :68,77s/\v('height':) 650/\=submatch(1)." ".system('python -c "import random; print(random.randint(500, 600))"')[:-2]/

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.