RubenLaguna
RubenLaguna

Reputation: 24716

In vim, how can I prevent `#` in a variable to from being interpreted/expanded in user defined commands?

Currently I have the following user command (:h user-commands, :h :command)

:command -nargs=1 Browse !open <args>

The open command is a macOS cli utility that opens the browser.

When I invoke it with :Browse https://google.com/#my-fragment the # in the url will be expanded to the alternate (:h alternate-file , :h :_# , :h c_#), since there is no alternate it will error out with:

E194: No alternate file name to substitute for '#': !open 'https://google.com/#my-fragment'

In this case, and I want the <args> passed verbatim (no expansions), how can I prevent ALL expansions?

Upvotes: 1

Views: 60

Answers (1)

RubenLaguna
RubenLaguna

Reputation: 24716

You should use fnameescape() or shellescape() (:h fnameescape(), :h shellescape()) in conjunction with :execute :

:command -nargs=1 Browse silent execute '!open' shellescape(<q-args>,1)

See (:h :silent, :h :execute, :h cmdline-special)

Upvotes: 1

Related Questions