Post

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.

Concepts

LSP (Language server protocol) is the key to configure Vim. In VScode, LSP is used for clangd to work correctly.

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 .

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

how to use :lua command

1
2
:lua =jit.version
:lua =vim.fn.stdpath('data')

how to use Neovim as a Lua playground

Open a new file and write some lua code, and then

1
:luafile %

TODO: I still could not figure out how to easily redirect the output to somewhere else.

pretty print

  • vim.inspect

% not working

Usually because you have unmatched braces in the comments or in a string like regex. In this case, you can use treesitter’s incremental selection.

treesitter is not working with vim -S session.vim

I once have this problem. The issue is the with markdown files. Manually do TSUpdate markdown fixed this issue.

how to format json

Format the whole file :%!jq .. Format a selection: After selection, then type :, then it will show up :'<,'>. Type :'<,'>!jq .. If you do not want to overwrite the current buffer, :'<,'>w !jq ..

This post is licensed under CC BY 4.0 by the author.