markdown-preview.nvim: Running MarkdownPreview throws uncaught exception: Error: spawn xdg-open ENOENT in WSL 2 with terminal Vim

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.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (2 by maintainers)

Most upvoted comments

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’

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.

@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'

I have update README and improve the error message

@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

@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.