2021-10-11

Vim-W3M Plugin

W3M is a text-based browser and pager that can well complement a terminal-centric workspace. My simple note-taking application has the option to open hyperlinks and emails directly on the default Windows browser and email client, similar to gx or gf for a VIM buffer.

Instead of the default browser, I would like to have a text browser like W3M that opens my hyperlinks. One solution that seems to be rather straightforward consists of rearranging the following vim-shell repository. The interesting feature of this project is that it can open not only hyperlinks and email addresses but also local files and folders.

From the vim-shell plugin, I have extracted just the part related to the Open command into a new plugin (vim-w3m) suitable for gVIM running inside a Windows terminal and W3M compiled with MINGW64.

Get w3m for Windows

On a Windows machine with a working MSYS2/MINGW64 installation the W3M binary can be obtained with:

   $ pacman -S w3m

The default location is C:\msys64\usr\bin, if you have installed MINGW64/MSYS2 in a different directory you can find the location for your set-up with:

  $ whereis w3m
  w3m: /usr/bin/w3m.exe /usr/libexec/w3m /usr/share/w3m /usr/share/man/man1/w3m.1.gz

The paths are relative to MINGW64/MSYS2 root directory (default C:\msys64).

GVIM configuration

The following lines should be added to the .vimrc file:

   Plug 'https://gitlab.com/Sevepy/vim-w3m'
   Plug 'xolox/vim-misc'

and then call :PlugInstall from VIM command mode which it will download the two plugins inside the local VIM's configuration folder (in my case %USERPROFILE%\.vim\plugged).

Custom configuration

In the .vimrc it's also possible to specify a different path for the W3M executable, and a different start page, if needed.

 let g:w3m_binary_path = 'C:\\msys64\\usr\\bin'
 let g:w3m_start_page = 'https://tessarinseve.pythonanywhere.com/nws/index.html'

VIM moves the cursor on a hyperlink, email address, or local file/folder path, and then use the keyboard shortcut

 <leader>oo 

to open them in a new terminal window.

The start page will be displayed when the cursor is located in an empty buffer line.

Project's takeaways

Opening VIM with the following option:

   $ vim -V9vim.log

will create a log file (vim.log) in the current directory where eventual errors with the plugin's code are recorded.