Neovim Getting Started Guide (2): Common Plug-ins

Original link: http://youngxhui.top/2023/08/neovim-%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97%E4%BA%8C%E5% B8%B8%E8%A7%81%E6%8F%92%E4%BB%B6/

neovim.png

Common plugin configuration

After the introduction in the previous chapters, the current neovim can be used basically. The common plug-ins are recommended below, so let’s get started quickly.

? nvim-tree

nvim tree is a file browser that can quickly select files in the sidebar.

The current neovim plugin installation is very simple. According to the way we know before, first find the relevant warehouse on Github: https://github.com/nvim-tree/nvim-tree.lua , and then install it. How to install it will be detailed in the README of the project.

Two packages need to be installed here, the first is nvim-tree, and the second is an optional package, mainly used to display icons. Add configuration in plugin.lua .

 1 2 3 4
 require ( "lazy" ). setup ( -- 省略其他配置{ "kyazdani42/nvim-tree.lua" , event = "VimEnter" , dependencies = "nvim-tree/nvim-web-devicons" }, )

Then create a plugins-config directory in the lua directory, and create a new nvim-tree.lua file in the directory. After that we can start our configuration. Most configurations can actually refer to the official Wiki.

Here we use the pcall function to load related plugins.

Why use pcall ? When the plug-in is not installed or other problems occur, when nvim starts, it cannot load related queries, and an exception will be thrown, which can be captured through pcall , so as not to affect the use of nvim.

 1 2 3 4 5
 local status , nvim_tree = pcall ( require , "nvim-tree" ) if not status then vim.notify ( "没有找到nvim-tree" ) return end

In this way, when nvim-tree cannot be loaded, it will be reported through vim.notify .

All that’s left is to configure our nvim-tree.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 nvim_tree.setup ({ sort_by = "case_sensitive" , -- 是否显示git 状态git = { enable = true , }, -- 过滤文件filters = { dotfiles = true , -- 过滤dotfile custom = { "node_modules" }, -- 其他过滤目录}, view = { -- 文件浏览器展示位置,左侧:left, 右侧:right side = "left" , -- 行号是否显示number = false , relativenumber = false , signcolumn = "yes" -- 显示图标width = 30 , }, renderer = { group_empty = true , }, })

Finally, add the configuration in the outermost init.lua.

 1
 require ( "plugins-config.nvim-tree" )

Open it after re-exit, and use the command :NvimTreeToggle to open or close the sidebar.

It is indeed a little inconvenient to open or close by command every time, you can operate by setting shortcut keys. Remember our keybinding.lua from earlier? We want to put all the shortcut key settings into this file.

 1 2
 -- alt + m 打开或关闭文件浏览器map ( "n" , "<Am>" , ":NvimTreeToggle<CR>" , opt )

After this, you can open and close the file browser by pressing Alt + m. In addition, there are some shortcut keys, such as creating a new file, deleting a file, renaming, etc. These actually have official preset detailed configurations , and you can also view related information through :h nvim-tree .

If you need to modify the shortcut key, you can add the relevant shortcut key in keybinding.lua, and then reference it in nvim-tree.

 1 2 3 4 5
 local pluginKeybinding = {}  
pluginKeyBinding.nvim - tree = { { key = "<F5>" , action = "refresh" }, }

In nvim-tree, set relevant shortcut keys. First, introduce this variable in nvim-tree.lua, and set related values ​​in setup.

 1 2 3 4 5 6 7 8 9 10 11 12
 local keymap = require ( 'keybinding' ). nvim - tree  
nvim_tree.setup ({ -- ... view = { mappings = { custom_only = false , list = keymap , } } -- ... }) 

? bufferline

bufferline is a plug-in for buffer management, which can open multiple tabs like a modern IDE or editor, and can switch quickly. The installation part is not described in detail, and it is described in detail in the README. bufferline address: https://github.com/akinsho/bufferline.nvim

Create a new bufferline.lua file in the plugins-configs directory, configure it, still use pcall to load, and then configure it.

 1 2 3 4 5 6 7
 local status , bufferline = pcall ( require , "bufferline" ) if not status then vim.notify ( "没有找到bufferline" ) return end  
bufferline.setup ({})

Open neovim again and open multiple files at the same time, you will find a new tab appears above.

Some settings need to be made, such as the currently opened nvim-tree installed earlier, the tab will be displayed on the nvim-tree, which is obviously not in line with expectations. Use the command :h bufferline-configuration to view the configuration supported by the plugin.

Through the help documentation, we can see that offset configuration is related to the nvim tree, so that we can complete the configuration through related configurations.

 1 2 3 4 5 6 7 8 9 10 11 12
 bufferline.setup ({ options = { offsets = { { filetype = "NvimTree" , text = "File Explorer" , text_align = "left" , separator = true , }, }, }, })

