-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
253 lines (223 loc) · 8.89 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
-- Colorscheme
vim.cmd("colorscheme peanut")
-- Options
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"
vim.g.loaded_node_provider = 0
vim.g.loaded_perl_provider = 0
vim.g.loaded_python3_provider = 0
local opt = vim.opt
opt.clipboard = "unnamedplus"
opt.cmdheight = 0
opt.cursorline = true
opt.fillchars:append({ eob = " ", foldopen = "", foldclose = "", vert = " " })
opt.laststatus = 3
opt.mouse = ""
opt.number = true
opt.pumblend = 10
opt.relativenumber = true
opt.scrolloff = 2
opt.showmode = false
opt.showtabline = 0
opt.sidescrolloff = 2
opt.smoothscroll = true
opt.splitright = true
opt.splitbelow = true
opt.undofile = true
opt.undolevels = 10000
opt.virtualedit = "block"
opt.wrap = false
-- Default file format
opt.expandtab = true
opt.shiftwidth = 4
opt.tabstop = 4
opt.textwidth = 80
-- Folding
---@diagnostic disable-next-line
_G.foldexpr_handler = function()
local buf = vim.api.nvim_get_current_buf()
if vim.b[buf].ts_folds == nil then
-- as long as we don't have a filetype, don't bother
-- checking if treesitter is available (it won't)
if vim.bo[buf].filetype == "" then
return "0"
end
if vim.bo[buf].filetype:find("dashboard") then
vim.b[buf].ts_folds = false
else
vim.b[buf].ts_folds = pcall(vim.treesitter.get_parser, buf)
end
end
return vim.b[buf].ts_folds and vim.treesitter.foldexpr() or "0"
end
opt.foldexpr = "v:lua.foldexpr_handler()"
opt.foldmethod = "expr"
opt.foldtext = ""
-- Powershell options
opt.shell = "pwsh"
opt.shellcmdflag =
"-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new();$PSDefaultParameterValues['Out-File:Encoding']='utf8';$PSStyle.OutputRendering=[System.Management.Automation.OutputRendering]::PlainText;Remove-Alias -Force -ErrorAction SilentlyContinue tee;"
opt.shellredir = "2>&1 | %%{ '$_' } | Out-File %s; exit $LastExitCode"
opt.shellpipe = "2>&1 | %%{ '$_' } | tee %s; exit $LastExitCode"
opt.shellquote = ""
opt.shellxquote = ""
--Keymaps
local map = vim.keymap.set
-- better up/down
map({ "n", "x" }, "j", "v:count == 0 ? 'gj' : 'j'", { desc = "Down", expr = true, silent = true })
map({ "n", "x" }, "k", "v:count == 0 ? 'gk' : 'k'", { desc = "Up", expr = true, silent = true })
-- Move to window using the <ctrl> hjkl keys
map("n", "<C-h>", "<C-w>h", { desc = "Go to left window", remap = true })
map("n", "<C-j>", "<C-w>j", { desc = "Go to lower window", remap = true })
map("n", "<C-k>", "<C-w>k", { desc = "Go to upper window", remap = true })
map("n", "<C-l>", "<C-w>l", { desc = "Go to right window", remap = true })
-- Resize window using <ctrl> arrow keys
map("n", "<C-Up>", "<cmd>resize +1<cr>", { desc = "Increase window height" })
map("n", "<C-Down>", "<cmd>resize -1<cr>", { desc = "Decrease window height" })
map("n", "<C-Left>", "<cmd>vertical resize -1<cr>", { desc = "Decrease window width" })
map("n", "<C-Right>", "<cmd>vertical resize +1<cr>", { desc = "Increase window width" })
-- Split windows
map("n", "<leader>w=", "<C-W>=", { desc = "Equally high and wide", remap = true })
map("n", "<leader>wh", "<C-W>s", { desc = "Split window below", remap = true })
map("n", "<leader>wv", "<C-W>v", { desc = "Split window right", remap = true })
map("n", "<leader>wd", "<C-W>c", { desc = "Delete window", remap = true })
-- Move lines
map("n", "<A-j>", "<cmd>execute 'move .+' . v:count1<cr>==", { desc = "Move down" })
map("n", "<A-k>", "<cmd>execute 'move .-' . (v:count1 + 1)<cr>==", { desc = "Move up" })
map("i", "<A-j>", "<esc><cmd>m .+1<cr>==gi", { desc = "Move down" })
map("i", "<A-k>", "<esc><cmd>m .-2<cr>==gi", { desc = "Move up" })
map("v", "<A-j>", ":<C-u>execute \"'<,'>move '>+\" . v:count1<cr>gv=gv", { desc = "Move down" })
map("v", "<A-k>", ":<C-u>execute \"'<,'>move '<-\" . (v:count1 + 1)<cr>gv=gv", { desc = "Move up" })
-- Buffers
map("n", "<S-h>", "<cmd>bprevious<cr>", { desc = "Prev buffer" })
map("n", "<S-l>", "<cmd>bnext<cr>", { desc = "Next buffer" })
map("n", "[b", "<cmd>bprevious<cr>", { desc = "Prev buffer" })
map("n", "]b", "<cmd>bnext<cr>", { desc = "Next buffer" })
-- Clear search and stop snippet on escape
map({ "i", "n", "s" }, "<esc>", function()
vim.cmd("nohlsearch")
if vim.snippet then
vim.snippet.stop()
end
return "<esc>"
end, { expr = true, desc = "Escape and clear hlsearch" })
-- https://github.com/mhinz/vim-galore#saner-behavior-of-n-and-n
map("n", "n", "'Nn'[v:searchforward].'zv'", { expr = true, desc = "Next search result" })
map("x", "n", "'Nn'[v:searchforward]", { expr = true, desc = "Next search result" })
map("o", "n", "'Nn'[v:searchforward]", { expr = true, desc = "Next search result" })
map("n", "N", "'nN'[v:searchforward].'zv'", { expr = true, desc = "Prev search result" })
map("x", "N", "'nN'[v:searchforward]", { expr = true, desc = "Prev search result" })
map("o", "N", "'nN'[v:searchforward]", { expr = true, desc = "Prev search result" })
-- Add undo break-points
map("i", ",", ",<c-g>u")
map("i", ".", ".<c-g>u")
map("i", ";", ";<c-g>u")
-- Save file
map({ "i", "x", "n", "s" }, "<C-s>", "<cmd>w<cr><esc>", { desc = "Save file" })
-- Better indenting
map("v", "<", "<gv")
map("v", ">", ">gv")
-- Commenting
map("n", "gco", "o<esc>Vcx<esc><cmd>normal gcc<cr>fxa<bs>", { desc = "Add comment below" })
map("n", "gcO", "O<esc>Vcx<esc><cmd>normal gcc<cr>fxa<bs>", { desc = "Add comment above" })
-- lazy
map("n", "<leader>pl", "<cmd>Lazy<cr>", { desc = "Lazy" })
-- New file
map("n", "<leader>fn", "<cmd>enew<cr>", { desc = "New file" })
-- Lists
map("n", "<leader>xl", "<cmd>lopen<cr>", { desc = "Location list" })
map("n", "<leader>xq", "<cmd>copen<cr>", { desc = "Quickfix list" })
-- Quit
map("n", "<leader>qq", "<cmd>qa<cr>", { desc = "Quit all" })
-- Diagnostic
local diagnostic_goto = function(next, severity)
local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev
severity = severity and vim.diagnostic.severity[severity] or nil
return function()
go({ severity = severity })
end
end
map("n", "<leader>cd", vim.diagnostic.open_float, { desc = "Line diagnostics" })
map("n", "]d", diagnostic_goto(true), { desc = "Next diagnostic" })
map("n", "[d", diagnostic_goto(false), { desc = "Prev diagnostic" })
map("n", "]e", diagnostic_goto(true, "ERROR"), { desc = "Next error" })
map("n", "[e", diagnostic_goto(false, "ERROR"), { desc = "Prev error" })
map("n", "]w", diagnostic_goto(true, "WARN"), { desc = "Next warning" })
map("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev warning" })
-- Autocmds
local autocmd = vim.api.nvim_create_autocmd
local function augroup(name)
return vim.api.nvim_create_augroup("MyConfig" .. name, { clear = true })
end
-- Go to last location when opening a buffer
autocmd("BufReadPost", {
group = augroup("LastLocation"),
callback = function(event)
local exclude = { "gitcommit" }
local buf = event.buf
if vim.tbl_contains(exclude, vim.bo[buf].filetype) then
return
end
local mark = vim.api.nvim_buf_get_mark(buf, '"')
local lcount = vim.api.nvim_buf_line_count(buf)
if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark)
end
end,
})
-- Close some filetypes with <q>
autocmd("FileType", {
group = augroup("CloseWithQ"),
pattern = {
"checkhealth",
"dap-float",
"gitsigns-blame",
"help",
"qf",
},
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.schedule(function()
map("n", "q", function()
vim.cmd("close")
pcall(vim.api.nvim_buf_delete, event.buf, { force = true })
end, {
buffer = event.buf,
silent = true,
desc = "Quit buffer",
})
end)
end,
})
-- Enable spellcheck and line wrapping in select file types
autocmd("FileType", {
group = augroup("SpellWrap"),
pattern = { "text", "gitcommit", "markdown" },
callback = function()
vim.opt_local.wrap = true
vim.opt_local.spell = true
end,
})
-- Install Lazy plugin manager
local lazy_path = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
local lazy_is_installed = vim.uv.fs_stat(lazy_path)
if not lazy_is_installed then
local lazy_repo = "https://github.com/folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazy_repo, lazy_path })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.runtimepath:prepend(lazy_path)
-- Load Lazy plugin manager
require("lazy").setup({
spec = { { import = "plugins" } },
rocks = { enabled = false },
checker = { enabled = true },
})