nvim插件管理工具packer的使用
閱讀時間:全文 1437 字,預估用時 8 分鐘
創作日期:2022-05-13
上篇文章:使用ssh_config管理ssh密钥
BEGIN
packer 🔗是一个功能强大且支持扩展的neovim插件管理工具,只要是基于neovim实现的ide全部是用packer来管理插件的,因此packer是学习nvim的必学插件。
packer自动安装
packer是管理插件的插件,一般在init.vim或者init.lua中写入自动安装脚本,让nvim启动后就执行packer的安装,如:
local fn = vim.fn
local install_path = fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
if fn.empty(fn.glob(install_path)) > 0 then
PACKER_BOOTSTRAP = fn.system {
"git",
"clone",
"--depth",
"1",
"https://github.com/wbthomason/packer.nvim",
install_path,
}
vim.cmd [[packadd packer.nvim]]
end
其中fn.stdpath "data"
返回nvim放置程序数据的目录,mac上是~/.local/share/nvim
,拼接/site/pack/packer/start/packer.nvim
后就是packer的安装目录,fn.empty(fn.glob(install_path))
用于判断packer是否已经安装,如果没有安装则拉取wbthomason/packer.nvim
后执行vim的packadd命令添加它,只有通过packadd命令添加过vim才会加载。
指定插件配置文件保存后自动安装
vim写法
augroup packer_user_config
autocmd!
autocmd BufWritePost plugins.lua source <afile> | PackerCompile
augroup end
lua写法
vim.cmd([[
augroup packer_user_config
autocmd!
autocmd BufWritePost plugins.lua source <afile> | PackerCompile
augroup end
]])
其中plugins.lua即为插件配置文件,实际触发文件写入成功(BufWritePost)后执行的是PackerCompile命令,autocmd为自动执行,后面加!
表示删除该组下的自动命令。
引入packer
安装成功后我们执行载入并在之后使用packer:
local status_ok, packer = pcall(require, "packer")
if not status_ok then
return
end
使用init方法修改默认配置
下面为官网的配置及注释说明,如display为弹框样式,log为日志等级,还有autoremove、max_jobs等配置项:
packer.init {
ensure_dependencies = true, -- Should packer install plugin dependencies?
snapshot = nil, -- Name of the snapshot you would like to load at startup
snapshot_path = join_paths(stdpath 'cache', 'packer.nvim'), -- Default save directory for snapshots
package_root = util.join_paths(vim.fn.stdpath('data'), 'site', 'pack'),
compile_path = util.join_paths(vim.fn.stdpath('config'), 'plugin', 'packer_compiled.lua'),
plugin_package = 'packer', -- The default package for plugins
max_jobs = nil, -- Limit the number of simultaneous jobs. nil means no limit
auto_clean = true, -- During sync(), remove unused plugins
compile_on_sync = true, -- During sync(), run packer.compile()
disable_commands = false, -- Disable creating commands
opt_default = false, -- Default to using opt (as opposed to start) plugins
transitive_opt = true, -- Make dependencies of opt plugins also opt by default
transitive_disable = true, -- Automatically disable dependencies of disabled plugins
auto_reload_compiled = true, -- Automatically reload the compiled file after creating it.
git = {
cmd = 'git', -- The base command for git operations
subcommands = { -- Format strings for git subcommands
update = 'pull --ff-only --progress --rebase=false',
install = 'clone --depth %i --no-single-branch --progress',
fetch = 'fetch --depth 999999 --progress',
checkout = 'checkout %s --',
update_branch = 'merge --ff-only @{u}',
current_branch = 'branch --show-current',
diff = 'log --color=never --pretty=format:FMT --no-show-signature HEAD@{1}...HEAD',
diff_fmt = '%%h %%s (%%cr)',
get_rev = 'rev-parse --short HEAD',
get_msg = 'log --color=never --pretty=format:FMT --no-show-signature HEAD -n 1',
submodules = 'submodule update --init --recursive --progress'
},
depth = 1, -- Git clone depth
clone_timeout = 60, -- Timeout, in seconds, for git clones
default_url_format = 'https://github.com/%s' -- Lua format string used for "aaa/bbb" style plugins
},
display = {
non_interactive = false, -- If true, disable display windows for all operations
open_fn = nil, -- An optional function to open a window for packer's display
open_cmd = '65vnew \\[packer\\]', -- An optional command to open a window for packer's display
working_sym = '⟳', -- The symbol for a plugin being installed/updated
error_sym = '✗', -- The symbol for a plugin with an error in installation/updating
done_sym = '✓', -- The symbol for a plugin which has completed installation/updating
removed_sym = '-', -- The symbol for an unused plugin which was removed
moved_sym = '→', -- The symbol for a plugin which was moved (e.g. from opt to start)
header_sym = '━', -- The symbol for the header line in packer's display
show_all_info = true, -- Should packer show all update details automatically?
prompt_border = 'double', -- Border style of prompt popups.
keybindings = { -- Keybindings for the display window
quit = 'q',
toggle_info = '<CR>',
diff = 'd',
prompt_revert = 'r',
}
},
luarocks = {
python_cmd = 'python' -- Set the python command to use for running hererocks
},
log = { level = 'warn' }, -- The default print log level. One of: "trace", "debug", "info", "warn", "error", "fatal".
profile = {
enable = false,
threshold = 1, -- integer in milliseconds, plugins which load faster than this won't be shown in profile output
},
autoremove = false, -- Remove disabled or unused plugins without prompting the user
}
使用startup安装插件
return packer.startup(function(use)
use "wbthomason/packer.nvim" -- 管理packer自己,如更新
use { "nvim-lua/plenary.nvim", as = 'plenary' } -- 常用工具插件
-- 如果第一次安装还需要执行下sync命令:require("packer").sync()
end)
startup方法有三种使用方式,上面的是最常用的一种,可传入配置项控制插件行为,如配置run参数在安装插件成功后执行命令,参数包括:
use {
'myusername/example', -- The plugin location string
-- The following keys are all optional
disable = boolean, -- Mark a plugin as inactive
as = string, -- Specifies an alias under which to install the plugin
installer = function, -- Specifies custom installer. See "custom installers" below.
updater = function, -- Specifies custom updater. See "custom installers" below.
after = string or list, -- Specifies plugins to load before this plugin. See "sequencing" below
rtp = string, -- Specifies a subdirectory of the plugin to add to runtimepath.
opt = boolean, -- Manually marks a plugin as optional.
branch = string, -- Specifies a git branch to use
tag = string, -- Specifies a git tag to use. Supports '*' for "latest tag"
commit = string, -- Specifies a git commit to use
lock = boolean, -- Skip updating this plugin in updates/syncs. Still cleans.
run = string, function, or table, -- Post-update/install hook. See "update/install hooks".
requires = string or list, -- Specifies plugin dependencies. See "dependencies".
rocks = string or list, -- Specifies Luarocks dependencies for the plugin
config = string or function, -- Specifies code to run after this plugin is loaded.
-- The setup key implies opt = true
setup = string or function, -- Specifies code to run before this plugin is loaded.
-- The following keys all imply lazy-loading and imply opt = true
cmd = string or list, -- Specifies commands which load this plugin. Can be an autocmd pattern.
ft = string or list, -- Specifies filetypes which load this plugin.
keys = string or list, -- Specifies maps which load this plugin. See "Keybindings".
event = string or list, -- Specifies autocommand events which load this plugin.
fn = string or list -- Specifies functions which load this plugin.
cond = string, function, or list of strings/functions, -- Specifies a conditional test to load this plugin
module = string or list -- Specifies Lua module names for require. When requiring a string which starts
-- with one of these module names, the plugin will be loaded.
module_pattern = string/list -- Specifies Lua pattern of Lua module names for require. When
requiring a string which matches one of these patterns, the plugin will be loaded.
}
常用vim命令
-- 执行编译load文件
:PackerCompile
-- 删除未使用或未开启的插件
:PackerClean
-- 先清理再安装插件
:PackerInstall
-- 先清理再更新插件
:PackerUpdate
-- 先执行更新再执行编译
:PackerSync
-- 立即加载插件
:PackerLoad completion-nvim ale
FINISH
上篇文章:使用ssh_config管理ssh密钥