1

In my script, I am trying to open an URL in my default browser using

silent exec '!rundll32 url.dll,FileProtocolHandler '.a:url

where a:url is an URL-encoded string containing percent % signs. Vim obviously expands the percent sign with the current file name, resulting in an URL like

https://slovnik.seznam.cz/preklad/cesky_nemecky/bohatmyFile.txtc3myFile.txtbd

instead of

https://slovnik.seznam.cz/preklad/cesky_nemecky/bohat%c3%bd

(Remark: The encoded string stands for "bohatý" - "rich" in Czech)

How can I prevent VIM from expanding the percent sign?

1

1 Answer 1

1

You can use the escape() function to escape characters that are interpreted as meta-characters, such as % (also # and ! in the case of running an external command with :!.)

In your case:

silent exec '!rundll32 url.dll,FileProtocolHandler '.escape(a:url, '%#!')

One alternative, particularly as you're using silent, so not interested in the command's output, is to use the system() function call directly, which doesn't do as much command-line mangling as :! does.

call system('rundll32 url.dll,FileProtocolHandler '.a:url)

One advantage of system() (or systemlist()) is that you can get the output of the commands as a Vimscript string (or list of lines), if you wish to inspect or manipulate them after the command has finished running.

1
  • 1
    Thank you, the escape() function solved the problem. system() did not work, though. The browser did not open. Dec 12, 2020 at 22:39

Your Answer

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

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