Files
dotfiles/nvim/.config/nvim/init.lua
2025-08-01 20:17:05 -04:00

175 lines
4.8 KiB
Lua

vim.cmd.colorscheme("vim")
vim.g.mapleader = " "
vim.keymap.set("n", "<C-d>", "<C-d>zz")
vim.keymap.set("n", "<C-h>", "<C-w><C-h>")
vim.keymap.set("n", "<C-j>", "<C-w><C-j>")
vim.keymap.set("n", "<C-k>", "<C-w><C-k>")
vim.keymap.set("n", "<C-l>", "<C-w><C-l>")
vim.keymap.set("n", "<C-u>", "<C-u>zz")
vim.keymap.set("n", "<Esc>", ":nohlsearch<CR>")
vim.keymap.set("n", "<leader>f", vim.lsp.buf.format)
vim.keymap.set("n", "<leader>a", ":lua vim.lsp.buf.code_action()<CR>")
vim.keymap.set("n", "<leader>pv", ":Ex<CR>")
vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv")
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv")
vim.keymap.set({ 'n', 'v', 'x' }, '<leader>d', '"+d<CR>')
vim.keymap.set({ 'n', 'v', 'x' }, '<leader>y', '"+y<CR>')
vim.o.number = true
vim.o.relativenumber = true
vim.o.shiftwidth = 2
vim.o.tabstop = 2
vim.o.expandtab = true
vim.o.scrolloff = 8
vim.o.wrap = false
vim.o.winborder = "rounded"
vim.o.clipboard = "unnamedplus"
vim.o.swapfile = false
vim.opt.path:append("**")
vim.keymap.set("i", "me::", function()
return vim.fn.strftime("Author: vx_clutch <https://vx-clutch.github.io/vxserver.dev/>\nDate: %B %d, %Y\nLicense: BSD-3-Clause")
end, { expr = true })
local augroup = vim.api.nvim_create_augroup("vxclutch", {})
vim.api.nvim_create_autocmd("BufReadPost", {
group = augroup,
callback = function()
local mark = vim.api.nvim_buf_get_mark(0, '"')
local lcount = vim.api.nvim_buf_line_count(0)
if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark)
end
end,
})
vim.api.nvim_create_autocmd("TermOpen", {
group = augroup,
callback = function()
vim.opt_local.number = false
vim.opt_local.relativenumber = false
vim.opt_local.signcolumn = "no"
end,
})
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
callback = function()
local dir = vim.fn.expand('<afile>:p:h')
if vim.fn.isdirectory(dir) == 0 then
vim.fn.mkdir(dir, 'p')
end
end,
})
local undodir = vim.fn.expand("~/.vim/undodir")
if vim.fn.isdirectory(undodir) == 0 then
vim.fn.mkdir(undodir, "p")
end
local terminal_state = {
buf = nil,
win = nil,
is_open = false
}
local function FloatingTerminal()
if terminal_state.is_open and vim.api.nvim_win_is_valid(terminal_state.win) then
vim.api.nvim_win_close(terminal_state.win, false)
terminal_state.is_open = false
return
end
if not terminal_state.buf or not vim.api.nvim_buf_is_valid(terminal_state.buf) then
terminal_state.buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_option(terminal_state.buf, 'bufhidden', 'hide')
end
local width = math.floor(vim.o.columns * 0.8)
local height = math.floor(vim.o.lines * 0.8)
local row = math.floor((vim.o.lines - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
terminal_state.win = vim.api.nvim_open_win(terminal_state.buf, true, {
relative = 'editor',
width = width,
height = height,
row = row,
col = col,
style = 'minimal',
border = 'rounded',
})
vim.api.nvim_win_set_option(terminal_state.win, 'winblend', 0)
vim.api.nvim_win_set_option(terminal_state.win, 'winhighlight',
'Normal:FloatingTermNormal,FloatBorder:FloatingTermBorder')
vim.api.nvim_set_hl(0, "FloatingTermNormal", { bg = "none" })
vim.api.nvim_set_hl(0, "FloatingTermBorder", { bg = "none", })
local has_terminal = false
local lines = vim.api.nvim_buf_get_lines(terminal_state.buf, 0, -1, false)
for _, line in ipairs(lines) do
if line ~= "" then
has_terminal = true
break
end
end
if not has_terminal then
vim.fn.termopen(os.getenv("SHELL"))
end
terminal_state.is_open = true
vim.cmd("startinsert")
vim.api.nvim_create_autocmd("BufLeave", {
buffer = terminal_state.buf,
callback = function()
if terminal_state.is_open and vim.api.nvim_win_is_valid(terminal_state.win) then
vim.api.nvim_win_close(terminal_state.win, false)
terminal_state.is_open = false
end
end,
once = true
})
end
local function CloseFloatingTerminal()
if terminal_state.is_open and vim.api.nvim_win_is_valid(terminal_state.win) then
vim.api.nvim_win_close(terminal_state.win, false)
terminal_state.is_open = false
end
end
vim.keymap.set("n", "<leader>t", FloatingTerminal, { noremap = true, silent = true })
vim.keymap.set("t", "<Esc>", function()
if terminal_state.is_open then
vim.api.nvim_win_close(terminal_state.win, false)
terminal_state.is_open = false
end
end, { noremap = true, silent = true })
vim.pack.add({
{ src = "https://github.com/neovim/nvim-lspconfig" },
})
local lspconfig = require("lspconfig")
local servers = { "lua_ls", "clangd" }
for _, server in ipairs(servers) do
lspconfig[server].setup({})
end
vim.diagnostic.config({
virtual_text = true,
underline = true,
})