1

I'm using neovim on mac os, in case that information is of use.

In the shell I have some aliases such as

alias g='open HTTP://google.com

For sites that I access regularly, I would like to be able to have a similar shortcut available within vim, but I'm not sure how to go about this. I'm on mac os (which is why the command is usually open).

I have tried:

:! open https://vi.stackexchange.com/

Which does open stack exchange in a new tab (which is good), but it also puts some output into my vim command pallette which is not wanted.

:! open https://vi.stackexchange.com/
[No write since last change]

Press ENTER or type command to continue

The above is in the command pallette, which makes me think that there's likely a better approach.

Edit

Perhaps the following is the correct approach:

:silent exec "!open https://vi.stackexchange.com/"

Edit 2

The following is an example of something that doesn't work unless it's quoted:

doesn't work:

:silent !open https://www.youtube.com/watch?v=XA2WjJbmmoM&feature=youtu.be&t=2578

works:

:silent !open 'https://www.youtube.com/watch?v=XA2WjJbmmoM&feature=youtu.be&t=2578'
2

I don't think you need exec in your command. !open <URL> in itself is a complete command recognized by Vim. So simply,

:silent !open https://vi.stackexchange.com/ is sufficient!

EDIT: As OP mentioned edits so here is a detailed explanation of why URLs containing ampersand & won't work without using either quotes or escape character.

In OP's edit 2 part 1, the ampersand & is used to repeat last substitute in Ex-mode. So using that URL without quotes, Vim will interpret it as multiple Ex-mode commands separated by &. You can get the desired output by replacing & with \& inside URL. But I would suggest to use quotes.

| improve this answer | |
  • thanks - I have added an example of where things break as well (so - i think perhaps this is solved now? Unless there's anything additional to do around best practices or such) – baxx Jul 29 at 0:19
  • 2
    That's right. In Edit 2, part 1 the ampersand & is used to repeat last substitute in Ex-mode. So using that URL without quotes, Vim will interpret it as multiple Ex-mode commands separated by &. You can get the desired output by replacing & with \& inside URL. But I would suggest to use quotes. – weirdsmiley Jul 29 at 0:24
  • cheers - if you could stick the comment into the answer that'd make it more complete imo, thanks though – baxx Jul 29 at 0:41

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.