用packer.nvim来管理Neovim的插件

第一步是安装

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

然后在Neovim配置目录,创建lua子目录,并且增加一个plugins.lua

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
local fn = vim.fn
local install_path = fn.stdpath('data') .. '/site/pack/packer/start/packer.nvim'
local packer_compiled_path = fn.stdpath('config') .. '/plugin/packer_compiled.lua'

local is_directory_exists = function(dir)
return fn.empty(fn.glob(dir)) == 0
end

local file_exists = function(name)
local file = io.open(name, "r")
if file ~= nil then
io.close(file)
return true
else
return false
end
end

local packer_startup = function(use)
use "wbthomason/packer.nvim"
use 'neovim/nvim-lspconfig'

if not file_exists(packer_compiled_path) then
require('packer').sync()
end
end

if not is_directory_exists(install_path) then
print('Installing packer plugin...')
fn.system({ 'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path })
print('All done, please restart Neovim.')
do return end
end

local require_packer_succeed, packer = pcall(require, "packer")
if require_packer_succeed then
return packer.startup(packer_startup)
end

最后在init.lua中增加引用:

1
require('plugins')

References