Vim
Missing features:
- Cannot explore 3rd library file structure. In vscode, you can view them directly.
Smooth scrolling is very important.
When I started using nvim in a remote server, I immediately realized that copying to clipboard does not work. A quick search led me to osc52. The readme file provides enough background about this issue and the solution. Also, any other plugins that use clipboard breaks over ssh. For example, I use gitlinker a lot, and it breaks. I forked the repo and fix the issue for myself. I probably will raise a PR to add OSC-52 support.
Build
1
2
3
cd neovim
make
ln -s build/compile_commands.json compile_commands.json
We need this build step to see those generated files.
Lua
What level of Lua knowledge is required to efficiently work with Neovim? I believe you only need to read https://neovim.io/doc/user/lua.html .
Vim APIs
stdlib
Neovim has a lua stdlib. Basically, it is a bunch of C functions or submodules that are exported to a Lua module vim
. Because this module is auto loaded, we do not need to explicitly import it. Therefore, we can write vim.print(...)
in our Lua code directly.
How this binding is done? It is done by the standard Lua function lua_pushcfunction
or luaL_register
. The relevant code is here. You can see that print
is set as a global function, and many other functions are added. A few lines blow it calls nlua_state_add_stdlib()
which loaded a few more functions and submodules. We can use below script to print out all stdlib.
1
2
3
4
5
for key, value in pairs(vim) do
if type(value) == 'function' then
print(key)
end
end
vim.api
Neovim also defines a bunch of api functions under a sub module api
under vim
module. See code. The corresponding file lua_api_c_bindings.generated.c
is auto generated. I copy part of its content as below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void nlua_add_api_functions(lua_State *lstate)
FUNC_ATTR_NONNULL_ALL
{
lua_createtable(lstate, 0, 175);
lua_pushcfunction(lstate, &nlua_api_nvim_get_autocmds);
lua_setfield(lstate, -2, "nvim_get_autocmds");
lua_pushcfunction(lstate, &nlua_api_nvim_create_autocmd);
lua_setfield(lstate, -2, "nvim_create_autocmd");
lua_pushcfunction(lstate, &nlua_api_nvim_del_autocmd);
lua_setfield(lstate, -2, "nvim_del_autocmd");
lua_pushcfunction(lstate, &nlua_api_nvim_clear_autocmds);
lua_setfield(lstate, -2, "nvim_clear_autocmds");
...
lua_pushcfunction(lstate, &nlua_api_nvim_win_text_height);
lua_setfield(lstate, -2, "nvim_win_text_height");
lua_setfield(lstate, -2, "api");
}
Event loop
Tips
- The
=
trick::lua =jit.version
. Adding=
in front of the command means printing out the result. It avoids addingvim.print()
. - When debugging plugins, we sometimes need to print out the content of some variable. There is a universal pretty print function:
vim.inspect
. So,vim.notify(vim.inspect(...))
is pretty much what you need. - Run current lua buffer
:luafile %
. So we can use Neovim as a Lua playground. - Format JSON.
- For the whole file
:%!jq .
. Also, for malformed json, use jfc instead.%!jfc -r
. - Format a selection: After selection, then type
:
, then it will show up:'<,'>
. Type:'<,'>!jq .
.
- For the whole file
- Three ways to add a char to the end of a range of lines.
:<range>s/$/<char>/g
:<range>normal A<char>
Ctrl-v
visual block mode -> type$
-> typeA
and<char>
- Set LSP log level
:lua vim.lsp.log.set_level('debug')
. This new log level will write all requests and responses to the lsp log.