Embed Neovim in Chrome, Firefox & others.
Turn your browser¹ into a Neovim client (demos: justinmk , Sean Feng ).
¹ Firefox and Chrome are specifically supported. Other Chromium based browsers such as Brave, Vivaldi, Opera, and Arc should also work but are not specifically tested.
Just click on any textarea and it will be immediately replaced by an instance of Firenvim. To set the content of the now hidden textarea to the content of the Neovim instance, simply :w. If you want to close the Firenvim overlay and return to the textarea, use :q. If you selected an element where you expected the Firenvim frame to appear and it didn't, try pressing <C-e>.
Before installing anything please read SECURITY.md and make sure you're okay with everything mentioned. In the event you think of a way to compromise Firenvim, please send me an email (you can find my address on my website).
Install Firenvim as a regular Neovim plugin, then run the built-in post-install script.
{ 'glacambre/firenvim', build = ":call firenvim#install(0)" }Plug 'glacambre/firenvim', { 'do': { _ -> firenvim#install(0) } }call minpac#add('glacambre/firenvim', { 'type': 'opt', 'do': 'packadd firenvim | call firenvim#install(0)'})
if exists('g:started_by_firenvim')
packadd firenvim
endifvundle, others
Install the plugin as you usually would, then run this shell command:
$ nvim --headless "+call firenvim#install(0) | q"…lua if vim.g.started_by_firenvim == true then vim.o.laststatus = 0 else vim.o.laststatus = 2 end
Alternatively, you can detect when Firenvim connects to Neovim by using the `UIEnter` autocmd event:
```lua
vim.api.nvim_create_autocmd({'UIEnter'}, {
callback = function(event)
local client = vim.api.nvim_get_chan_info(vim.v.event.chan).client
if client ~= nil and client.name == "Firenvim" then
vim.o.laststatus = 0
end
end
})Similarly, you can detect when Firenvim disconnects from a Neovim instance with the UILeave autocommand.
The Neovim buffer loaded into a textarea is given a unique name. All buffers are named something like this: domainname_page_selector.txt (see the toFileName function).
This alows you to configure different settings by creating autocommands targeting/matching the buffername for that url/page/element. For example, this will set file type to markdown for all GitHub buffers:
vim.api.nvim_create_autocmd({'BufEnter'}, {
pattern = "github.com_*.txt",
command = "set filetype=markdown"
})
…
lua
vim.g.firenvim_config = {
globalSettings = { alt = "all" },
localSettings = {
[".*"] = {
cmdline = "neovim",
content = "text",
priority = 0,
selector = "textarea",
takeover = "always"
}
}
}With this configuration, takeover will be set to always on all websites. If we wanted to override this value on british websites, we could add the following lines to our init.vim. Notice how the priority of this new regex is higher than that of the .* regex:
vim.g.firenvim_config.localSettings["https?://[^/]+\\.co\\.uk/"] = { takeover = 'never', priority = 1 }The selector attribute of a localSetting controls what elements Firenvim automatically takes over. Here's the default value:
vim.g.firenvim_config.localSettings['.*'] = { selector = 'textarea:not([readonly], [aria-readonly]), div[role="textbox"]' }If you don't want to use Firenvim with rich text editors (e.g. Gmail, Outlook, Slack…) as a general rule, you might want to restrict Firenvim to simple textareas:
vim.g.firenvim_config.localSettings['.*'] = { selector = 'textarea' }Since selector is just a CSS selector, you have access to all of CSS's pseudo selectors, including :not(), which allows you to exclude elements that have certain attributes, like this:
vim.g.firenvim_config.localSettings['.*'] = { selector = 'textarea:not([class=xxx])' }
…
lua
vim.g.firenvim_config.localSettings['.*'] = { takeover = 'always' }Setting a global policy (.*) with narrower ones for specific websites is an easy way to build a whitelist/allowlist or blacklist/blocklist deciding what webxites Firenvim should work on.
You can chose between Neovim's built-in command line, firenvim's command line and no command line at all by setting the localSetting named cmdline to either neovim, firenvim or none, e.g.:
vim.g.firenvim_config.localSettings['.*'] = { cmdline = 'firenvim' }Choosing none does not make sense unless you have alternative way to display the command line such as noice.nvim.
The content localSetting controls how Firenvim should read the content of an element. Setting it to html will make Firenvim fetch the content of elements as HTML, text will make it use plaintext. The default value is text:
vim.g.firenvim_config.localSettings['.*'] = { content = 'html' }
…
lua
vim.g.firenvim_config = {
globalSettings = {
ignoreKeys = {
all = { '<C-->' },
normal = { '<C-1>', '<C-2>' }
}
}
}
…
lua
vim.fn['firenvim#eval_js']('alert("Hello World!")', 'MyFunction')You can move focus from the editor back to the page or the input field by calling firenvim#focus_page or firenvim#focus_input. Here's an example that does exactly this if you press <Esc> twice while in normal mode:
vim.api.nvim_set_keymap("n", "<Esc><Esc>", "<Cmd>call firenvim#focus_page()<CR>", {})There is also a function named firenvim#hide_frame() which will temporarily hide the Firenvim frame. You will then be able to bring the Neovim frame back either by unfocusing and refocusing the textarea or by using the keybinding to manually trigger Firenvim.
vim.api.nvim_set_keymap("n", "<C-z>", "<Cmd>call firenvim#hide_frame()<CR>", {})A function named firenvim#press_keys() will allow you to send key events to the underlying input field by taking a list of vim-like keys (e.g. a, <CR>, <Space>…) as argument. Note that this only "triggers" an event, it does not add text to the input field. For example if you'd like firenvim to send <CR> to the webpage when you press <CR> in the editor, you can use the following mapping which is useful with chat apps:
vim.api.nvim_create_autocmd({'BufEnter', {
pattern = "riot.im_*",
command = [[inoremap <CR> <Esc>:w<CR>:call firenvim#press_keys("<LT>CR>")<CR>ggdGa]],
})
…
lua
vim.api.nvim_create_autocmd({'TextChanged', 'TextChangedI'}, {
nested = true,
command = "write"
})Depending on how large the edited buffer is, this could be a little slow. This more sophisticated approach will throttle writes:
vim.api.nvim_create_autocmd({'TextChanged', 'TextChangedI'}, {
callback = function(e)
if vim.g.timer_started == true then
return
end
vim.g.timer_started = true
vim.fn.timer_start(10000, function()
vim.g.timer_started = false
vim.cmd('silent write')
end)
end
})Due to space constraints, the external command line covers part of the buffer. This can be a problem as sometimes Neovim will send a message that tells Firenvim to draw the command line, and then never send the message to tell Firenvim to stop displaying it. In order to work around this problem, a "cmdlineTimeout" configuration option has been implemented, which makes Firenvim hide the external command line after the cursor has moved and some amount of milliseconds have passed:
vim.g.firenvim_config = {
globalSettings = {
cmdlineTimeout = 3000
}
}
…
lua
vim.g.firenvim_config = {
localSettings = {
['.*'] = {
filename = '/tmp/{hostname}_{pathname%10}.{extension}'
}
}
}
…
lua
vim.g.firenvim_config = {
globalSettings = {
['<C-w>'] = 'noop',
['<C-n>'] = 'default'
}
}Note that on Firefox on Linux some keyboard shortcuts might not be overridable. I circumvent this issue by running a patched version of Firefox (note: once Firefox is patched, you won't need to setup webextension keyboard shortcuts).
:editor command.No open issues yet, or sync has not completed.