0%

Neovim常用配置(2)

使用 Lua 配置 Neovim,并设置自己的 workflow

结合命令行工具

我在编码时常常有使用 git 的需求,但又不想总是在命令行中敲命令

于是我利用与 ToggleTerm 把命令行工具 lazygit 嵌入至 Neovim 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local Terminal = require('toggleterm.terminal').Terminal

local lazygit = Terminal:new({ cmd = "lazygit", direction = 'float', hidden = true })
local top = Terminal:new({ cmd = "top", direction = 'float', hidden = true })


-- lazygit
vim.api.nvim_create_user_command("LazyGit",
function()
lazygit:toggle()
end,
{ nargs = 0 })

-- top
vim.api.nvim_create_user_command("Top",
function()
top:toggle()
end,
{ nargs = 0 })

同样类似的,还可以通过命令行工具 trans 进行翻译,并通过 neovim 的 api 将翻译结果显示出来.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local function translate_terminal()
local mode = vim.api.nvim_get_mode()['mode']
local to_translate
if mode == 'n' then
to_translate = vim.fn.expand('<cword>')
elseif mode == 'v' then
to_translate = require('basic').get_visual_selection()
end
local command = string.format('trans "%s"', to_translate)

async.run(function()
local translated_content = vim.fn.systemlist(command)
utils.show_term_content(translated_content)
end)
end

设置 Layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vim.api.nvim_create_user_command(
"BufferDelete",
function()
---@diagnostic disable-next-line: missing-parameter
local file_exists = vim.fn.filereadable(vim.fn.expand("%p"))
local modified = vim.api.nvim_buf_get_option(0, "modified")

if file_exists == 0 and modified then
local user_choice = vim.fn.input(
"The file is not saved, whether to force delete? Press enter or input [y/n]:")
if user_choice == "y" or string.len(user_choice) == 0 then
vim.cmd("bd!")
end
return
end

local force = not vim.bo.buflisted or vim.bo.buftype == "nofile"

vim.cmd(force and "bd!" or string.format("bp | bd! %s", vim.api.nvim_get_current_buf()))
end,
{ desc = "Delete the current Buffer while maintaining the window layout" })

在 Neovim 中编辑 Hexo blog

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
local blog_path = "~/Documents/Hexo-Blog"

local function blogNew(input)
vim.api.nvim_set_current_dir(blog_path)
require('nvim-tree.api').tree.change_root(blog_path)
local output = vim.fn.system("hexo n " .. '\"' .. input.args .. '\"')

if (vim.v.shell_error == 0) then
local path = string.sub(output, string.find(output, '~', 1, true), -1)
vim.cmd(":e " .. path)
else
vim.notify("Failed creating new blog post" .. input.args, "error")
end
end

local function blogNewDraft(input)
vim.api.nvim_set_current_dir(blog_path)
require('nvim-tree.api').tree.change_root(blog_path)
local output = vim.fn.system("hexo new draft " .. '\"' .. input.args .. '\"')

if (vim.v.shell_error == 0) then
local path = string.sub(output, string.find(output, '~', 1, true), -1)
vim.cmd(":e " .. path)
else
vim.notify("Failed creating new blog post" .. input.args, "error")
end
end

local function blogGenerateAndDeploy()
vim.api.nvim_set_current_dir(blog_path)
if (os.execute("hexo g && hexo s")) then
vim.notify("Deploy the blog successfully", "info")
else
vim.notify("Deployment of blog failed", "error")
end
end

取消下一行注释

1
2
3
4
5
6
7
-- avoid comment when enter the new line
vim.api.nvim_create_autocmd({ "BufEnter" }, {
pattern = "*",
callback = function()
vim.opt.formatoptions = vim.opt.formatoptions - { "c", "r", "o" }
end,
})