edit-code-block.nvim
is an extension for Neovim that uses tree-sitter
to parse code injections in any type of file that supports them, and provides
an edit buffer with the code block and the correct file type, so the edit
can use language dependent tools like LSP. It can be downloaded
here.
It supports language blocks that are embedded in markdown
, javascript
or
css
blocks in HTML
, etc. The only requirement is to have the tree-sitter
parsers installed for those languages.
The plugin uses tree-sitter
’s LanguageTree
through the lua
vim.treesitter.languagetool
module to detect injected language blocks in
the parent file, and, when invoked, it generates a buffer with the contents of
the block that contains the cursor. The buffer is linked to the parent, so when
you save it (:w
), the changes go to the parent. The buffer can be hidden and
reused later, or discarded with the usual commands.
For languages not supported by Treesitter, or parsers that don’t provide code
injection queries, you can still select some text and specify the file type to
apply (EditCodeBlockSelection
).
edit-code-block.nvimm
requires a version of Neovim with a recent tree-sitter
version enabled (probably 0.8 or newer, I tested it on 0.9). It doesn’t depend on
any other extensions. But, of course, you need to have enabled in your configuration
all the tree-sitter parsers for the languages you will use. For example, if you
are interested in markdown
, lua
, and python
, you need to enable the
three parsers in your Neovim configuration.
Install using Plug.
Plug 'dawsers/edit-code-block.nvim'
and setup and configure using lua:
require('ecb').setup {
wincmd = 'split', -- this is the default way to open the code block window
}
Command | Description |
---|---|
EditCodeBlock | Open the block in a window specified by wincmd |
EditCodeBlockOrg | Open the org-mode block in a window specified by wincmd |
EditCodeBlockSelection | Open the selection in a window specified by wincmd |
Both EditCodeBlock
and EditOrgCodeBlock
accept an optional argument to
override the default wincmd
, and provide auto-completion for those arguments.
For example:
:EditCodeBlock tabnew
:EditCodeBlock split
:EditCodeBlock leftabove vsplit
:EditCodeBlock rightbelow split
...
EditCodeBlockSelection
works on selections, and requires at least one
argument, the file type to apply to the new buffer. Additional arguments are
understood as the wincmd
:'<,'>EditCodeBlockSelection lua
:'<,'>EditCodeBlockSelection python tabnew
:'<,'>EditCodeBlockSelection cpp rightbelow split
...
There are no default key bindings.