2

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?

2 Answers 2

5

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-r><C-a> is necessary

2
  • 1
    I think you mean <C-r><C-a> rather than <C-W>, don't you?
    – user98761
    Jun 11, 2022 at 23:31
  • @user98761 yep, thanks
    – D. Ben Knoble
    Jun 12, 2022 at 0:30

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.