Neovim Getting Started Guide (1)

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.

 1 2
 vim.g . encoding = "UTF-8" vim.o . fileencoding = "UTF-8"

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.

 1 2 3 4
 vim.o . tabstop = 4 vim.bo . tabstop = 4 vim.o . softtabstop = 4 vim.o . shiftround = true

You can also add other basic configurations, such as displaying line numbers, etc. The specific configurations are as follows

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
 -- 编码方式utf8 vim.g . encoding = "UTF-8" vim.o . fileencoding = "utf-8" -- jkhl 移动时光标周围保留8行vim.o . scrolloff = 8 vim.o . sidescrolloff = 8 -- 显示行号vim.wo . number = true -- 使用相对行号vim.wo . relativenumber = true -- 高亮所在行vim.wo . cursorline = true -- 显示左侧图标指示列vim.wo . signcolumn = "yes" -- 右侧参考线vim.wo . colorcolumn = "160" -- 缩进字符vim.o . tabstop = 4 vim.bo . tabstop = 4 vim.o . softtabstop = 4 vim.o . shiftround = true -- >> << 时移动长度vim.o . shiftwidth = 4 vim.bo . shiftwidth = 4 -- 空格替代tabvim.o . expandtab = true vim.bo . expandtab = true -- 新行对齐当前行vim.o . autoindent = true vim.bo . autoindent = true vim.o . smartindent = true -- 搜索大小写不敏感,除非包含大写vim.o . ignorecase = true vim.o . smartcase = true -- 搜索不要高亮vim.o . hlsearch = false  
vim.o . incsearch = true -- 命令模式行高vim.o . cmdheight = 1 -- 自动加载外部修改vim.o . autoread = true vim.bo . autoread = true -- 禁止折行vim.wo . wrap = false -- 光标在行首尾时<Left><Right>可以跳到下一行vim.o . whichwrap = "<,>,[,]" -- 允许隐藏被修改过的buffer vim.o . hidden = true -- 鼠标支持vim.o . mouse = "a" -- 禁止创建备份文件vim.o . backup = false vim.o . writebackup = false vim.o . swapfile = false -- smaller updatetime vim.o . updatetime = 300  
vim.o . timeoutlen = 500  
vim.o . splitbelow = true vim.o . splitright = true -- 自动补全不自动选中vim.g . completeopt = "menu,menuone,noselect,noinsert" -- 样式vim.o . background = "dark" vim.o . termguicolors = true vim.opt . termguicolors = true -- 不可见字符的显示,这里只把空格显示为一个点vim.o . list = false vim.o . listchars = "space:·,tab:>-"  
vim.o . wildmenu = true  
vim.o . shortmess = vim.o . shortmess .. "c" -- 补全显示10行vim.o . pumheight = 10 vim.o . clipboard = "unnamedplus" 

? 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.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14
 local lazypath = vim.fn . stdpath ( "data" ) .. "/lazy/lazy.nvim" if not vim.loop . fs_stat ( lazypath ) then vim.fn . system ({ "git" , "clone" , "--filter=blob:none" , "https://github.com/folke/lazy.nvim.git" , "--branch=stable" , -- latest stable release lazypath , }) end vim.opt . rtp : prepend ( lazypath )  
require ( "lazy" ). setup ()

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.

 1 2 3
 require ( "lazy" ). setup ( { "catppuccin/nvim" , name = "catppuccin" , priority = 1000 } )

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.

 1
 vim.cmd . colorscheme ( "catppuccin" )

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.

 1 2 3 4 5
 . ├── init.lua └── lua ├── basic.lua └── 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.

 1 2
 require ( "basic" ) require ( "plugin" )

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:

 1 2 3 4 5 6 7
 . ├── init.lua ├── lazy-lock.json └── lua ├── basic.lua ├── plugin.lua └── theme.lua 

⌨ 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.

 1 2
 vim.g . mapleader = " " vim.g . maplocalleader = " "

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
 1 2 3 4 5 6 7 8 9 10 11 12 13
 vim.g . mapleader = " " vim.g . maplocalleader = " " local opt = { noremap = true , silent = true }  
-- visual模式下缩进代码vim.keymap . set ( "v" , "<" , "<gv" , opt ) vim.keymap . set ( "v" , ">" , ">gv" , opt )  
-- 左右Tab切换map ( "n" , "<Ch>" , ":BufferLineCyclePrev<CR>" , opt ) map ( "n" , "<Cl>" , ":BufferLineCycleNext<CR>" , opt )  
-- 省略其他基础配置

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

 1 2 3 4 5 6 7
 window :  
  # Make `Option` key behave as `Alt` (macOS only):  
  # - OnlyLeft  
  # - OnlyRight  
  # - Both  
  # - None (default)  
  option_as_alt : Both  

References

Why Emacs and Vim are called two artifacts

BurntSushi/ripgrep

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.