summaryrefslogtreecommitdiffstats
path: root/.config/nvim/init.lua
blob: 0a116d5dde41cc8dc045213e28d13d680797378b (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
if vim.env.DOTFILES_DIR == nil then
	vim.notify("DOTFILES_DIR is not set!", vim.log.levels.WARN)
end

vim.g.dotfiles = { augroup = vim.api.nvim_create_augroup("dotfiles", {}) }

------------------------------------------------------------------------------------------------------------------------
-- Local functions
------------------------------------------------------------------------------------------------------------------------

-- Show the diff between the current buffer and the file it was loaded from
local function show_buf_diff_orig()
	vim.cmd.new({ mods = { vertical = true } })
	vim.b.buftype = "nofile"
	vim.cmd.read("++edit #")
	vim.api.nvim_buf_set_lines(0, 0, 2, true, {})
	vim.cmd.diffthis()
	vim.cmd.wincmd("p")
	vim.cmd.diffthis()
end

local function reload_buffer()
	local view = vim.fn.winsaveview()
	if vim.o.filetype == "netrw" then
		vim.cmd.edit(".")
	else
		vim.cmd.edit()
	end
	vim.fn.winrestview(view)
end

local function toggle_wrap()
	vim.o.wrap = not vim.o.wrap
end

------------------------------------------------------------------------------------------------------------------------
-- Miscellaneous
------------------------------------------------------------------------------------------------------------------------

vim.g.mapleader = " " -- Set before any `vim.keymap.set()` call

-- Highlight on yank
vim.api.nvim_create_autocmd("TextYankPost", {
	desc = "Highlight on yank",
	group = vim.g.dotfiles.augroup,
	callback = function()
		vim.hl.on_yank()
	end,
})

-- Start terminal mode automatically
vim.api.nvim_create_autocmd({ "WinEnter", "TermOpen" }, {
	desc = "Start terminal mode",
	group = vim.g.dotfiles.augroup,
	pattern = "term://*",
	callback = function()
		vim.cmd.startinsert()
	end,
})

------------------------------------------------------------------------------------------------------------------------
-- Plugins
------------------------------------------------------------------------------------------------------------------------

vim.pack.add({
	"https://github.com/qadzek/link.vim", -- Tidy URLs
	"https://github.com/tpope/vim-surround", -- Manipulate delimiters
	"https://github.com/airblade/vim-gitgutter", -- Show git status in the gutter
})

------------------------------------------------------------------------------------------------------------------------
-- Commands (there's more in the different plugins)
------------------------------------------------------------------------------------------------------------------------

vim.api.nvim_create_user_command("DiffOrig", show_buf_diff_orig, { desc = "Show diff between current buffer and file" })

------------------------------------------------------------------------------------------------------------------------
-- Keymaps (there's more in the different plugins)
------------------------------------------------------------------------------------------------------------------------
-- See `:help key-notation` and `:help <Cmd>`

-- Miscellaneous
vim.keymap.set("n", "<Leader>s", "<Cmd>update<CR>", { desc = "Save buffer" })
vim.keymap.set("n", "<Leader>be", reload_buffer, { desc = "Reload buffer" })
vim.keymap.set("n", "<Leader>bd", "<Cmd>bdelete<CR>", { desc = "Delete current buffer" })
vim.keymap.set("n", "<Leader>bb", "<Cmd>close<CR>", { desc = "Delete current window" })
vim.keymap.set("n", "<Leader>bw", toggle_wrap, { desc = "Toggle text wrap" })
vim.keymap.set("n", "<M-z>", "zA", { desc = "Toggle fold (recursively) under cursor" })
vim.keymap.set("n", "ZR", vim.cmd.SessionRestart, { desc = "Save and load default session and restart" })
vim.keymap.set("n", "ZZ", vim.cmd.SessionExitSave, { desc = "Save to default session and exit" })
vim.keymap.set("n", "ZQ", vim.cmd.SessionExitNoSave, { desc = "Save to default session and exit" })
vim.keymap.set("t", "<C-\\><C-\\>", "<C-\\><C-n>", { desc = "Exit terminal mode" })
vim.keymap.set("n", "<Leader>mm", "<Cmd>messages<CR>", { desc = "Show all messages" })
vim.keymap.set("n", "<Leader>mc", "<Cmd>messages clear<CR>", { desc = "Clear all messages" })
vim.keymap.set("n", "<Leader>lh", vim.cmd.LspHintToggle, { desc = "Toggle inlay hints (LSP)" })
-- These two mappings work well together to start command mode and insert from history in two keystrokes.
vim.keymap.set("n", "<C-p>", ":", { desc = "Enter command mode" })
vim.keymap.set("c", "<C-p>", "<Up>", { desc = "Insert previous history entry" })
-- Navigate files
vim.keymap.set("n", "<M-e>", vim.cmd.GotoExplorer, { desc = "Open/close/focus netrw window" })
-- Navigate window
vim.keymap.set({ "n", "i" }, "<M-h>", "<C-w>h", { desc = "Move left a window" })
vim.keymap.set({ "n", "i" }, "<M-j>", "<C-w>j", { desc = "Move down a window" })
vim.keymap.set({ "n", "i" }, "<M-k>", "<C-w>k", { desc = "Move up a window" })
vim.keymap.set({ "n", "i" }, "<M-l>", "<C-w>l", { desc = "Move right a window" })
vim.keymap.set("t", "<M-h>", "<C-\\><C-n><C-w>h", { desc = "Move left a window" })
vim.keymap.set("t", "<M-j>", "<C-\\><C-n><C-w>j", { desc = "Move down a windowd" })
vim.keymap.set("t", "<M-k>", "<C-\\><C-n><C-w>k", { desc = "Move up a window" })
vim.keymap.set("t", "<M-l>", "<C-\\><C-n><C-w>l", { desc = "Move right a window" })
-- Navigate tab
vim.keymap.set({ "n", "i", "t" }, "<M-n>", "<Cmd>tabnext #<CR>", { desc = "Move to last accessed tab page" })
vim.keymap.set({ "n", "i", "t" }, "<M-n>", "<Cmd>tabnext<CR>", { desc = "Move to next tab page" })
vim.keymap.set({ "n", "i", "t" }, "<M-b>", "<Cmd>tabprevious<CR>", { desc = "Move to previous tab page" })
vim.keymap.set("i", "<M-h>", "<Space><BS><Left>")
vim.keymap.set("i", "<M-l>", "<Space><BS><Right>")
-- Navigate in command/insert mode
vim.keymap.set({ "c", "i" }, "<M-h>", "<Space><BS><Left>")
vim.keymap.set({ "c", "i" }, "<M-l>", "<Space><BS><Right>")
vim.keymap.set("c", "<M-H>", "<C-Left>")
vim.keymap.set("c", "<M-L>", "<C-Right>")
-- Non incremental history search, works when the completion menu is active
vim.keymap.set("c", "<C-k>", function()
	return vim.fn.pumvisible() == 1 and "<C-e><Up>" or "<Up>"
end, { expr = true })
vim.keymap.set("c", "<C-j>", function()
	return vim.fn.pumvisible() == 1 and "<C-e><Down>" or "<Down>"
end, { expr = true })

------------------------------------------------------------------------------------------------------------------------
-- Options
------------------------------------------------------------------------------------------------------------------------

vim.opt.shell = "/usr/bin/bash"
vim.opt.foldenable = true -- Folds are closed by default
vim.opt.pumheight = 0 -- Popup menu height
vim.opt.confirm = true -- Confirm before losing buffer unsaved changes
vim.opt.splitbelow = false
vim.opt.splitright = false
vim.opt.clipboard = "unnamedplus" -- Sync clipboard
vim.opt.cursorline = true -- Highlight the line where the cursor is on.
vim.opt.scrolloff = 2 -- Keep this many screen lines above/below the cursor
vim.opt.ignorecase = true -- Search ignores case by default
vim.opt.smartcase = true -- Search is case-sensitive if searching for uppercase characters
vim.opt.conceallevel = 2 -- Hide text with the "conceal" syntax attribute
vim.opt.list = true -- Display <Tab> and other non-printables
vim.opt.listchars = { -- Characters used by 'list'
	tab = "> ", -- Tab
	trail = "-", -- Trailing space
	nbsp = "+", -- Non-breakable space
	extends = "→", -- Last column when 'wrap' is off
	precedes = "←", -- First column when there is text before it
}
-- See `'cpoptions'`
-- Use a `\` like `CTRL_V` in mappings, abbreviations, user commands and the "to" part of menu commands
-- Useful for example for matching mappings that start with space: `:map \ `
vim.opt.cpoptions:remove("B")
-- Backup
vim.opt.swapfile = true -- Store a swap file, see `:help swap-file`
vim.opt.undofile = true -- Save buffer undo history
vim.opt.backup = true -- Backup file before overwriting
vim.opt.backupdir = { vim.fn.stdpath("state") .. "/nvim/backup//" }
vim.opt.writebackup = true -- Keep backup after writing file
-- Gutter
vim.opt.number = true -- Show line number
vim.opt.relativenumber = true -- Show relative line number
vim.opt.numberwidth = 4 -- Minimal number of columns for line number (includes the space before the text)
vim.opt.signcolumn = "auto" -- Always show sign column
vim.opt.foldcolumn = "0" -- Fixed number of fold columns
-- Wrapping
vim.opt.wrap = false -- Don't wrap text by default
vim.opt.showbreak = "+++ " -- String to show wrapped lines
vim.opt.linebreak = true -- Wrap at characters in 'breakat'
vim.opt.breakindent = true -- Indent wrapped lines
-- Tab and backspace (See `help 30.5`)
vim.opt.tabstop = 4 -- <Tab> and <Bs> visually expand at columns multiple of 'tabstop'
vim.opt.shiftwidth = 0 -- Number of space in first indendation. If 0 use 'tabstop' value.
vim.opt.softtabstop = -1 -- <Tab> and <Bs> stop at columns multiple of 'softtabstop'. If <0 use 'shiftwidth' value.
vim.opt.expandtab = false -- Don't turn tabs into spaces