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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
vim.g.mapleader = " "
vim.opt.scrolloff = 10
vim.g.python3_host_prog = "/Users/eugene/.pyenv/versions/3.11.5/bin/python"
vim.opt.visualbell = true
vim.opt.encoding = "utf-8"
vim.opt.fileencoding = "utf-8"
vim.opt.number = true
vim.opt.linebreak = true
vim.opt.splitbelow = true
vim.opt.splitright = true
vim.opt.shiftwidth = 4
vim.opt.backspace = { "indent", "eol", "start" }
vim.cmd("filetype plugin indent on")
-- Whitespace settings per filetype
vim.api.nvim_create_autocmd("FileType", {
pattern = "python",
command = "setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4",
})
vim.api.nvim_create_autocmd({ "BufNewFile", "BufRead" }, {
pattern = "*.go",
command = "setlocal noet tabstop=4 shiftwidth=4 softtabstop=4",
})
vim.api.nvim_create_autocmd("FileType", {
pattern = { "javascript", "typescript", "java", "rust", "c", "cpp", "lua" },
command = "setlocal expandtab tabstop=2 shiftwidth=2 softtabstop=2",
})
-- Automatically install packer.nvim if not installed
local ensure_packer = function()
local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
if fn.empty(fn.glob(install_path)) > 0 then
fn.system({ "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path })
vim.cmd([[packadd packer.nvim]])
return true
end
return false
end
local packer_bootstrap = ensure_packer()
-- Autocommand to reload Neovim whenever you save the init.lua
vim.cmd([[
augroup packer_user_config
autocmd!
autocmd BufWritePost init.lua source <afile> | PackerSync
augroup end
]])
-- Plugin manager setup using packer.nvim
require("packer").startup(function(use)
-- Packer can manage itself
use("wbthomason/packer.nvim")
-- Auto complete
use("neovim/nvim-lspconfig")
use("hrsh7th/nvim-cmp")
use("hrsh7th/cmp-nvim-lsp")
use("hrsh7th/cmp-buffer")
use("hrsh7th/cmp-path")
use("hrsh7th/cmp-cmdline")
-- Telescope for advanced searching
use({
"nvim-telescope/telescope.nvim",
tag = "0.1.8",
requires = { { "nvim-lua/plenary.nvim" } },
})
-- Telescope FZF extension
use({ "nvim-telescope/telescope-fzf-native.nvim", run = "make" })
-- Tags-based plugin for searching symbols
use("ludovicchabant/vim-gutentags")
-- Git gutter
use("airblade/vim-gitgutter")
-- Status bar
use("nvim-lualine/lualine.nvim")
-- Git wrapper with :G
use("tpope/vim-fugitive")
-- GitHub vim fugitive plugin
use("tpope/vim-rhubarb")
-- Editorconfig file support
use("editorconfig/editorconfig-vim")
-- Theme
use("sainnhe/everforest")
-- Go support
use({ "fatih/vim-go", run = ":GoUpdateBinaries" })
-- Treesitter for better syntax highlighting
use({ "nvim-treesitter/nvim-treesitter", run = ":TSUpdate" })
-- File explorer
use("kyazdani42/nvim-tree.lua")
-- LSP Installer
use("williamboman/mason.nvim")
use("williamboman/mason-lspconfig.nvim")
-- Install luasnip and friendly-snippets
use({
"L3MON4D3/LuaSnip",
requires = { "rafamadriz/friendly-snippets" },
})
use({
"jose-elias-alvarez/null-ls.nvim",
requires = { "nvim-lua/plenary.nvim" },
})
-- Terminal
use({ "akinsho/toggleterm.nvim", tag = "v2.12.0" })
use({
"amrbashir/nvim-docs-view",
opt = true,
cmd = { "DocsViewToggle" },
config = function()
require("docs-view").setup({
position = "right",
width = 50,
})
end,
})
-- Automatically set up configuration after cloning packer.nvim
-- This should be kept at the end after all plugins
if packer_bootstrap then
require("packer").sync()
end
end)
-- Null ls setup
local null_ls = require("null-ls")
null_ls.setup({
sources = {
null_ls.builtins.formatting.stylua,
},
})
-- Treesitter setup
require("nvim-treesitter.configs").setup({
ensure_installed = {
"c",
"cpp",
"go",
"python",
"javascript",
"typescript",
"java",
"rust",
"lua",
},
highlight = {
enable = true,
},
incremental_selection = {
enable = true,
},
indent = {
enable = true,
},
})
-- Lualine setup
require("lualine").setup({
options = {
theme = "everforest",
section_separators = { "", "" },
component_separators = { "", "" },
},
})
-- Nvim-tree setup
require("nvim-tree").setup({
view = {
side = "left",
width = 30,
},
actions = {
open_file = {
quit_on_open = false,
},
},
})
-- Mason setup
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = { "clangd", "pyright", "gopls", "tsserver", "rust_analyzer", "jdtls", "lua_ls" },
})
-- Setup luasnip with nvim-cmp
local cmp = require("cmp")
local luasnip = require("luasnip")
require("luasnip.loaders.from_vscode").lazy_load()
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = {
["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
["<C-y>"] = cmp.config.disable,
["<C-e>"] = cmp.mapping({
i = cmp.mapping.abort(),
c = cmp.mapping.close(),
}),
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(cmp.mapping.select_next_item(), { "i", "s" }),
["<S-Tab>"] = cmp.mapping(cmp.mapping.select_prev_item(), { "i", "s" }),
},
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "treesitter" },
{ name = "buffer" },
{ name = "path" },
}),
})
-- Setup lspconfig.
local capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities())
local lspconfig = require("lspconfig")
local lsp_servers = { "clangd", "pyright", "gopls", "tsserver", "rust_analyzer", "jdtls", "lua_ls" }
lspconfig.lua_ls.setup({
settings = {
Lua = {
runtime = {
version = "LuaJIT",
-- Lua path
path = vim.split(package.path, ";"),
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { "vim" },
},
workspace = {
-- Make the server aware of Neovim runtime files
library = {
[vim.fn.expand("$VIMRUNTIME/lua")] = true,
[vim.fn.stdpath("config") .. "/lua"] = true,
},
},
telemetry = {
enable = false,
},
},
},
})
for _, lsp in ipairs(lsp_servers) do
if lsp ~= "lua_ls" then
lspconfig[lsp].setup({
capabilities = capabilities,
})
end
end
-- Telescope setup
require("telescope").setup({
defaults = {
vimgrep_arguments = {
"rg",
"--color=never",
"--no-heading",
"--with-filename",
"--line-number",
"--column",
"--smart-case",
},
prompt_prefix = "> ",
selection_caret = "> ",
path_display = { "smart" },
},
pickers = {
find_files = {
theme = "dropdown",
},
live_grep = {
theme = "dropdown",
},
buffers = {
theme = "dropdown",
},
help_tags = {
theme = "dropdown",
},
},
extensions = {
fzf = {
fuzzy = true,
override_generic_sorter = true,
override_file_sorter = true,
case_mode = "smart_case",
},
},
})
require("telescope").load_extension("fzf")
-- Theme setup
vim.opt.termguicolors = true
vim.g.everforest_background = "soft"
vim.g.everforest_better_performance = 1
vim.g.everforest_disable_italic_comment = 1
vim.cmd("colorscheme everforest")
-- Syntax highlighting
vim.cmd("syntax on")
-- Terminal setup
require("toggleterm").setup({
size = 20,
open_mapping = [[<c-\>]], -- Toggle terminal with Ctrl-\
hide_numbers = true,
shade_filetypes = {},
shade_terminals = true,
shading_factor = "1", -- The degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light
start_in_insert = true,
insert_mappings = true, -- Whether or not the open mapping applies in insert mode
persist_size = true,
direction = "horizontal", -- 'vertical' | 'horizontal' | 'window' | 'float'
close_on_exit = true, -- Close the terminal window when the process exits
shell = vim.o.shell, -- Change the default shell
float_opts = {
border = "curved", -- 'single' | 'double' | 'shadow' | 'curved'
winblend = 0,
highlights = {
border = "Normal",
background = "Normal",
},
},
})
--- Key mappings
-- Telescope 'Find Files'
vim.api.nvim_set_keymap("n", "<leader>ff", ":Telescope find_files<CR>", { noremap = true, silent = true })
-- Telescope 'Find Grep' for text search
vim.api.nvim_set_keymap("n", "<leader>fg", ":Telescope live_grep<CR>", { noremap = true, silent = true })
-- Telescope 'Find symbols'
vim.api.nvim_set_keymap("n", "<leader>fs", ":Telescope treesitter<CR>", { noremap = true, silent = true })
-- Telescope 'Find buffers'
vim.api.nvim_set_keymap("n", "<leader>fb", ":Telescope buffers<CR>", { noremap = true, silent = true })
-- LSP 'Find symbols' -- More context aware symbol information if LSP is setup for the language
vim.api.nvim_set_keymap("n", "<leader>ls", ":Telescope lsp_document_symbols<CR>", { noremap = true, silent = true })
-- LSP 'Find Workspace Symbols' -- Finds all symbols across the workspace rather than a single file
vim.api.nvim_set_keymap("n", "<leader>lw", ":Telescope lsp_workspace_symbols<CR>", { noremap = true, silent = true })
-- LSP 'Format' -- Formats the current file asyncronously
vim.api.nvim_set_keymap(
"n",
"<leader>lf",
":lua vim.lsp.buf.format({ async = true })<CR>",
{ noremap = true, silent = true }
)
-- 'Git status'
vim.api.nvim_set_keymap("n", "<leader>gs", ":G<CR>", { noremap = true, silent = true })
-- 'Git diff'
vim.api.nvim_set_keymap("n", "<leader>gd", ":Gdiff<CR>", { noremap = true, silent = true })
-- 'Git diff --staged'
vim.api.nvim_set_keymap('n', '<leader>gds', ':Gdiffsplit --staged<CR>', { noremap = true, silent = true })
-- Toggle/Update docs view
vim.api.nvim_set_keymap('n', '<leader>dt', ':DocsViewToggle<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>du', ':DocsViewUpdate<CR>', { noremap = true, silent = true })
-- Close the current window
vim.api.nvim_set_keymap("n", "<leader>q", ":q<CR>", { noremap = true, silent = true })
-- Save the current file
vim.api.nvim_set_keymap("n", "<leader>w", ":w<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("i", "<C-k>", "<cmd>lua require'luasnip'.jump(1)<CR>", { silent = true })
vim.api.nvim_set_keymap("i", "<C-j>", "<cmd>lua require'luasnip'.jump(-1)<CR>", { silent = true })
vim.api.nvim_set_keymap("s", "<C-k>", "<cmd>lua require'luasnip'.jump(1)<CR>", { silent = true })
vim.api.nvim_set_keymap("s", "<C-j>", "<cmd>lua require'luasnip'.jump(-1)<CR>", { silent = true })
-- Mappings for opening terminals
vim.api.nvim_set_keymap("n", "<leader>tt", ":ToggleTerm<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>th', ':ToggleTerm size=15 direction=horizontal<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>tv', ':ToggleTerm size=60 direction=vertical<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>tf', ':ToggleTerm direction=float<CR>', { noremap = true, silent = true })
-- Map <Esc> in terminal mode to exit to normal mode
vim.api.nvim_set_keymap('t', '<Esc>', '<C-\\><C-n>', { noremap = true, silent = true })
-- Use Ctrl-[hjkl] to navigate between splits
vim.api.nvim_set_keymap('n', '<C-k>', ':wincmd k<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-j>', ':wincmd j<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-h>', ':wincmd h<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-l>', ':wincmd l<CR>', { noremap = true, silent = true })
-- Command to run C programs
vim.api.nvim_create_user_command("RunC", function(opts)
vim.cmd("!./" .. vim.fn.expand("%<") .. " " .. opts.args)
end, { nargs = 1 })
|