“Windows Subsystem for Linux” detection from within your .vimrc

The aptly named “Windows Subsystem for Linux” has become increasingly powerful over the past year and I found myself thinking about it when keeping my dotfiles up to date. While many Unix oriented configs and helpers will work right out of the box, programs that have to deal with specific quirks of the terminal emulator might still behave weirdly on WSL, vim is one of those. While the problem has been identified and a workaround is known, maintaining the portablility of my .vimrc is still a priority, so I had to find a way to set ttymouse to sgr only when running on WSL.

As of now, the only reliable way to detect if you are running on WSL is to check the content of /prov/version or /proc/sys/kernel/osrelease.

In the end, with a bit of vimscript voodoo, this is the hack I came up with:

" Check if we're on WSL
let wsl=system("grep -q Microsoft /proc/version")

if strlen(wsl)
    " Better support of the mouse in the WSL terminal
    set ttymouse=sgr
    " Better support of the mouse in terminal
    if has("mouse_sgr")
        set ttymouse=urxvt
        set ttymouse=xterm2

This little snippet, as well as many other vim stuff can be found in my dotfiles for anyone to browse or take inspiration from.

Enter your desired user name/password and after your comment has been reviewed by an admin it will be posted and your account will be enabled. If you are already registered please login before posting.