-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
173 lines (149 loc) · 4.55 KB
/
init.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
--vim.opt: behaves like :set
--vim.opt_global: behaves like :setglobal
--vim.opt_local: behaves like :setlocal
--vim.o: behaves like :set
--vim.go: behaves like :setglobal, like let g: in vim script
--vim.bo: for buffer-scoped options
--vim.wo: for window-scoped options (can be double indexed)
vim.g.mapleader = " "
vim.cmd([[set mouse-=a]])
-- 搜索忽略大小写
vim.opt.ignorecase = true
-- 禁止折行
vim.wo.wrap = false
-- utf8
vim.g.encoding = "UTF-8"
vim.o.fileencoding = "utf-8"
vim.o.hidden = true
vim.o.relativenumber = true
vim.o.number = true
vim.o.tabstop = 4
vim.o.shiftwidth = 4
vim.o.smartcase = true
vim.o.scrolloff = 10
vim.o.cursorline = true
vim.o.incsearch = true
vim.o.spelllang = "en"
vim.o.clipboard = "unnamedplus"
-- Set updatetime for CursorHold
-- 300ms of no cursor movement to trigger CursorHold
vim.o.updatetime = 300
vim.wo.colorcolumn = "120"
vim.o.fileformats = "unix"
-- 自动补全不自动选中
vim.opt.completeopt = "menu,menuone,preview,noinsert,popup"
-- 样式 使neovim支持 termguicolors
vim.o.termguicolors = true
vim.opt.termguicolors = true
vim.opt.list = true
vim.opt.listchars = "eol:\\u21b5,space:.,tab:>-,trail:-"
vim.cmd.colorscheme("sorbet")
vim.cmd.syntax("on")
vim.g.netrw_winsize = 20
-- 使用增强状态栏插件后不再需要 vim 的模式提示
--vim.o.showmode = false
--------------------------------------------------------------
--- key map
--------------------------------------------------------------
vim.keymap.set("n", "<leader>w", "<cmd>w<cr>", { silent = true })
vim.keymap.set("n", "<c-h>", "<c-w>h", { silent = true })
vim.keymap.set("n", "<c-j>", "<c-w>j", { silent = true })
vim.keymap.set("n", "<c-k>", "<c-w>k", { silent = true })
vim.keymap.set("n", "<c-l>", "<c-w>l", { silent = true })
vim.keymap.set("n", "<leader>q", "<cmd>q<cr>", { silent = true })
vim.keymap.set("n", "<c-n>", "<cmd>Lex<cr>", { silent = true })
--------------------------------------------------------------
--- auto command
---------------------------------------------------------------
local myAutoGroup = vim.api.nvim_create_augroup("myAutoGroup", {
clear = true,
})
local autocmd = vim.api.nvim_create_autocmd
-- 进入Terminal 自动进入插入模式
autocmd("TermOpen", {
group = myAutoGroup,
command = "startinsert",
})
-- 自动保存
autocmd({ "InsertLeave", "TextChanged" }, {
group = myAutoGroup,
pattern = { "*" },
command = "silent! wall",
})
autocmd("BufWritePre", {
group = myAutoGroup,
pattern = { "*" },
callback = function()
local fn = vim.fn
local dir = fn.expand("<afile>:p:h")
-- This handles URLs using netrw. See ':help netrw-transparent' for details.
if dir:find("%l+://") == 1 then
return
end
if fn.isdirectory(dir) == 0 then
fn.mkdir(dir, "p")
end
end,
})
-- reopen file at the same position
autocmd("BufReadPost", {
callback = function()
local row, col = unpack(vim.api.nvim_buf_get_mark(0, '"'))
if { row, col } ~= { 0, 0 } then
local max_rows = vim.api.nvim_buf_line_count(0)
if max_rows < row then
row = max_rows
end
vim.api.nvim_win_set_cursor(0, { row, 0 })
end
end,
group = myAutoGroup,
})
-- 定义一个函数来显示并选择缓冲区
function SelectAndSwitchBuffer()
-- 获取所有缓冲区的列表
local bufs = vim.api.nvim_list_bufs()
local buf_names = {}
-- 构建缓冲区名称列表
for _, buf in ipairs(bufs) do
local bufname = vim.api.nvim_buf_get_name(buf)
table.insert(buf_names, bufname)
end
-- 打印缓冲区列表
print("Available buffers:")
for i, bufname in ipairs(buf_names) do
print(i .. ": " .. bufname)
end
-- 获取用户输入的缓冲区编号
vim.ui.input({ prompt = "Enter buffer number to switch: " }, function(input)
local choice = tonumber(input)
if choice and bufs[choice] then
-- 切换到选择的缓冲区
vim.api.nvim_set_current_buf(bufs[choice])
else
print("Invalid buffer number.")
end
end)
end
-- 创建一个命令来调用这个函数
vim.api.nvim_create_user_command("SwitchBuffer", SelectAndSwitchBuffer, {})
vim.keymap.set("n", "<leader>e", "<cmd>SwitchBuffer<cr>", {})
-- 使用 autocmd 监听插入模式下的键入事件
vim.api.nvim_create_autocmd({ "InsertCharPre" }, {
pattern = "*",
callback = function()
local triggers = { " ", "\t", "," }
-- 获取当前模式
local mode = vim.api.nvim_get_mode().mode
-- 只在插入模式下触发
if mode == "i" then
local char = vim.v.char
if not vim.list_contains(triggers, char) then
local key = vim.keycode("<C-x><C-n>")
vim.api.nvim_feedkeys(key, "m", false)
end
else
print("not in insert mode")
end
end,
})