在nvim配置文件目录下的mappings.lua文件里面新增下面代码。

linux下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
map('n', '<leader>lr',
function ()
require('nvchad.term').runner{
id = "boo",
pos = "sp",

cmd = function ()
local file = vim.fn.expand "%"
local ft_cmds = {
python = "python " .. file,
}

return ft_cmds[vim.bo.ft]
end,
}
end, {desc = "Run Current File"}
)

windows下:

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
map('n', '<leader>lr',
function ()
require('nvchad.term').runner{
id = 'boo',
pos = 'sp',
shell = "pwsh",

cmd = function ()
local file = vim.fn.expand "%"
local conda_env = vim.fn.getenv("CONDA_DEFAULT_ENV")
if conda_env == "" then
conda_env = "base"
end

local ft_cmds = {
python = string.format('conda activate %s; python "%s"', conda_env, file),
}

local cc = ft_cmds[vim.bo.ft]
print("Executing command: " ..cc)
return cc
end,
}
end, {desc = "Run Current File"}
)

最后保存完当前编辑的文件,使用 + l r即可快速执行当前代码。