summaryrefslogtreecommitdiffstats
path: root/dotfiles/vim.lua
blob: 4baaf187d306653a3bdd00eb15bf6dc584913f2f (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
-- Options
----------

vim.o.undofile = true
vim.o.backup = true
vim.cmd "set backupdir-=."

vim.cmd "set shortmess+=c"

vim.o.mouse = "a"

vim.o.ignorecase = true
vim.o.smartcase = true

vim.o.smartindent = true

-- tabstop and shiftwidth are set locally by individual filetypes

vim.o.inccommand = "split"

vim.o.scrolloff = 1
vim.o.sidescrolloff = 5

vim.o.colorcolumn = "80"
vim.o.cursorline = true

vim.o.title = true

vim.o.wildmode = "longest:full,full"
vim.o.completeopt = "menu,menuone,preview,noinsert,noselect"

-- Use ripgrep
vim.o.grepprg = vim.g.ripgrep_path .. " --vimgrep --smart-case"
vim.o.grepformat = "%f:%l:%c:%m," .. vim.o.grepformat

vim.o.termguicolors = true
vim.o.background = "dark"

-- Mode already shown by the status line
vim.o.showmode = false

-- Do not set "trail:-" because it messes up the highlighting
vim.o.listchars = "tab:│ ,extends:>,precedes:<,nbsp:+"
vim.wo.list = true
vim.o.fillchars = "fold:─,vert:│"

-- Leaders
----------

vim.g.maplocalleader = ","
vim.g.mapleader = ";"

-- Misc
-------

-- From neovim#14420
-- Restores the position of previously opened files
vim.cmd [[
	augroup LastCursorPos
		autocmd!
		autocmd BufReadPost * if @% !~# "\.git[\/\\]COMMIT_EDITMSG$" && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
	augroup end
]]

-- Highlight yanked text
vim.cmd "autocmd TextYankPost * silent! lua vim.highlight.on_yank()"

vim.g.tex_flavor = "latex"

vim.cmd "colorscheme gruvbox"

-- Mappings
-----------

local map = vim.api.nvim_set_keymap
local mapopts = { noremap = true, silent = true }

vim.fn["camelcasemotion#CreateMotionMappings"] "<LocalLeader>"

map("n", "=of", [[:set <C-R>=(&formatoptions =~ "a") ? 'formatoptions-=a' : 'formatoptions+=a'<CR><CR>]], mapopts)

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

-- Gitsigns

require('gitsigns').setup {
	keymaps = {
		noremap = true,
		buffer = true,
		silent = true,

		['n ]c'] = { expr = true, "&diff ? ']c' : '<cmd>lua require\"gitsigns\".next_hunk()<CR>'"},
		['n [c'] = { expr = true, "&diff ? '[c' : '<cmd>lua require\"gitsigns\".prev_hunk()<CR>'"},

		['n <leader>hs'] = '<cmd>lua require"gitsigns".stage_hunk()<CR>',
		['n <leader>hu'] = '<cmd>lua require"gitsigns".undo_stage_hunk()<CR>',
		['n <leader>hr'] = '<cmd>lua require"gitsigns".reset_hunk()<CR>',
		['n <leader>hR'] = '<cmd>lua require"gitsigns".reset_buffer()<CR>',
		['n <leader>hp'] = '<cmd>lua require"gitsigns".preview_hunk()<CR>',
		['n <leader>hb'] = '<cmd>lua require"gitsigns".blame_line()<CR>',

		-- Text objects
		['o ih'] = ':<C-U>lua require"gitsigns".select_hunk()<CR>',
		['x ih'] = ':<C-U>lua require"gitsigns".select_hunk()<CR>',
	}
}

-- Treesitter

require('nvim-treesitter.configs').setup {
	highlight = {
		enable = true,
	},
	incremental_selection = {
		enable = true,
		keymaps = {
			init_selection = "gnn",
			node_incremental = "grn",
			scope_incremental = "grc",
			node_decremental = "grm",
		},
	},
	indent = {
		enable = true,
		disable = { "rust" },
	},
	matchup = {
		enable = true,
	},
	textobjects = {
		select = {
			enable = true,
			keymaps = {
				-- You can use the capture groups defined in textobjects.scm
				["af"] = "@function.outer",
				["if"] = "@function.inner",
				["ac"] = "@class.outer",
				["ic"] = "@class.inner",
			},
		},

		move = {
			enable = true,
			goto_next_start = {
				["]m"] = "@function.outer",
				["]]"] = "@class.outer",
			},
			goto_next_end = {
				["]M"] = "@function.outer",
				["]["] = "@class.outer",
			},
			goto_previous_start = {
				["[m"] = "@function.outer",
				["[["] = "@class.outer",
			},
			goto_previous_end = {
				["[M"] = "@function.outer",
				["[]"] = "@class.outer",
			},
		},
	},
}

vim.o.foldmethod = 'expr'
vim.o.foldexpr = 'nvim_treesitter#foldexpr()'

-- nvim-compe

require("compe").setup {
	source = {
		buffer = true,
		calc = true,
		latex_symbols = true,
		nvim_lsp = true,
		nvim_lua = true,
		nvim_treesitter = true,
		omni = false,
		path = true,
		spell = true,
		vsnip = true,
		zsh = true,
	},
}

map("i", "<CR>", "compe#confirm('<CR>')", { noremap = true, silent = true, expr = true })
map("i", "<C-e>", "compe#close('<C-e>')", { noremap = true, silent = true, expr = true })

-- Telescope

map("n", "<leader>fb", "<cmd>Telescope buffers<cr>", mapopts)
map("n", "<leader>ff", "<cmd>Telescope find_files<cr>", mapopts)
map("n", "<leader>fg", "<cmd>Telescope live_grep<cr>", mapopts)
map("n", "<leader>fh", "<cmd>Telescope help_tags<cr>", mapopts)
map("n", "<leader>ft", "<cmd>Telescope treesitter<cr>", mapopts)

-- Lualine

local lsp_status = require('lsp-status')
lsp_status.config {
	indicator_errors = "err:",
	indicator_hint = "hint:",
	indicator_info = "info:",
	indicator_ok = "ok",
	indicator_warnings = "warn:",
	spinner_frames = {""},
	status_symbol = "lsp:",
}
lsp_status.register_progress()

function LspStatus()
	if #vim.lsp.buf_get_clients() < 1 then return "" end

	return lsp_status.status()
end

require("lualine").setup {
	options = {
		component_separators = "",
		icons_enabled = false,
		section_separators = "",
	},
	sections = {
		lualine_c = {'filename', LspStatus},
	},
}

-- VSnip

map("i", "<Tab>", "vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>'", { silent = true, expr = true })
map("s", "<Tab>", "vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>'", { silent = true, expr = true })

map("i", "<S-Tab>", "vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<Tab>'", { silent = true, expr = true })
map("s", "<S-Tab>", "vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<Tab>'", { silent = true, expr = true })

-- OSCyank

-- Text yanked into the "t register gets copied using OSC52 escape sequences
-- (e.g. goes through SSH)
vim.cmd "autocmd TextYankPost * if v:event.regname is 't' | OSCYankReg t | endif"