Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running MarkdownPreview throws uncaught exception: Error: spawn xdg-open ENOENT in WSL 2 with terminal Vim #199

Closed
nickjj opened this issue May 28, 2020 · 18 comments

Comments

@nickjj
Copy link

nickjj commented May 28, 2020

Hi,

I'm using Vim 8.1 inside of Ubuntu 20.04 running in WSL 2 using the latest pull from master at the time of writing this comment.

Inside of WSL 1 this plugin worked great. It would open my default browser when running that command without having to do anything custom.

But now within WSL 2 it fails to run :MarkdownPreview with:

[markdown-preview.vim] uncaught exception: Error: spawn xdg-open ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
    at onErrorNT (internal/child_process.js:469:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

Turns out you need to install sudo apt-get install -y xdg-utils, and then things work fine. Although there is a ~5 second delay before it opens where as with WSL 1 it was instant.

Might be worth adding something to the docs or wiki for such an issue in a WSL section.

@iamcco iamcco closed this as completed in b255e65 May 29, 2020
@iamcco
Copy link
Owner

iamcco commented May 29, 2020

I have update README and improve the error message

@mrbeardad
Copy link

Turns out you need to install sudo apt-get install -y xdg-utils, and then things work fine. Although there is a ~5 second delay before it opens where as with WSL 1 it was instant.

It doesn't work well for me now in WSL2.
I have changed ip and port, when I execute :MarkdownPreview, my chrome on windows10 will open with site http://172.30.39.164:8854/page/1, and warn me :

This site can’t be reached
172.30.39.164 refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

What can I do to solve it?

@nickjj
Copy link
Author

nickjj commented Aug 2, 2020

I didn't change my ip:port and it works here with an address of http://127.0.0.1:8576/page/1, although there is still a multi-second delay for it to open. I'm running commit faacfbcefe88badb0a5bee3290ec690373552f2b of this plugin.

I would try disabling Windows defender / firewall to see if that fixes anything.

@mrbeardad
Copy link

hahahahahhahaha... I have solved it!

I have execute lsof -i in WSL2, and this plugin is listening localhost but not IP address, WSL2 treats Windows as a stand-alone host, so the connection from Windows is refused.

Add let g:mkdp_open_to_the_world = 1 to init.vim will solve this problem!

@iamcco 可以把这加文档里啦,老哥。换windows可搞死我了,wsl(我死了):joy:

@iamcco
Copy link
Owner

iamcco commented Aug 3, 2020

@mrbeardad README 有链接链到这里了

@ndaman
Copy link

ndaman commented Aug 26, 2020

I just wanted to mention, using WSL2 (Arch if that makes a difference) the above methods did not work for me.

What DID work was to install wslu and set
g:mkdp_browser='wslview'
in my vimrc (well, init.vim). I have not experienced the multi-second delays the others are talking about using this approach.

@StarfishC
Copy link

There is my solution:
sudo ln -s /mnt/c/Program\ Files\ (x86)/Microsoft/Edge/Application/msedge.exe /usr/bin/edge
let let g:mkdp_browser = 'edge'

@xeon-chow
Copy link

xeon-chow commented Sep 16, 2020

I just wanted to mention, using WSL2 (Arch if that makes a difference) the above methods did not work for me.

What DID work was to install wslu and set
g:mkdp_browser='wslview'
in my vimrc (well, init.vim). I have not experienced the multi-second delays the others are talking about using this approach.

@ndaman it works on my debian wsl2, mostly perfect except that my terminal window will go blank after i start preview, then it gradually reloads everything when i make some mouse or keyboard movement. no big deal but kinda annoying.

@qingbyin
Copy link

@XeonChow it can be solved by using wsl-open instead of wslview. Install wsl-open and set g:mkdp_browser='wsl-open'

If you prefer a general vim setting to a WSL specified one, use this trick without modifying g:mkdp_browser

sudo rm $(which xdg-open)
sudo ln -s $(which wsl-open) /usr/local/bin/xdg-open

Just run :MarkdownPreview and you should be good to go.

@xeon-chow
Copy link

@XeonChow it can be solved by using wsl-open instead of wslview. Install wsl-open and set g:mkdp_browser='wsl-open'

If you prefer a general vim setting to a WSL specified one, use this trick without modifying g:mkdp_browser

sudo rm $(which xdg-open)
sudo ln -s $(which wsl-open) /usr/local/bin/xdg-open

Just run :MarkdownPreview and you should be good to go.

@qingbyin
thank yuo so much, i followed your instruction and it works flawlessly.

@ZihaoXingUP
Copy link

I didn't change my ip:port and it works here with an address of http://127.0.0.1:8576/page/1, although there is still a multi-second delay for it to open. I'm running commit faacfbcefe88badb0a5bee3290ec690373552f2b of this plugin.

I would try disabling Windows defender / firewall to see if that fixes anything.

when i tap ':MarkdownPreview' on WSL1 vim. my chrome on windows10 will open with site 'http://127.0.0.1:9410/markdown/16300587241?L2hvbWUvemloYW94aW5nL3NoaW55X3Byb2plY3RzL1N3aW1NYXAvd3d3L3Rlc3QubWQmL2hvbWUvemloYW94aW5nLy52aW0vYnVuZGxlL21hdGhqYXgtc3VwcG9ydC1mb3ItbWtkcC9tYXRoamF4LXN0YXRpYy8='
it can't access this website, what should i do to solve this problem?

@ZihaoXingUP
Copy link

@XeonChow it can be solved by using wsl-open instead of wslview. Install wsl-open and set g:mkdp_browser='wsl-open'

If you prefer a general vim setting to a WSL specified one, use this trick without modifying g:mkdp_browser

sudo rm $(which xdg-open)
sudo ln -s $(which wsl-open) /usr/local/bin/xdg-open

Just run :MarkdownPreview and you should be good to go.

when i tap ':MarkdownPreview' on WSL1 vim. my chrome on windows10 will open with site 'http://127.0.0.1:9410/markdown/16300587241?L2hvbWUvemloYW94aW5nL3NoaW55X3Byb2plY3RzL1N3aW1NYXAvd3d3L3Rlc3QubWQmL2hvbWUvemloYW94aW5nLy52aW0vYnVuZGxlL21hdGhqYXgtc3VwcG9ydC1mb3ItbWtkcC9tYXRoamF4LXN0YXRpYy8='
it can't access this website, what should i do to solve this problem?

@ZihaoXingUP
Copy link

There is my solution:
sudo ln -s /mnt/c/Program\ Files\ (x86)/Microsoft/Edge/Application/msedge.exe /usr/bin/edge
let let g:mkdp_browser = 'edge'

when i tap ':MarkdownPreview' on WSL1 vim. my edge on windows10 will open with site 'http://127.0.0.1:9410/markdown/16300587241?L2hvbWUvemloYW94aW5nL3NoaW55X3Byb2plY3RzL1N3aW1NYXAvd3d3L3Rlc3QubWQmL2hvbWUvemloYW94aW5nLy52aW0vYnVuZGxlL21hdGhqYXgtc3VwcG9ydC1mb3ItbWtkcC9tYXRoamF4LXN0YXRpYy8='
it can't access this website, what should i do to solve this problem?

@nickjj
Copy link
Author

nickjj commented Aug 27, 2021

If anyone is looking for a solution that doesn't require installing wsl-open, modifying your vimrc or creating a symlink you can set up this alias: alias open="/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe"

I've been doing this on WSL 2 for months and running :MarkdownPreview works.

@johnsonkee
Copy link

I use this plugin in WSL1, ubuntu. Every time I input :MarkdownPreview, the statusline shows " openning 'http://127.0.0.1:8781/page/5' ... done! (xdg-open)". And I have to type this address in my browser to view my markdown.

I would like to ask how to make the preview page show automatically? Thank you very much.

@timsu92
Copy link

timsu92 commented Sep 13, 2022

@ndaman it works on my debian wsl2, mostly perfect except that my terminal window will go blank after i start preview, then it gradually reloads everything when i make some mouse or keyboard movement. no big deal but kinda annoying.

@XeonChow Another way to solve is to redraw! in the last line of the function pointed by g:mkdp_browserfunc

This is working on WSL2(Ubuntu 22.04) on a x64 machine.
This opens using Chrome by default, and switch to Edge if Chrome not found.

function! MdpOpenPreview(url) abort
	let l:mdp_browser = '/mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe'
	let l:mdp_browser_opts = '--new-window'
	if !filereadable(substitute(l:mdp_browser, '\\ ', ' ', 'g'))
		let l:mdp_browser = '/mnt/c/Program\ Files\ \(x86\)/Microsoft/Edge/Application/msedge.exe'
		let l:mdp_browser_opts = '--new-window'
	endif
	execute join(['silent! !', l:mdp_browser, l:mdp_browser_opts, a:url])
	redraw!
endfunction

let g:mkdp_browserfunc = 'MdpOpenPreview'

@timsu92
Copy link

timsu92 commented Sep 13, 2022

I use this plugin in WSL1, ubuntu. Every time I input :MarkdownPreview, the statusline shows " openning 'http://127.0.0.1:8781/page/5' ... done! (xdg-open)". And I have to type this address in my browser to view my markdown.

I would like to ask how to make the preview page show automatically? Thank you very much.

@johnsonkee I guess your settings is broken.

When I was testing, I encountered this as well. And it turned out that I made mistakes. Furthermore, even if silent is not used, error messages regarding to the call to browser hide behind vim.

@Moxie1337
Copy link

@ndaman it works on my debian wsl2, mostly perfect except that my terminal window will go blank after i start preview, then it gradually reloads everything when i make some mouse or keyboard movement. no big deal but kinda annoying.

@XeonChow Another way to solve is to redraw! in the last line of the function pointed by g:mkdp_browserfunc

This is working on WSL2(Ubuntu 22.04) on a x64 machine. This opens using Chrome by default, and switch to Edge if Chrome not found.

function! MdpOpenPreview(url) abort
	let l:mdp_browser = '/mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe'
	let l:mdp_browser_opts = '--new-window'
	if !filereadable(substitute(l:mdp_browser, '\\ ', ' ', 'g'))
		let l:mdp_browser = '/mnt/c/Program\ Files\ \(x86\)/Microsoft/Edge/Application/msedge.exe'
		let l:mdp_browser_opts = '--new-window'
	endif
	execute join(['silent! !', l:mdp_browser, l:mdp_browser_opts, a:url])
	redraw!
endfunction

let g:mkdp_browserfunc = 'MdpOpenPreview'

You solve my problem! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests