VSCode Neovim Integration
Neovim is a fork of Vim to allow greater extensibility and integration. This extension uses a fully embedded Neovim instance, no more half-complete Vim emulation! VSCode's native functionality is used for insert mode and editor commands, making the best use of both editors.
- π Almost fully feature-complete Vim integration by utilizing Neovim as a backend.
- π§ Supports custom
init.vim
and many Vim plugins. - π₯ First-class and lag-free insert mode, letting VSCode do what it does best.
- π€ Complete integration with VSCode features (lsp/autocompletion/snippets/multi-cursor/etc).
Table of Contents
- π§° Getting Started
- π‘ Tips and Features
- β‘οΈ API
- β¨οΈ Keybindings (shortcuts)
- π§° Developing
- β€οΈ Credits & External Resources
-
Install the vscode-neovim extension.
-
Install Neovim 0.9.0 or greater.
- Set the Neovim path in the extension settings. You must specify the full path to Neovim, like
"
C:\Neovim\bin\nvim.exe
" or "/usr/local/bin/nvim
". - The setting id is "
vscode-neovim.neovimExecutablePaths.win32/linux/darwin
", respective to your system.
- Set the Neovim path in the extension settings. You must specify the full path to Neovim, like
"
-
If you want to use Neovim from WSL, set the
useWSL
configuration toggle and specify the Linux path to the nvim binary.wsl.exe
Windows binary andwslpath
Linux binary are required for this.wslpath
must be available through$PATH
Linux env setting. Usewsl --list
to check for the correct default Linux distribution. -
Assign affinity value for performance improvement.
-
Go to Settings > Features > Extensions > Experimental Affinity.
Add an entry with item name
asvetliakov.vscode-neovim
and value 1.OR
-
Add to your
settings.json
:"extensions.experimental.affinity": { "asvetliakov.vscode-neovim": 1 },
-
Since many Vim plugins can cause issues in VSCode, it is recommended to start from an empty init.vim
. For a guide for
which types of plugins are supported, see troubleshooting.
Before creating an issue on Github, make sure you can reproduce the problem with an empty init.vim
and no VSCode
extensions.
To determine if Neovim is running in VSCode, add to your init.vim
:
if exists('g:vscode')
" VSCode extension
else
" ordinary Neovim
endif
In lua:
if vim.g.vscode then
-- VSCode extension
else
-- ordinary Neovim
end
To conditionally activate plugins, vim-plug
has a
few solutions. packer.nvim
and lazy.nvim
have built-in support for cond = vim.g.vscode
. See
plugins in the wiki for tips on configuring Vim plugins.
- On a Mac, the h, j, k and l movement keys may not repeat when held, to
fix this open Terminal and execute the following command:
defaults write com.microsoft.VSCode ApplePressAndHoldEnabled -bool false
. - To fix the remapped escape key not working in Linux, set
"keyboard.dispatch": "keyCode"
- File and editor management commands such as
:e
/:w
/:q
/:vsplit
/:tabnext
/etc are mapped to corresponding VSCode commands and behavior may be different (see below).- Do not use vim commands like
:w
in scripts/keybindings, they won't work. If you're using them in some custom commands/mappings, you might need to rebind them to call VSCode commands from Neovim withrequire('vscode-neovim').call()
(see API).
- Do not use vim commands like
- When you type some commands they may be substituted for another, like
:write
will be replaced by:Write
. - Scrolling is done by VSCode. C-d/C-u/etc are slightly different.
- Editor customization (relative line number, scrolloff, etc) is handled by VSCode.
- Dot-repeat (.) is slightly different - moving the cursor within a change range won't break the repeat.
sequence. In Neovim, if you type
abc<cursor>
in insert mode, then move the cursor toa<cursor>bc
and type1
here the repeat sequence would be1
. However, in VSCode, it would bea1bc
. Another difference is that when you delete some text in insert mode, dot repeat only works from right to left, meaning it will treat Del key as BS keys when running dot repeat.
If you get the "Unable to init vscode-neovim: command 'type' already exists" message, uninstall other VSCode extensions
that register the type
command (like VSCodeVim or
Overtype).
Make sure you have the extension running in its own thread using affinity (see installation).
Extensions that share the same affinity value are associated with a shared extension host (extension manager from VSCode). Performance issues arise when a number of extensions have the same host. On-going operations of one extension may slow down the operations of another. However, if an extension is assigned an affinity, its extension host runs in a separate worker thread. The operations of an extension with the host in one thread don't directly affect the operations of the extension with its host running in another.
If you have any performance problems (cursor jitter usually) make sure you're not using these kinds of extensions:
- Anything that renders decorators very often:
- Line number extensions (VSCode has built-in support for normal/relative line numbers)
- Indent guide extensions (VSCode has built-in indent guides)
- Brackets highlighter extensions (VSCode has built-in feature)
- VSCode extensions that delay the extension host like "Bracket Pair Colorizer"
- Vim plugins that increase latency and cause performance problems.
- Make sure to disable unneeded plugins, as many of them don't make sense with VSCode and may cause problems.
- You don't need any code, highlighting, completion, LSP plugins as well any plugins that spawn windows/buffers (nerdtree and similar), fuzzy-finders, etc.
- Many navigation/textobject/editing plugins should be fine.
If you're not sure, disable all other extensions, reload VSCode window, and see if the problem persists before reporting it.
Since VSCode is responsible for insert mode, custom insert-mode Vim mappings don't work. To map composite escape keys, put into your keybindings.json:
for jj
{
"command": "vscode-neovim.compositeEscape1",
"key": "j",
"when": "neovim.mode == insert && editorTextFocus",
"args": "j"
}
to enable jk add also:
{
"command": "vscode-neovim.compositeEscape2",
"key": "k",
"when": "neovim.mode == insert && editorTextFocus",
"args": "k"
}
Currently, there is no way to map both jk
and kj
, or to map jk
without also mapping jj
.
VSCode's jumplist is used instead of Neovim's. This is to make VSCode native navigation (mouse click, jump to definition, etc) navigable through the jumplist.
Make sure to bind to workbench.action.navigateBack
/ workbench.action.navigateForward
if you're using custom
mappings. Marks (both upper & lowercased) should work fine.
Command menu has the wildmenu completion on type. The completion options appear after 1.5s (to not bother you when you
write :w
or :noh
). Up/Down selects the option and Tab accepts it. See the gif:
Multiple cursors work in:
- Insert mode
- Visual line mode
- Visual block mode
To spawn multiple cursors from visual line/block modes type ma/mA or mi/mI (by default). The effect differs:
- For visual line mode, mi will start insert mode on each selected line on the first non whitespace character and ma will on the end of line.
- For visual block mode, mi will start insert on each selected line before the cursor block and ma after.
- mA/mI versions accounts for empty lines (only for visual line mode, for visual block mode they're same as ma/mi).
See gif in action:
The built-in multi-cursor support may not meet your needs. Please refer to the plugin vscode-multi-cursor.nvim for more multi-cursor features
Load the module:
local vscode = require('vscode-neovim')
vscode.action()
: asynchronously executes a vscode command.vscode.call()
: synchronously executes a vscode command.vscode.on()
: defines a handler for some Nvim UI events.vscode.has_config()
: checks if a vscode setting exists.vscode.get_config()
: gets a vscode setting value.vscode.update_config()
: sets a vscode setting.vscode.notify()
: shows a vscode message (see also Nvim'svim.notify
).vscode.to_op()
: A helper formap-operator
. See code_actions.lua for the usagevscode.get_status_item
: Gets a vscode statusbar item. Properties can be assigned, which magically updates the statusbar item.g:vscode_clipboard
: Clipboard provider using VSCode's clipboard API. Used by default when in WSL. See:h g:clipboard
for more details. Usage:let g:clipboard = g:vscode_clipboard
Asynchronously executes a vscode command. See Examples.
Parameters:
name
(string): The name of the action, generally a vscode command.opts
(table): Map of optional parameters:args
(table): List of arguments passed to the vscode command. If the command only requires a single object parameter, you can directly pass in a map-like table.- Examples:
action('foo', { args = { 'foo', 'bar', β¦ } })
action('foo', { args = { foo = bar, β¦ } })
- Examples:
range
(table): Specific range for the action. Implicitly passed in visual mode. Has three possible forms (all values are 0-indexed):[start_line, end_line]
[start_line, start_character, end_line, end_character]
{start = { line = start_line, character = start_character}, end = { line = end_line, character = end_character}}
restore_selection
(boolean): Whether to preserve the current selection. Only valid whenrange
is specified. Defaults totrue
.callback
: Function to handle the action result. Must have this signature:function(err: string|nil, ret: any)
err
is the error message, if anyret
is the result- If no callback is provided, error will be shown as a VSCode notification.
Synchronously executes a vscode command. See Examples.
Parameters:
name
(string): The name of the action, generally a vscode command.opts
(table): Same as vscode.action().timeout
(number): Timeout in milliseconds. The default value is -1, which means there is no timeout.
Returns: the result of the action
- Format selection (default binding):
xnoremap = <Cmd>lua require('vscode-neovim').call('editor.action.formatSelection')<CR> nnoremap = <Cmd>lua require('vscode-neovim').call('editor.action.formatSelection')<CR><Esc> nnoremap == <Cmd>lua require('vscode-neovim').call('editor.action.formatSelection')<CR>
- Open definition aside (default binding):
nnoremap <C-w>gd <Cmd>lua require('vscode-neovim').action('editor.action.revealDefinitionAside')<CR>
- Find in files for word under cursor (see the
vscode command definition
for the expected parameter format):
nnoremap ? <Cmd>lua require('vscode-neovim').action('workbench.action.findInFiles', { args = { query = vim.fn.expand('<cword>') } })<CR>
Currently, two built-in actions are provided for testing purposes:
_ping
returns"pong"
_wait
waits for the specified milliseconds and then returns"ok"
do -- Execute _ping asynchronously and print the result
vscode.action("_ping", {
callback = function(err, res)
if err == nil then
print(res) -- outputs: pong
end
end,
})
end
-- Format current document
vscode.action("editor.action.formatDocument")
do -- Comment the three lines below the cursor
local curr_line = vim.fn.line(".") - 1 -- 0-indexed
vscode.action("editor.action.commentLine", {
range = { curr_line + 1, curr_line + 3 },
})
end
do -- Comment the previous line
local curr_line = vim.fn.line(".") - 1 -- 0-indexed
local prev_line = curr_line - 1
if prev_line >= 0 then
vscode.action("editor.action.commentLine", {
range = { prev_line , prev_line },
})
end
end
do -- Find in files for word under cursor
vscode.action("workbench.action.findInFiles", {
args = { query = vim.fn.expand('<cword>') }
})
end
-- Execute _ping synchronously and print the result
print(vscode.call("_ping")) -- outputs: pong
-- Wait for 1 second and print the return value 'ok'
print(vscode.call("_wait", { args = { 1000 } })) -- outputs: ok
-- Wait for 2 seconds with a timeout of 1 second
print(vscode.call("_wait", { args = { 2000 } }), 1000)
-- error: Call '_wait' timed out
Currently no available events for user use.
Check if configuration has a certain value.
Parameters:
name
(string|string[]): The configuration name or an array of configuration names.
Returns:
boolean|boolean[]
: Returnstrue
if the configuration has a certain value,false
otherwise. Ifname
is an array, returns an array of booleans indicating whether each configuration has a certain value or not.
Get configuration value.
Parameters:
name
(string|string[]): The configuration name or an array of configuration names.
Returns:
unknown|unknown[]
: The value of the configuration. Ifname
is an array, returns an array of values corresponding to each configuration.
Update configuration value.
Parameters:
name
(string|string[]): The configuration name or an array of configuration names.value
(unknown|unknown[]): The new value for the configuration.target
("global"|"workspace"): The configuration target. Optional
Examples:
------------------
--- has_config ---
------------------
-- Check if the configuration "not.exist" exists
print(vscode.has_config("not.exist"))
-- Should return: false
-- Check multiple configurations
vim.print(vscode.has_config({ "not.exist", "existing.config" }))
-- Should return: { false, true }
------------------
--- get_config ---
------------------
-- Get the value of "editor.tabSize"
print(vscode.get_config("editor.tabSize")) -- a number
-- Get multiple configurations
vim.print(vscode.get_config({ "editor.fontFamily", "editor.tabSize" }))
-- Should return: { "the font family", "the editor tabSizse" }
---------------------
--- update_config ---
---------------------
-- Update the value of "editor.tabSize"
vscode.update_config("editor.tabSize", 16, "global")
-- Update multiple configurations
vscode.update_config({ "editor.fontFamily", "editor.tabSize" }, { "Fira Code", 14 })
Show a vscode notification
You can set vscode.notify
as your default notify function.
vim.notify = vscode.notify
Creates a status item
id
(string): The identifier of the item
local test = vscode.get_status_item('test')
test.text = 'hello' -- Show the text
test.text = '' -- Hide the item
test.text = nil -- Close the item
test.text = '' -- error: The status item "test" has been closed
Note: Since 1.0.0, vimscript functions are deprecated. Use the Lua api instead.
VSCodeNotify()
/VSCodeCall()
: deprecated, use Luarequire('vscode-neovim').call()
instead.VSCodeNotifyRange()
/VSCodeCallRange()
: deprecated, use Luarequire('vscode-neovim').call(β¦, {range:β¦})
instead.VSCodeNotifyRangePos()
/VSCodeCallRangePos()
: deprecated, use Luarequire('vscode-neovim').call(β¦, {range:β¦})
instead.
Default commands and bindings available for file/scroll/window/tab management:
- See vscode-scrolling.vim for scrolling commands reference
- See vscode-file-commands.vim for file commands reference
- See vscode-tab-commands.vim for tab commands reference
- See vscode-window-commands.vim for window commands reference
π‘ "With bang" refers to adding a "!" to the end of a command.
This document only mentions some special cases, it is not an exhaustive list of keybindings and commands. Use VSCode and Nvim features to see documentation and all defined shortcuts:
- Run the
Preferences: Open Keyboard Shortcuts
vscode command and search for "neovim" to see all keybindings. - Use the Nvim
:help
command to see the documentation for a given command or keybinding. For example try:help :split
or:help zo
.- Note that
:help
for<C-β¦>
bindings is spelledCTRL-β¦
. For example to see the help for<c-w>
, run:help CTRL-W
.
- Note that
- Search the online Nvim documentation: https://neovim.io/doc/user/
Every special (control/alt) keyboard shortcut must be explicitly defined in VSCode to send to neovim. By default, only bindings that are included by Neovim by default are sent.
To pass custom bindings to Neovim, for example C-h in normal mode, add to your keybindings.json:
{
"command": "vscode-neovim.send",
// the key sequence to activate the binding
"key": "ctrl+h",
// don't activate during insert mode
"when": "editorTextFocus && neovim.mode != insert",
// the input to send to Neovim
"args": "<C-h>"
}
There are three configurations for toggling keybindings:
ctrlKeysForInsertMode
: toggle ctrl keys for insert mode.ctrlKeysForNormalMode
: toggle ctrl keys for normal mode.editorLangIdExclusions
: disable keybindings defined by this extension in certain filetypes. Please note that this will not affect all keybindings.
If you find that these options are not working, you can manually modify the keybindings in VSCode (see below).
π‘ See Keybindings help to see all defined shortcuts and their documentation.
Key | VSCode Command |
---|---|
= / == | editor.action.formatSelection |
gh / K | editor.action.showHover |
gd / C-] | editor.action.revealDefinition Also works in vim help. |
gf | editor.action.revealDeclaration |
gH | editor.action.referenceSearch.trigger |
gO | workbench.action.gotoSymbol |
C-w gd / C-w gf | editor.action.revealDefinitionAside |
gD | editor.action.peekDefinition |
gF | editor.action.peekDeclaration |
Tab | togglePeekWidgetFocus Switch between peek editor and reference list. |
C-n / C-p | Navigate lists, parameter hints, suggestions, quick-open, cmdline history, peek reference list |
π‘ To specify the default peek mode, modify
editor.peekWidgetDefaultFocus
in your settings.
π‘ See Keybindings help to see all defined shortcuts and their documentation.
Key | VSCode Command |
---|---|
j or k | list.focusDown/Up |
h or l | list.collapse/select |
Enter | list.select |
gg | list.focusFirst |
G | list.focusLast |
o | list.toggleExpand |
C-u or C-d | list.focusPageUp/Down |
zo or zO | list.expand |
zc | list.collapse |
zC | list.collapseAllToFocus |
za or zA | list.toggleExpand |
zm or zM | list.collapseAll |
/ or Escape | list.toggleKeyboardNavigation |
π‘ See Keybindings help to see all defined shortcuts and their documentation.
Key | VSCode Command |
---|---|
r | renameFile |
d | deleteFile |
y | filesExplorer.copy |
x | filesExplorer.cut |
p | filesExplorer.paste |
v | explorer.openToSide |
a | explorer.newFile |
A | explorer.newFolder |
R | workbench.files.action.refreshFilesExplorer |
π‘ See Keybindings help to see all defined shortcuts and their documentation.
The following keybinding is set by default: When hover is invisible, K is sent to nvim(show hover); when hover is visible, press K again to focus the hover widget.
{
"command": "editor.action.showHover",
"key": "shift+k",
"when": "neovim.init && neovim.mode == normal && editorTextFocus && editorHoverVisible"
}
Key | VSCode Command |
---|---|
h | editor.action.scrollLeftHover |
j | editor.action.scrollDownHover |
k | editor.action.scrollUpHover |
l | editor.action.scrollRightHover |
gg | editor.action.goToTopHover |
G | editor.action.goToBottomHover |
C-f | editor.action.pageDownHover |
C-b | editor.action.pageUpHover |
The extension aliases various Nvim commands (:edit
, :enew
, :find
, :write
, :saveas
, :wall
, :quit
, etc.) to
equivalent vscode commands. Also their normal-mode equivalents (where applicable) such as C-w q, etc.
π‘ See Keybindings help to see all defined shortcuts and their documentation.
The extension aliases various Nvim tab commands (:tabedit
, :tabnew
, :tabfind
, :tabclose
, :tabnext
,
:tabprevious
, :tabfirst
, :tablast
) to equivalent vscode commands. Also their normal-mode equivalents (where
applicable) such as gt, etc.
π‘ See Keybindings help to see all defined shortcuts and their documentation.
The extension aliases various Nvim buffer/window commands (:split
, :vsplit
, :new
, :vnew
, :only
) to equivalent
vscode commands. Also their normal-mode equivalents (where applicable) such as C-w s, etc.
π‘ See Keybindings help to see all defined shortcuts and their documentation.
π‘ Split size distribution is controlled by
workbench.editor.splitSizing
setting. By default, it'sdistribute
, which is equal to vim'sequalalways
andeadirection = 'both'
(default).
To use VSCode command 'Increase/decrease current view size' instead of separate bindings for width and height:
workbench.action.increaseViewSize
workbench.action.decreaseViewSize
Copy this into init.vim
function! s:manageEditorSize(...)
let count = a:1
let to = a:2
for i in range(1, count ? count : 1)
call VSCodeNotify(to ==# 'increase' ? 'workbench.action.increaseViewSize' : 'workbench.action.decreaseViewSize')
endfor
endfunction
" Sample keybindings. Note these override default keybindings mentioned above.
nnoremap <C-w>> <Cmd>call <SID>manageEditorSize(v:count, 'increase')<CR>
xnoremap <C-w>> <Cmd>call <SID>manageEditorSize(v:count, 'increase')<CR>
nnoremap <C-w>+ <Cmd>call <SID>manageEditorSize(v:count, 'increase')<CR>
xnoremap <C-w>+ <Cmd>call <SID>manageEditorSize(v:count, 'increase')<CR>
nnoremap <C-w>< <Cmd>call <SID>manageEditorSize(v:count, 'decrease')<CR>
xnoremap <C-w>< <Cmd>call <SID>manageEditorSize(v:count, 'decrease')<CR>
nnoremap <C-w>- <Cmd>call <SID>manageEditorSize(v:count, 'decrease')<CR>
xnoremap <C-w>- <Cmd>call <SID>manageEditorSize(v:count, 'decrease')<CR>
Enabled by ctrlKeysForInsertMode
Default: ["a", "d", "h", "j", "o", "r", "t", "u", "w"]
π‘ See Keybindings help to see all defined shortcuts and their documentation.
Enabled by ctrlKeysForNormalMode
Default: ["a", "b", "d", "e", "f", "h", "i", "j", "k", "l", "o", "r", "t", "u", "v", "w", "x", "y", "z", "/", "]"]
π‘ See Keybindings help to see all defined shortcuts and their documentation.
Always enabled.
- Tab
- Ctrl keys:
<C-h>
<C-w>
<C-u>
<C-n>
<C-p>
<C-l>
<C-g>
<C-t>
- All
<C-r>
prefixed keys
π‘ See Keybindings help to see all defined shortcuts and their documentation.
There are two ways to customize colors:
- Set colors in nvim
Note: Due to the support for the syntax
option requiring processing of syntax highlights, all built-in highlight
groups may be overridden or cleared. Therefore, please do not link any highlights to the built-in highlight groups.
-
Set colors in vscode
References:
Please see CONTRIBUTING.md for details on how to contribute to this project.
- vim-altercmd - Used for rebinding default commands to call VSCode command.
- neovim nodejs client - NodeJS library for communicating with Neovim.
- VSCodeVim - Used for various inspiration.