0

I want to VISUAL select a piece of text character-wise and pass it to an external command.

For example I want to select only the URL from the the text below

abcde www.google.com abcde

and open it in the default browser.

But after I VISUAL selected it and ran the command

:call system('xdg-open', @*)

nothing happened

But the register * was filled with the selected contents. I tested it with:

:echo @*
www.google.com

What is the right command to achieve it?

3

prototype of system is:

system({cmd} [, {input}])

according to :help system()

If {input} is a string it is written to a pipe and passed as
stdin to the command.  The string is written as-is, line
separators are not changed.

so your final command becomes something like this:

echo www.google.com | xdg-open

which doesn't make sense, as xdg-open doesn't read from stdin. you can call

echo system('xdg-open', @*)

to check error message.

At last you can call system like this:

call system('xdg-open ' . @*)
                     ^ don't miss space
0

One can also do :!xdg-open <C-r><C-w> to shell out and grab the word under the cursor.

I’m not sure how this handles a WORD like www.google.com; it may be that <C-W> is necessary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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