当前位置:网站首页>Starting from zero configuration vim (11) -- plug-in management

Starting from zero configuration vim (11) -- plug-in management

2022-08-11 11:04:00 aluluka

Before we introduced the basic configuration part and the shortcut key configuration part.If you configure these two parts,vimIt's already better.But as a code editor, it is still relatively simple.,Use the configuration to complete daily coding tasks can appear out of puff.vimThe more powerful point is that it has a rich plug-in system.A large number of master in the above development of various forms of plug-in for it.Today we will start with plugin management,Leverage the achievements of our predecessors to enrich ourvim功能

使用luaAdd autocommand group

Before officially introducing the plugin, let's optimize the timing of loading the configuration file.When we introduced the automatic command before, we have already given how to automatically load the configuration

augroup NVIMRC
autocmd!
autocmd BufWritePost init.lua source %
augroup END

在使用vimscriptOn occasions it works fine.但是如何在 neovimconfigure it in?
目前有两种解决办法

使用新版 neovim api

neovim0.7 版本以后,We can use the following api to create and use autocommand groups

  • nvim_create_augroup({name}, {*opts}) : Create an autocommand group,如果创建成功,Returns the autocommand group'sid
  • nvim_create_autocmd({event}, {*opts}):Create an autocommand.

nvim_create_augroup Pass the name of an autocommand group,Additionally it can accept a table 作为属性值,Currently attribute values ​​can be passed in a clear 的布尔值,Equivalent to whether to execute autocmd! .

nvim_create_autocmd,The first parameter is one or more event strings table,它的含义与 autocmd 中的事件相同,The string used is also the same.The second parameter is a representation of the property table.常用的有:

  • group: Belongs to an autocommand group
  • pattern: autocmd中的 pattern部分
  • callback: 一个lua的回调函数,当事件发生时,调用该回调函数
  • command: This field can be filled with a vim命令的字符串,相当于 autocmd中的 command部分

We will change the above code to lua 版本,First create an autocommand group using

local nvimrc = vim.api.nvim_create_augroup("NVIMRC", {
    clear = true})

Then for our convenience command field to complete this autocommand

vim.api.nvim_create_autocmd({
    "BufWritePost"}, {
    
    pattern = "init.lua",
    group = nvimrc,
    command = "source %"
})

在上一篇文章中,We already have a preliminary concept of dividing modules.With more and more configurations,以后在 init.lua must be accompanied by a large number of require.我们可以通过 gf Quickly jump to the corresponding module,But the premise is that we have set path 变量.每次退出 nvim 再进来,需要重新设置,Do you find it troublesome??If we use autocommand group,在进入 init.lua 之后自动设置 path 就好了.After we learned to use autocommand groups,to fulfill this requirement

vim.api.nvim_create_autocmd({
    "BufWritePost"}, {
    
    pattern = "init.lua",
    group = nvimrc,
    callback = function()
      vim.o.path = vim.o.path .. ",**/*"
    end
})

path You can add multiple paths as file search paths in,between multiple paths , 分割.We said earlier that you can use **/* 表示当前目录下所有文件.Exit and re-enter nvim No need to manually enter it again afterwards path 了.

在lua中执行vim命令

在 0.7Previous versions can't through the aboveapito create autocommands.But it provides executionvim命令的接口.

我们可以使用 vim.cmd 来执行 vim 命令.它接收一个字符串参数,This string represents the vim 命令.可以使用引号括起来,But special characters need to be escaped.也可以使用 [[]] 来括起来,There is no need for escaping.Using the above function we can easily achieve the above functions

vim.cmd[[ augroup NVIMRC autocmd! autocmd BufWritePost init.lua source % autocmd BufReadPost init.lua set path+=**/* augroup END ]]

两种方式各有千秋,但是既然使用 lua做配置,So what I think is to be able to use lua的地方尽量用 lua.

Still in use for care 0.6 版本的小伙伴,Let's make a version judgment first.最后的代码如下所示

if vim.fn.has "nvim-0.7" then
  local nvimrc = vim.api.nvim_create_augroup("NVIMRC", {clear = true})
  vim.api.nvim_create_autocmd({"BufWritePost"}, {
    pattern = "init.lua",
    group = nvimrc,
    command = "source %"
  })
  
  vim.api.nvim_create_autocmd({"BufWritePost"}, {
    pattern = "init.lua",
    group = nvimrc,
    callback = function()
      vim.o.path = vim.o.path .. ",**/*"
    end
  })
else
  vim.cmd[[
    augroup NVIMRC
      autocmd!
      autocmd BufWritePost init.lua source %
      autocmd BufReadPost init.lua set path+=**/*
      augroup END
  ]]
end

I put this code in lua/autocmd.lua 中.所以我需要在 init.lua 中加载这个文件

require("autocmd")

插件管理

在新版 neovim (版本大于 0.5 ) 中,一般推荐使用 packer This plugin management tool.

根据官方的描述,We use the following method to install

git clone --depth 1 https://github.com/wbthomason/packer.nvim\
 ~/.local/share/nvim/site/pack/packer/start/packer.nvim

根据官方的描述,我们可以在 lua/plugins.lua Add plugin management related code in.

return require('packer').startup(function(use)
      -- Packer can manage itself
      use 'wbthomason/packer.nvim'
      -- Add another plugin
end)

接着我们需要在 init.lua 中加载这个文件

require("plugins")

Later we can use the following commands to manage the plugin

  • PackerInstall:Clean unnecessary plugins first and then install plugins
  • PackerClean: Clean up unwanted plugins
  • PackerUpdate:Clean the plugin first,Then update the existing plugin,Install plugins that are not currently available
  • PackerSync: 包括 PackerCleanPackerUpdate 的功能

根据官方的描述,No matter what plugins you want to add or delete,都可以用 PackerSync This command is done.

After we can use q来退出,In case of error you can use rto redownload
在这里插入图片描述

本篇就到这里了.The next few articles mainly download and configure some plug-ins through the plug-in management tool,敬请期待!

原网站

版权声明
本文为[aluluka]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208111047099268.html