Nvim-技巧积累

redir

获取当期目录下的所有文件名

1
2
local current_dir = vim.fn.expand('%:p:h') -- 获取当前文件所在目录的绝对路径
local files = vim.split(vim.fn.glob(current_dir .. '/*'), "\n") -- 获取当前目录下所有文件

不加载任何配置启动 nvim

1
nvim -u NONE

之后可以用:

1
:luafile file.lua

来加载特定的文件.

用户自定义 Ex 命令

需要注意, 用户自定义的命令需要以大写字母开头.

需要借助 :command 命令, 其后跟的一个字符相当于输入了一个 attribute:

  • !, :command! 相当于 command -bang
  • ", -resigter
  • |, -bar
  • b, 意思是 local to current buffer, 应该是 -buffer

自定义命令的语法为:

1
:com[mand][!] [{attr}...] {cmd} {repl}

{cmd} 为名称.

{repl} 为实际执行的内容. (“replacement”, 即这个命令替换为什么)

{attr} 有四类:

  • Argument handling, -nargs
  • completion behavior, -completion
  • range handling, -range, -count, -addr
  • special cases

自定义 command-completion

command-completion 是 Nvim 自带的一个功能, 在如 vim.ui.input() 获取输入时, 按下 <TAB> 进行补全时, 会显示的列表, 默认有:

-complete=arglist	file names in argument list
-complete=augroup	autocmd groups
-complete=buffer	buffer names
-complete=behave	:behave suboptions
-complete=color		color schemes
-complete=command	Ex command (and arguments)
-complete=compiler	compilers
-complete=dir		directory names

等, 具体可看 :h command.

自定义的语法为 -complete=custom,{func}-complete=customlist,{func}

{func} 会自动传入三个变量 ArgLead, CmdLine, CursorPos

使用 nvim 的 lua api 的示例:

1
2
3
4
5
vim.api.nvim_create_user_command("MyCompleteCommand",
"",
{nargs = 1, complete = function()
return {'option1', 'option2', 'option3'}
end})

Nvim-技巧积累
http://example.com/2024/01/16/Nvim-技巧积累/
作者
Jie
发布于
2024年1月16日
许可协议