Through such settings, it can be found that when the nvim tree is opened, the tab bar will automatically shift to the right instead of appearing above the nvim tree.
Other settings can be viewed through the help above. Post my configuration here.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 options = { close_command = "bdelete! %d" , -- 点击关闭按钮关闭right_mouse_command = "bdelete! %d" , -- 右键点击关闭indicator = { icon = '▎' , -- 分割线style = 'underline' , }, buffer_close_icon = '?' , modified_icon = '●' , close_icon = '' , offsets = { { filetype = "NvimTree" , text = "File Explorer" , text_align = "left" , separator = true , } }, }

bdelete related command is used here, which is provided by the moll/vim-bbye plugin, which needs to be installed to use the above configuration.
At the same time, you can set shortcut keys for switching tabs in keybinding.lua. I set it here as Ctrl + h/l to switch left and right.

 1 2
 map ( "n" , "<Ch>" , ":BufferLineCyclePrev<CR>" , opt ) map ( "n" , "<Cl>" , ":BufferLineCycleNext<CR>" , opt ) 

✏ lualine

The lualine plugin can provide a similar effect, providing some information prompts on the editor, such as Git status, text encoding, etc. The picture below is the effect on Github.

How to install can directly see the introduction on nvim-lualine/lualine.nvim , so I won’t go into details. Also create the relevant configuration file lualine.lua under plugins-configs, import it through pcall , use lualine.setup({}) to import the plugin, and finally add the configuration file in init.lua. The installation and use methods of each plug-in are basically similar. If there are different situations, they will be explained separately . These steps will not be explained in the subsequent plug-in installation.

In lualine, the display area is divided into 6 parts, which are composed of A , B , C , X , Y , and Z

 1 2 3
 +-------------------------------------------------+ | A | B | CX | Y | Z | +-------------------------------------------------+

Each of the above sections can be customized. There are three samples in the warehouse, which can be found directly in example .

Related configuration:

 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
 local status , lualine = pcall ( require , "lualine" ) if not status then vim.notify ( "没有找到lualine" ) return end  
lualine.setup ({ options = { theme = "auto" , component_separators = { left = "|" , right = "|" }, section_separators = { left = " " , right = "" }, }, extensions = { "nvim-tree" , "toggleterm" }, sections = { lualine_c = { "filename" , }, lualine_x = { "filesize" , { "fileformat" , symbols = { unix = '' , -- e712 dos = '' , -- e70f mac = "" , -- e711 }, }, "encoding" , "filetype" , }, }, })

After configuration, you can see this effect.

? telescope

After the above configuration, the editing interface of Neovim has been gradually “modernized”. Telescope can make our search process smoother, and it can be mainly used for fuzzy queries.

Telescope installation is also relatively simple, you can refer to the README on github for installation. However, it is often impossible to perform fuzzy queries after installation. Here we need another configuration. In fact, it is also clearly written in the README. After the installation is complete, you need to run the :checkhealth telescope command.
Through this command, you can see the status of the current plug-in and whether it is available. If it is the first installation, it will prompt ERROR and WARNING, as shown in the figure:

From the prompt, you can see that rg and fd are missing, and the relevant installation address is given in the following instructions. These two software are the key to fuzzy search, and can be installed through the following two addresses. In the README of Github, the installation methods of the two software on different systems are clearly written.

If you are using a mac and brew has already been installed, you only need the following two lines of commands to complete the installation.

 1 2
 brew install rg brew install fd

After the installation is complete, re- :checkhealth telescope , if everything is OK, it proves that the installation is correct, as shown in the following figure:

After the installation is complete, you can use the telescope through the command to perform quick fuzzy queries. :Telescope find_file is to find files, Telescope live_grep is global query. For convenience, it can be bound to the corresponding shortcut key in the keybinding configuration. Below are the corresponding bindings for my shortcut keys.

 1 2 3 4 5
 -- Telescope -- 查找文件map ( "n" , "<Cp>" , ":Telescope find_files<CR>" , opt ) -- 全局搜索map ( "n" , "<Cf>" , ":Telescope live_grep<CR>" , opt )

More interesting configurations can be found in the Github repository.

appendix

Nerd font installation

In many of the above configurations, many icons cannot be displayed, which will cause problems in the text or interface display, and are often displayed as an , which requires a nerd font for support.

The Nerd font can be seen as a patch of the original font, which adds a large number of icons to the original font.

For icons that cannot be displayed above, the relevant Nerd fonts need to be installed. Relevant fonts can be downloaded from the official website , and some well-known fonts will have nerd versions, such as: FiraMono, JetBrainsMono, UbuntuMono, etc. You can download and install according to your preferences.

This article is reproduced from: http://youngxhui.top/2023/08/neovim-%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97%E4%BA%8C%E5% B8%B8%E8%A7%81%E6%8F%92%E4%BB%B6/
This site is only for collection, and the copyright belongs to the original author.