summaryrefslogtreecommitdiff
path: root/.config/nvim/init.lua
blob: e4a910c3e44a417ad4e28040a4bc19aeab16cee3 (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
------------------------------------------------------------
-- Filip's config for the Zoomer Editor
------------------------------------------------------------

------------------------------------------------------------
-- General / UI settings
------------------------------------------------------------
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.title = true
vim.opt.background = "dark"
vim.opt.laststatus = 2
vim.opt.showmode = true
vim.opt.showcmd = false
vim.opt.ruler = true
vim.opt.cursorline = true

-- Modern / visual quality
vim.opt.signcolumn = "yes"
vim.opt.pumblend = 10
vim.opt.winblend = 10

------------------------------------------------------------
-- Window / split behavior
------------------------------------------------------------
vim.opt.splitbelow = true
vim.opt.splitright = true

-- Preferred width and height for automatic window resizing
vim.opt.winwidth = 10
vim.opt.winheight = 10

-- Minimum width and height a window can shrink to
vim.opt.winminwidth = 10
vim.opt.winminheight = 10

-- Automatically balance windows when opening/closing
vim.opt.equalalways = true

------------------------------------------------------------
-- Timing & performance related
------------------------------------------------------------
vim.opt.updatetime = 50
vim.opt.timeoutlen = 400
vim.opt.ttimeoutlen = 10

------------------------------------------------------------
-- Backup, swap, undo
------------------------------------------------------------
vim.opt.undofile = true
vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.writebackup = false

------------------------------------------------------------
-- Search and highlighting
------------------------------------------------------------
vim.opt.hlsearch = false
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.incsearch = true
vim.opt.wildignore = { "*.o", "*.obj", "*.bak", "*.exe" }

------------------------------------------------------------
-- Indentation and formatting
------------------------------------------------------------
vim.opt.autoindent = true
vim.opt.cindent = true
vim.opt.smartindent = true
vim.opt.wrap = false
vim.opt.scrolloff = 5
vim.opt.completeopt = { "menuone", "noinsert" }

------------------------------------------------------------
-- Clipboard and mouse
------------------------------------------------------------
vim.opt.mouse = "a"

------------------------------------------------------------
-- Session and history (shada = viminfo replacement)
------------------------------------------------------------
vim.opt.shada = "'100,<50,s10,h"
vim.opt.shadafile = vim.fn.expand("~/.local/state/nvim/shada/main.shada")

------------------------------------------------------------
-- Miscellaneous
------------------------------------------------------------
vim.opt.spelllang = { "en", "pl" }
vim.cmd("syntax on")
vim.opt.termguicolors = true
vim.cmd.colorscheme("pablo")

-- Transparency
local function transparent_hl(group)
	vim.api.nvim_set_hl(0, group, { bg = "none", ctermbg = "none" })
end

for _, group in ipairs({ "Normal", "NormalFloat", "SignColumn", "FoldColumn" }) do
	transparent_hl(group)
end

------------------------------------------------------------
-- Autocommands
------------------------------------------------------------
local augroup = vim.api.nvim_create_augroup("UserAutocmds", { clear = true })

-- Reload any .lua file on save
vim.api.nvim_create_autocmd("BufWritePost", {
	group = augroup,
	pattern = vim.fn.stdpath("config") .. "/**/*.lua",
	callback = function()
		vim.cmd("source %")
		vim.notify("Lua config reloaded", vim.log.levels.INFO)
	end,
})

-- Remove trailing whitespace & final newlines, keep cursor
vim.api.nvim_create_autocmd("BufWritePre", {
	group = augroup,
	pattern = "*",
	callback = function()
		local pos = vim.fn.getpos(".")
		vim.cmd([[%s/\s\+$//e]])
		vim.cmd([[%s/\n\+\%$//e]])
		vim.fn.setpos(".", pos)
	end,
})

vim.api.nvim_create_autocmd("BufUnload", {
	pattern = "*.tex",
	callback = function()
		-- Get the full path of the file being closed
		local file = vim.fn.expand("%:p")

		-- Check if the file actually exists (to avoid errors on temporary buffers)
		if file ~= "" then
			-- Pass the file path as the first argument to the script
			vim.fn.jobstart({ "texclear", file })
		end
	end,
})

-- Make help windows vertical splits
vim.api.nvim_create_autocmd("FileType", {
	pattern = "help",
	callback = function()
		vim.cmd("wincmd L")
	end,
})

------------------------------------------------------------
-- Key mappings
------------------------------------------------------------
vim.g.mapleader = " "

-- Helper function for cleaner keymap definitions
-- Defaults: noremap = true, silent = true
local function map(mode, lhs, rhs, opts)
	opts = opts or {}
	opts.noremap = opts.noremap ~= false -- default true
	opts.silent = opts.silent ~= false -- default true
	opts.desc = opts.desc or nil -- optional description

	vim.keymap.set(mode, lhs, rhs, opts)
end

-- Delete char before cursor + char under cursor
map("n", "<BS>", "dBx", { desc = "Delete char before + under cursor" })

-- Start substitute on current line (pre-filled :%s//g)
map("n", "S", ":%s//g<Left><Left>", { silent = false, desc = "Substitute on current line" })
-- Note: silent = false because it enters command-line mode

-- Yank to end of line (consistent with D and C behavior)
map("n", "Y", "y$", { desc = "Yank to end of line" })

-- Swap current line with the one below
map("n", "-", "ddp", { desc = "Swap line with next" })

-- Swap current line with the one above
map("n", "_", "ddkP", { desc = "Swap line with previous" })

-- System clipboard yank & paste (line / WORD)
map("n", "<C-j>", '"+yy', { desc = "Yank line to system clipboard" })
map("n", "<C-c>", '"+yW', { desc = "Yank WORD to system clipboard" })
map("n", "<C-k>", '"+p', { desc = "Paste from system clipboard after cursor" })

-- Yank selection to system clipboard
map("v", "<C-j>", '"+y', { desc = "Yank selection to system clipboard" })

-- Toggle buffers
map("n", "<Tab><Tab>", "<C-^>", { desc = "Toggle last / alternate buffer" })

-- Split window vertically
map("n", "vv", "<C-w>v", { desc = "Split the current window vertically" })

-- Insert blank line below current line and center view
map("n", "<leader>o", "o<Esc>kzz", { desc = "Insert blank line below + center" })

-- Pressing <leader>h will prompt for a help topic and open it in vsplit
map("n", "<leader>h", ":vert help ", { desc = "Open help vertically" })

-- Open messages in vplit
map("n", "<leader>m", ":vnew | put =execute('messages')<CR>", { desc = "Open messages in vsplit" })

-- Explain diagnostics
map("n", "<leader>de", ":lua vim.diagnostic.open_float()<CR>", { desc = "Explain diagnostics" })

-- Go to the next diagnostic
map("n", "<leader>dn", ":lua vim.diagnostic.goto_next()<CR>", { desc = "Go to the next diagnostic" })

-- Go to the previous diagnostic
map("n", "<leader>dp", ":lua vim.diagnostic.goto_prev()<CR>", { desc = "Go to the previous diagnostic" })

-- Open diagnostics locations
map("n", "<leader>dl", ":lua vim.diagnostic.setqflist()<CR>", { desc = "Open diagnostics locations" })

-- Toggle diagnostics
map("n", "<leader>dt", function()
	local is_enabled = vim.diagnostic.is_enabled()
	vim.diagnostic.enable(not is_enabled)

	-- Print a small message so you know the state
	if not is_enabled then
		print("Diagnostics Enabled")
	else
		print("Diagnostics Disabled")
	end
end, { desc = "Toggle diagnostics" })

-- Repeat action on visual block
map("v", ".", ":normal .<CR>", { desc = "Repeat action on visual block" })

-- Force write with sudo
vim.cmd([[cabbrev w!! execute 'silent! write !sudo tee % >/dev/null' <bar> edit!]])

-- Resize windows
map("n", "<C-Up>", ":resize +2<CR>", { desc = "Increase window height" })
map("n", "<C-Down>", ":resize -2<CR>", { desc = "Decrease window height" })
map("n", "<C-Left>", ":vertical resize +2<CR>", { desc = "Increase window width" })
map("n", "<C-Right>", ":vertical resize -2<CR>", { desc = "Decrease window width" })

-- Create a custom user command to restart LSP
vim.api.nvim_create_user_command("LspRestart", function()
	local clients = vim.lsp.get_clients()
	for _, client in ipairs(clients) do
		vim.lsp.stop_client(client.id)
	end
	vim.cmd("edit") -- Re-opens the file to trigger LSP start
	print("LSP Restarted")
end, {})

------------------------------------------------------------
-- Tabs & indentation
------------------------------------------------------------
vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
vim.opt.expandtab = true

------------------------------------------------------------
-- Filetype-specific mappings
------------------------------------------------------------
vim.api.nvim_create_autocmd("FileType", {
	group = augroup,
	pattern = "python",
	command = "map <F5> :w<CR>:!python3 %<CR>",
})

------------------------------------------------------------
-- lazy.nvim
------------------------------------------------------------
require("config.lazy")

------------------------------------------------------------
-- luasnip
------------------------------------------------------------
require("luasnip").config.set_config({ -- Setting LuaSnip config

	-- Enable autotriggered snippets
	enable_autosnippets = true,

	-- Use Tab (or some other key if you prefer) to trigger visual selection
	store_selection_keys = "<Tab>",
})

vim.cmd([[
" Use Tab to expand and jump through snippets
imap <silent><expr> <Tab> luasnip#expand_or_jumpable() ? '<Plug>luasnip-expand-or-jump' : '<Tab>'
smap <silent><expr> <Tab> luasnip#jumpable(1) ? '<Plug>luasnip-jump-next' : '<Tab>'

" Use Shift-Tab to jump backwards through snippets
imap <silent><expr> <S-Tab> luasnip#jumpable(-1) ? '<Plug>luasnip-jump-prev' : '<S-Tab>'
smap <silent><expr> <S-Tab> luasnip#jumpable(-1) ? '<Plug>luasnip-jump-prev' : '<S-Tab>'
]])

-- Load snippets from fiadra/chadsnips
require("luasnip.loaders.from_vscode").lazy_load()