Original link: http://youngxhui.top/2023/07/neovim-%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97-%E4%B8%80/
In the world of programming, there are two ancient artifacts. One is called “Emacs, the editor of God”, and the other is called “vim, the editor of God”. From the birth of these two editors to the present, the jihad has never ended. Both vim and emacs are constantly evolving and developing. Gradually, a dazzling new star appeared on the branch of vim. He is neovim.
what is neovim
neovim From the name: Nova’s vim. According to the official instructions: nvim is a fork of vim, focusing on scalability and ease of use. A large number of vim users have migrated to nvim, and the charm of vim is the classic shortcut keys and rich plug-in system, which have been inherited from nvim. At the same time, it has built-in LSP and added new features such as asynchronous IO.
Here are some key features and benefits of Neovim:
- Compatibility: Neovim is a compatible version of Vim that works almost seamlessly with existing Vim configuration files and plugins. It supports Vim’s commands and modes of operation, so Vim users can easily switch to Neovim.
- Asynchronous support: Neovim introduces a mechanism for asynchronous task processing, allowing the editor to execute long-running tasks in the background without blocking the user interface. This allows plugins and scripts to handle time-consuming operations more efficiently, improving the responsiveness of the editor.
- Modern plugin system: Neovim provides a more flexible and easily extensible plugin system. It supports plug-ins written in various programming languages, and provides an interface for communicating with external processes, so that plug-ins can interact with other programs.
- Active community: Neovim has an active community that is constantly driving the development and improvement of the editor. The community provides a large collection of plugins, themes and configuration files, as well as contributions and support for new features.
Overall, Neovim is a very powerful and flexible text editor designed to provide a modern editing experience with a high degree of customization for users. Both beginners and experienced Vim users can benefit from Neovim’s functions and features.
basic configuration
The configuration in neovim can be configured through init.vim or init.lua. Most of the current configurations use lua. This article will also configure nvim through lua. Don’t worry if you don’t know how to use lua yet, lua can be used quickly. You can view the lua tutorial directly through :h lua-guide
.
init.lua
In mac/linux, the configuration file is located in the ~/.config/nvim/
directory, and in the windows system, the directory is located in %USERPROFILE%\AppData\Local\nvim\
. When nvim starts, it will load the init.lua file in this directory, so you only need to configure it in this file.
First, set the encoding format of the file and unify it to UTF-8. Only need to add relevant configuration in init.lua.
|
|
What do o
and g
here mean?
vim.o
means that global options can be set, while vim.g
is for global settings.
Similar related methods include vim.wo
setting window-local option, vim.bo
setting buffer-local option, etc.
Set tab, tab defaults to 4 spaces.
|
|
You can also add other basic configurations, such as displaying line numbers, etc. The specific configurations are as follows
|
|
plug-in system
vim/neovim is still active after years of development, and the plug-in system has contributed a lot. Abundant plug-ins can turn neovie into an IDE in minutes.
During the development of the community, vim’s plug-in system has also continued to grow. Currently, vim’s plug-ins can basically cover all aspects of editing. For neovim, you can query related plugins through the awesome-neovim project.
Whether it is vim or neovim, it does not have a plug-in manager itself, unlike vscode or other editors, which can easily add, delete or update checks. Of course, you don’t need to worry about it, as various masters have developed multiple plug-in management tools. Currently, for neovim, the well-known plug-in management tools include packer.nvim and lazy.nvim (note: not to be confused with LazyVim ).
This article will use lazy.nvim as a plugin management tool for plugin management. If you use packer.nvim as a plug-in manager, it does not affect the reading, you can skip the plug-in manager chapter.
plugin manager
lazy.nvim is a popular plug-in management tool, the installation is actually very simple. Add related codes in init.lua.
|
|
After saving and exiting, enter nvim again. nvim will check whether there is lazy, if not, it will clone from github.
Through the :Lazy
command, if you see the image below, it proves that lazy.nvim is installed successfully.
With lazy, you can quickly install, update and uninstall plugins later.
first plugin
In order to verify whether the lazy function is normal, first install a theme plugin to see. As the theme plugin, I choose catppuccin.nvim .
You can find out how to install and add the plugin to the lazy configuration through the catppuccin documentation.
|
|
Exit neovim and enter again, you will see that lazy is downloading related plugins.
After the installation is complete, we found that our main body has been applied, but when we exited and entered again, we found that the theme color has returned to the default color scheme. We only need to add a sentence at the end of the configuration file to set the relevant color scheme.
|
|
Among them, catppuccin has many configurations, so I won’t go into details here. You can check the configuration content in the relevant warehouse.
With the plugin manager, it is possible to add different plugins.
configured organization
After the simple understanding above, all the configurations are currently written in init.lua. When more plug-ins are added, the code in lua will continue to increase. Then when it needs to be modified, querying related configurations will also become However, through the characteristics of lua, we can split different configuration files to achieve high cohesion and low coupling.
For lua, different lua scripts can be imported through the require
function.
Now create a lua folder under the same init.lua directory, and create two lua files in it, namely basic.lua and plugin.lua.
|
|
The structure is shown in the above directory tree, and then the configuration content can be copied to related files, the basic configuration is placed in basic.lua, and the lazy related configuration is copied to plugin.lua.
Finally, import the relevant configuration files into init.lua.
|
|
Among them, for the configuration of the color theme, we can create a theme.lua file to store the configuration related to the theme color matching, and finally don’t forget to add this file in init.lua.
The final directory structure is as follows:
|
|
shortcut key binding
In order to make vim more convenient and convenient for our use, we need to bind our shortcut keys for some operations, which will make your operation even more efficient. According to the above configuration rules, create a new file as keymapping.lua, and add it in init.lua.
Know shortcut keys
An important point of vim is that you can quickly and efficiently complete tasks through shortcut keys, pointing to where to type, and in configuring shortcut keys, there are actually several keys that we need to be familiar with, which are divided into Ctrl
, Alt
and Shift
keys. This is not to let everyone know where these keys are, but to say that these keys are very commonly used in configuration, and these keys are often abbreviated in configuration.
key position | abbreviation |
---|---|
Ctrl | C- |
shift | S- |
Alt | A- |
These three keys will be very common in the configuration. Of course, not only these three key positions can find relevant abbreviations, you can see all key position descriptions through :h key-notations
.
In addition, there is no Alt key for mac, so we need to modify the Option key. For this part, please refer to Appendix/Modify Alt key .
Leader key
The leader key is a very important key for vim, it can be said to be one of the most frequently used keys in vim. The leader, as the name suggests, is in the leading position. Generally, it is used as the forerunner of the shortcut key combination. When using other keys, press the leader first. For the leader, vim does not stipulate who is the leader, as long as you want, which key can also be the leader. Generally, keys such as space are set as leader keys.
The above are the basic knowledge before we configure. Let’s start setting up the relevant shortcut keys.
set shortcut key
Create a new file under the lua folder, keybinding.lua, and add it in init.lua, require("keybinding")
The first is the leader key mentioned above. Here I use a space as the leader key.
|
|
For the difference between mapleader
and maplocalleader
, please refer to https://luciaca.cn/posts/vimscript-learning-on-leaders related documents.
In neovim, it needs to be set through vim.keymap.set()
function. This function needs to pass in four parameters, namely mode
, lhs
, rhs
, opts
.
parameter name | illustrate |
---|---|
mode | The abbreviation of mode, the common ones are n(normal), i(insert), v(view), etc. |
lhs | Can be understood as the corresponding button |
rhs | corresponding function |
opts | related settings |
|
|
In this way, you can customize and add related buttons.
appendix
Modify the Alt key
iterm2
In setting, in Profiles-Keys, set Left Option Key to Esc+.
Alacritty
Edit window.option_as_alt
in the configuration file of alacritty
|
|
References
Why Emacs and Vim are called two artifacts
This article is reproduced from: http://youngxhui.top/2023/07/neovim-%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97-%E4%B8%80/
This site is only for collection, and the copyright belongs to the original author.