当前位置:网站首页>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'sidnvim_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 pluginsPackerClean
: Clean up unwanted pluginsPackerUpdate
:Clean the plugin first,Then update the existing plugin,Install plugins that are not currently availablePackerSync
: 包括PackerClean
和PackerUpdate
的功能
根据官方的描述,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,敬请期待!
边栏推荐
猜你喜欢
【翻译】Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality Artistic Style Transfer
安装nodejs
「开源推荐」一个通用的后台管理系统
从零开始配置 vim(11)——插件管理
分析 Flink 任务如何超过 YARN 容器内存限制
和为s的连续正数序列
数据库导出的csv文件纯数字被转为科学计数法
1.TCP/IP基础知识
SDS观察站
How long does it take to train a neural network, neural network training takes too long
随机推荐
SAP Product Enhancement Technology Review
B端产品需求分析与优先级判断
The ceiling-level microservice boss summed up this 451-page note to tell you that microservices should be learned this way
LeetCode·每日一题·1417.重新格式化字符串·模拟
【阿克曼运动控制】
Neural network visualization has 3 d version of the, the United States to fall!(open source)
全新FIDE 编译简单评测
学习二叉树
1. 类与对象——什么是对象
form-making notes on climbing pits (jeecg project replaces form designer)
独家采访 | 智能源于自发产生而非计划:进化论拥趸,前OpenAI研究经理、UBC大学副教授Jeff Clune
C# Call AutoNavi Map API to obtain latitude, longitude and positioning [Detailed 4D explanation with complete code]
fetch请求设置请求头错误导致无法跨域
LeetCode每日一题(1754. Largest Merge Of Two Strings)
【学习笔记】线性规划对偶定理
10Super详解
7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
为什么有些人不喜欢出身底层的人?
Notes and Recommendations for Using Logs
深度解析佛萨奇,Forsage魔豹联盟系统开发方案(源码部署)