· 4 years ago · Aug 06, 2021, 06:32 AM
1-- disable writing shada file while sourcing config:
2vim.opt.shadafile = "NONE"
3-------------
4-- Aliases --
5-------------
6
7local cmd = vim.cmd
8local fn = vim.fn
9local g = vim.g
10local map = vim.api.nvim_set_keymap
11local o = vim.opt
12local api = vim.api
13
14
15-- disable builtin plugins
16local disabled_built_ins = {
17 "netrw",
18 "netrwPlugin",
19 "netrwSettings",
20 "netrwFileHandlers",
21 "gzip",
22 "zip",
23 "zipPlugin",
24 "tar",
25 "tarPlugin",
26 "getscript",
27 "getscriptPlugin",
28 "vimball",
29 "vimballPlugin",
30 "2html_plugin",
31 "logipat",
32 "rrhelper",
33 "spellfile_plugin",
34 "shada_splugin",
35 "tutor_mode_plugin",
36 "remote_plugins",
37}
38
39for _, plugin in pairs(disabled_built_ins) do
40 g["loaded_" .. plugin] = 1
41end
42
43-- leader
44g.mapleader = ','
45
46-- turn line numbers and relative number on
47o.number = true
48o.relativenumber = true
49
50o.shortmess = o.shortmess + 'I' -- no welcome screen
51o.iskeyword = o.iskeyword + '-' -- count dashes as part of a word (useful for css rules, etc.)
52o.hidden = true -- don't allow buffers to exist in the background
53o.cursorline = true -- show the line the cursor is on
54o.showtabline = 2 -- always show tab (even w/ only 1 file open)
55o.signcolumn = 'yes' -- always show the 'sign' part of the gutter (so errors etc. don't change the width)
56o.linebreak = true -- prevent words from breaking in the middle of words
57o.showmode = false -- don't show --INSERT--, --NORMAL--, --VISUAL-- etc.
58o.ignorecase = true -- ignore case when searching
59o.smartcase = true -- if uppercase, then pay attention to cases while searching
60o.lazyredraw = true -- don't redraw while executing macros
61o.swapfile = false -- no swap files
62o.scrolloff = 2 -- keep 2 lines above and below cursor when at top or bottom of screen
63o.splitright = true -- open new split panes to the right
64o.splitbelow = true -- Open new split below
65o.clipboard = 'unnamed' -- use system clipboard
66o.termguicolors = true
67o.fillchars = 'eob: ' -- hide end of text ~'s by using space character instead
68o.completeopt = 'menuone,noselect'
69o.cindent = true -- better indenting
70o.expandtab = true -- expand tabs into spaces
71o.foldenable = false -- disable folding (I never use this and for some reason seems to speed up startup time)
72o.updatetime = 300
73
74
75-- -- configure status line
76-- vim.wo.statusline = " %f%= %p%%"
77
78-- helps comand completion
79o.wildmode = {'longest:full', 'full'}
80-- o.wildmode = o.wildmode + '.DS_Store'
81-- o.wildmode = o.wildmode + '*.jpg,*.jpeg,*.gif,*.png,*.gif,*.psd,*.o,*.obj,*.min.js'
82-- o.wildmode = o.wildmode + '*/bower_components/*,*/node_modules/*'
83-- o.wildmode = o.wildmode + '*/smarty/*,*/vendor/*,*/.git/*,*/.hg/*,*/.svn/*,*/.sass-cache/*,*/log/*,*/tmp/*,*/build/*,*/ckeditor/*,*/doc/*,*/source_maps/*,*/dist/*'
84
85map('n', "'", '`', {noremap = true}) -- better mark jumping
86map('n', 'Q', '@@', {noremap = true}) -- repeat the previous macro
87
88-- Always move linewise in normal mode
89map('n', 'k', 'gk', {noremap = true})
90map('n', 'j', 'gj', {noremap = true})
91
92map('n', 'Y', 'y$', {noremap = true}) -- yank till end of line
93
94-- I've never wanted to use 0 instead of ^
95map('n', '0', '^', {noremap = true})
96map('v', '0', '^', {noremap = true})
97
98map('n', '<Bslash>', '<C-O>', {noremap = true}) -- go to prev jump
99map('n', '<Bar>', '<C-I', {noremap = true}) -- go to next jump
100
101map('n', 'c', '"_c', {noremap = true}) -- prevent changes from going into system register
102
103-- save a keystroke when indenting and de-denting
104map('n', '>', '>>', {noremap = true})
105map('n', '<', '<<', {noremap = true})
106
107-- easily insert a newline in normal mode
108map('n', '<Up>', ":<c-u>put!=repeat([''],v:count)<bar>']+1<cr>", {noremap = true, silent = true})
109map('n', '<Down>', ":<c-u>put =repeat([''],v:count)<bar>'[-1<cr>", {noremap = true, silent = true})
110
111map('n', 'J', 'mzJ`z', {noremap = true}) -- make joining lines keep cursor position
112
113-- scrolling keybinds
114map('n', '<CR>', '<C-D>', {noremap = true})
115map('n', '<BS>', '<C-U>', {noremap = true})
116
117map('n', '<leader><CR>', ':nohlsearch<cr>', {noremap = true}) -- make it easy to turn off search highlight
118
119-- move between buffers easily
120map('n', '<leader><Left>', ':BufferLineMovePrev<CR>', {noremap = true, silent = true})
121map('n', '<leader><Right>', ':BufferLineMoveNext<CR>', {noremap = true, silent = true})
122
123-- go to previous or next buffer with arrow keys
124map('n', '<Left>', ':BufferLineCyclePrev<CR>', {noremap = true, silent = true})
125map('n', '<Right>', ':BufferLineCycleNext<CR>', {noremap = true, silent = true})
126
127-- easier 3 line deletes
128map('n', 'd<Down>', 'd2j', {noremap = true})
129map('n', 'd<Up>', 'd2k', {noremap = true})
130
131-- make search results be in middle of the screen
132map('n', 'n', 'nzz', {noremap = true})
133map('n', 'N', 'Nzz', {noremap = true})
134
135-- easier split navigation (ctrl + direction to go to split)
136map('n', '<C-J>', '<C-W><C-J>', {noremap = true})
137map('n', '<C-K>', '<C-W><C-K>', {noremap = true})
138map('n', '<C-L>', '<C-W><C-L>', {noremap = true})
139map('n', '<C-H>', '<C-W><C-H>', {noremap = true})
140-- For simple re-sizing of splits.
141map('n', '+', '<C-W>5>', {noremap = true})
142map('n', '-', '<C-W>5<', {noremap = true})
143map('n', '=', '<C-W>=', {noremap = true})
144-- easily make a split
145map('n', '<leader>sp', ':vs<CR>', {noremap = true})
146
147-- ctrl s to save all
148map('n', '<C-S>', ':wa<CR>', {noremap = true, silent = true})
149map('v', '<C-S>', '<C-C>:wa<CR>', {noremap = true, silent = true})
150map('i', '<C-S>', '<ESC>:wa<CR>', {noremap = true, silent = true})
151
152map('n', '<leader>c', 'zz7<C-E>', {noremap = true}) -- make it easy to center the screen (offset by 7 lines)
153map('n', '<leader>t', 'zt', {noremap = true}) -- make it easy to move to top of screen
154
155-- paste from delete
156map('n', '<leader>p', '""p', {noremap = true})
157map('n', '<leader>P', '""P', {noremap = true})
158-- paste from most recent yank by default
159map('n', 'p', '"0p', {noremap = true})
160map('n', 'P', '"0P', {noremap = true})
161-- select last pasted text
162map('n', 'gp', '`[v`]', {noremap = true})
163
164-- easy way to quit everything:
165map('n', '<leader>q', ':qa<CR>', {noremap = true})
166
167-- swap 'go uppercase' with 'go lowercase' since I use 'go uppercase' way more
168map('n', 'gU', 'gu', {noremap = true})
169map('n', 'gu', 'gU', {noremap = true})
170
171-- fugitive mappings
172map('n', '<leader>gb', ':Git blame<CR>', {noremap = true})
173map('n', '<leader>gp', ':Git push<CR>', {noremap = true})
174
175-- fzf
176map('n', '<C-p>', ':GitFiles!<CR>', {noremap = true})
177map('n', '<leader>b', ':Buffers!<CR>', {noremap = true})
178map('n', '<leader>h', ':History!<CR>', {noremap = true})
179map('n', 'K', ':Ag! <C-R><C-W><CR>', {noremap = true, silent = true})
180
181-- file exploer
182map('n', '<C-o>', ':NvimTreeToggle<CR>', {noremap = true})
183map('n', '<leader>f', ':NvimTreeFindFile<CR>', {noremap = true})
184
185-- for easy session management
186g.session_dir = '~/vim-sessions'
187cmd "exec 'nnoremap <leader>ss :mks! ' . g:session_dir . '/*.vim<C-D><BS><BS><BS><BS><BS>'"
188cmd "exec 'nnoremap <leader>sr :so ' . g:session_dir. '/*.vim<C-D><BS><BS><BS><BS><BS>'"
189-- TODO make command to delete all sessions
190g.startify_session_dir = g.session_dir
191
192-- lsp mappings
193map('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', {noremap = true, silent = true})
194map('n', '<leader>e', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', {noremap = true, silent = true})
195map('n', '<Tab>', '<Cmd>lua vim.lsp.buf.hover()<CR>', {noremap = true, silent = true})
196
197-- abbreviations
198cmd [[iabbrev c const]]
199cmd [[iabbrev e export]]
200cmd [[iabbrev doo TODO]]
201
202cmd 'au TextYankPost * lua vim.highlight.on_yank{higroup="Cursor", timeout=125, on_visual=false}' -- disabled in visual mode
203
204
205-- plugins
206require "paq" {
207
208 "savq/paq-nvim"; -- let Paq manage itself
209
210 -- color related stuff
211 "nxvu699134/vn-night.nvim"; -- colorscheme
212 "marko-cerovac/material.nvim"; -- colorscheme
213 "folke/tokyonight.nvim"; -- colorscheme
214 "shaunsingh/moonlight.nvim"; -- colorscheme
215 "navarasu/onedark.nvim"; -- colorscheme
216
217 "luochen1990/rainbow"; -- paren color matching
218 "norcalli/nvim-colorizer.lua"; -- colorize colors
219 "sheerun/vim-polyglot"; -- syntax (basically just needed for proper indenting and commenting)
220
221 -- tpope
222 "tpope/vim-surround"; -- for surrounding things
223 "tpope/vim-repeat"; -- make vim surround and other plugins work with .
224 "tpope/vim-fugitive"; -- git plugin
225 "tpope/vim-abolish"; -- case coercion
226
227 -- lsp
228 "neovim/nvim-lspconfig"; -- lsp config
229 "kabouzeid/nvim-lspinstall"; -- allows :LspInstall <language> to easily install lang servers
230 "hrsh7th/nvim-compe"; -- auto completion
231
232 -- File finding
233 {"junegunn/fzf", run = fn['fzf#install']};
234 "junegunn/fzf.vim";
235
236 -- misc.
237 "b3nj5m1n/kommentary"; -- for easy commenting
238
239 "mhartington/formatter.nvim"; -- for code formatting
240
241 "inkarkat/vim-ReplaceWithRegister"; -- make it easy to replace w/ register in one command. Use 'gr'
242
243 "mhinz/vim-startify"; -- nice start screen
244
245 "airblade/vim-rooter"; -- To set the root dir of a project (based on git)
246
247 "kyazdani42/nvim-web-devicons"; -- for icons
248
249 "kyazdani42/nvim-tree.lua"; -- file explorer
250
251 "matze/vim-move"; -- move text
252
253 "nvim-lua/plenary.nvim"; -- needed for gitsigns
254 "lewis6991/gitsigns.nvim"; -- add git signs
255
256 "windwp/nvim-ts-autotag"; -- automatically close tags for html, jsx, etc.
257 -- "alvan/vim-closetag"; -- automatically close tags for html, jsx, etc.
258 "andrewradev/tagalong.vim"; -- make editing an openting tag change the closing tag too
259
260 {"nvim-treesitter/nvim-treesitter", run = ":TSUpdate"}; -- treesitter (do I need this?)
261 "nvim-treesitter/playground"; -- playground for treesitter; helpful for writing queries
262
263 "steelsojka/pears.nvim"; -- auto close pairs
264
265 "akinsho/nvim-bufferline.lua"; -- buffer styling
266
267 "L3MON4D3/LuaSnip"; -- snippets
268
269 "hoob3rt/lualine.nvim"; -- status line
270
271 "akinsho/nvim-toggleterm.lua"; -- terminal
272}
273
274g.closetag_filenames = '*.html,*.xhtml,*.phtml,*.js,*.jsx,*.tsx'
275-- This will make the list of non-closing tags case-sensitive (e.g. `<Link>` will be closed while `<link>` won't.)
276g.closetag_emptyTags_caseSensitive = 1
277
278
279
280-- lsp setup
281require'lspinstall'.setup()
282local servers = require'lspinstall'.installed_servers()
283for _, server in pairs(servers) do
284 require'lspconfig'[server].setup{}
285end
286
287
288require'nvim-web-devicons'.setup {
289 -- globally enable default icons (default to false)
290 -- will get overriden by `get_icons` option
291 default = true;
292}
293
294require('colorizer').setup()
295
296g.rainbow_active = 1 -- turn on rainbow parens by default
297
298-- set colorscheme
299-- cmd 'colorscheme vn-night'
300
301-- require('material').set()
302-- g.material_style = "deep ocean" -- darker | lighter | palenight | oceanic | deep ocean
303
304g.tokyonight_style = "night" -- storm
305cmd 'colorscheme tokyonight'
306
307-- require('moonlight').set()
308
309-- g.onedark_style = "cool" -- default | darker | cool | deep | warm | warmer
310-- require('onedark').setup() -- classic atom theme
311
312-- file explorer settings
313-- note: use g? to goggle the help UI
314g.nvim_tree_width = 35
315g.nvim_tree_side = 'right'
316g.nvim_tree_ignore = { '.git', 'node_modules', '.cache' }
317g.nvim_tree_auto_close = 1
318g.nvim_tree_quit_on_open = 1
319g.nvim_tree_hide_dotfiles = 1
320
321local tree_cb = require'nvim-tree.config'.nvim_tree_callback
322
323g.nvim_tree_bindings = {
324 { key = "t", cb = tree_cb("tabnew") }
325}
326
327
328-- compe setup
329require'compe'.setup {
330 enabled = true;
331 autocomplete = true;
332 debug = false;
333 min_length = 1;
334 preselect = 'disable';
335 throttle_time = 100;
336 source_timeout = 200;
337 resolve_timeout = 800;
338 incomplete_delay = 400;
339 max_abbr_width = 100;
340 max_kind_width = 100;
341 max_menu_width = 100;
342 documentation = {
343 border = { '', '' ,'', ' ', '', '', '', ' ' }, -- the border option is the same as `|help nvim_open_win|`
344 winhighlight = "NormalFloat:CompeDocumentation,FloatBorder:CompeDocumentationBorder",
345 max_width = 120,
346 min_width = 60,
347 max_height = math.floor(vim.o.lines * 0.3),
348 min_height = 1,
349 };
350
351 source = {
352 path = true;
353 buffer = true;
354 calc = false;
355 nvim_lsp = true;
356 nvim_lua = true;
357 luasnip = true;
358 };
359}
360
361require'lspconfig'.sumneko_lua.setup {
362 settings = {
363 Lua = {
364 diagnostics = {
365 globals = { 'vim' }
366 }
367 }
368 }
369}
370
371local t = function(str)
372 return api.nvim_replace_termcodes(str, true, true, true)
373end
374
375local check_back_space = function()
376 local col = fn.col('.') - 1
377 return col == 0 or fn.getline('.'):sub(col, col):match('%s') ~= nil
378end
379
380-- use tab for completion
381_G.tab_complete = function()
382 if fn.pumvisible() == 1 then
383 return fn['compe#confirm']({keys = '<Tab>', select = true})
384 elseif fn['vsnip#available'](1) == 1 then
385 return t "<Plug>(vsnip-expand-or-jump)"
386 elseif check_back_space() then
387 return t "<Tab>"
388 else
389 return fn['compe#complete']()
390 end
391end
392
393map("i", "<Tab>", "v:lua.tab_complete()", {expr = true})
394map("s", "<Tab>", "v:lua.tab_complete()", {expr = true})
395
396
397require('gitsigns').setup{
398 signs = {
399 add = { text = "+"},
400 change = { text = "~"},
401 delete = { text = '-'},
402 },
403 watch_index = {
404 interval = 700,
405 follow_files = true
406 }
407}
408
409require('formatter').setup({
410 logging = false,
411 filetype = {
412 javascript = {
413 -- prettier
414 function()
415 return {
416 exe = "prettier",
417 args = {"--stdin-filepath", api.nvim_buf_get_name(0), '--single-quote'},
418 stdin = true
419 }
420 end
421 },
422 typescript = {
423 -- prettier
424 function()
425 return {
426 exe = "prettier",
427 args = {"--stdin-filepath", api.nvim_buf_get_name(0), '--single-quote'},
428 stdin = true
429 }
430 end
431 },
432 typescriptreact = {
433 -- prettier
434 function()
435 return {
436 exe = "prettier",
437 args = {"--stdin-filepath", api.nvim_buf_get_name(0), '--single-quote'},
438 stdin = true
439 }
440 end,
441 -- -- eslint
442 -- function()
443 -- return {
444 -- exe = "eslint_d",
445 -- args = { '--stdin', '--stdin-filename', api.nvim_buf_get_name(0), '--fix-to-stdout' },
446 -- stdin = true
447 -- }
448 -- end,
449 },
450 python = {
451 -- isort
452 function()
453 return {
454 exe = "isort",
455 args = {"-"},
456 stdin = true
457 }
458 end,
459 -- black
460 function()
461 return {
462 exe = "black",
463 args = {"-"},
464 stdin = true
465 }
466 end,
467 },
468 }
469})
470
471-- format on save
472api.nvim_exec([[
473augroup FormatAutogroup
474 autocmd!
475 autocmd BufWritePost *.js,*.ts,*.tsx,*.py FormatWrite
476augroup END
477]], true)
478
479-- When editing a file, always jump to the last known cursor position.
480-- Don't do it for commit messages, when the position is invalid, or when
481-- inside an event handler (happens when dropping a file on gvim).
482api.nvim_exec([[
483augroup vimrcEx
484 autocmd!
485 autocmd BufReadPost * if &ft != 'gitcommit' && line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g`\"" | endif
486augroup END
487]], true)
488
489-- easy commit
490api.nvim_exec([[
491function ConcatAndCommit(...)
492 let l:commitMessage = join(a:000)
493 execute "!git add --all && git commit -m '" . l:commitMessage . "'"
494endfunction
495
496:command! -nargs=+ Gca call ConcatAndCommit(<f-args>)
497]], true)
498
499map('n', '<leader>m', ':Gca ', {noremap = true})
500
501-- easy replace, TODO map w/ lua
502api.nvim_exec([[
503 nnoremap <leader>r :%s/\<<C-r><C-w>\>//g<Left><Left>
504]], true)
505
506-- search selected text, TODO map w/ lua
507api.nvim_exec([[
508 function! VisualSelection(direction, extra_filter) range
509 let l:saved_reg = @"
510 execute "normal! vgvy"
511
512 let l:pattern = escape(@", "\\/.*'$^~[]")
513 let l:pattern = substitute(l:pattern, "\n$", "", "")
514
515 if a:direction == 'gv'
516 call CmdLine("Ack '" . l:pattern . "' " )
517 elseif a:direction == 'replace'
518 call CmdLine("%s" . '/'. l:pattern . '/')
519 endif
520
521 let @/ = l:pattern
522 let @" = l:saved_reg
523 endfunction
524
525 vnoremap <silent> * :<C-u>call VisualSelection('', '')<CR>/<C-R>=@/<CR><CR>
526 vnoremap <silent> # :<C-u>call VisualSelection('', '')<CR>?<C-R>=@/<CR><CR>
527]], true)
528
529
530require "pears".setup()
531
532require('kommentary.config').configure_language("typescriptreact", {
533 prefer_single_line_comments = true,
534})
535
536require('kommentary.config').configure_language("lua", {
537 prefer_single_line_comments = true,
538})
539
540require('kommentary.config').configure_language("lua", {
541 prefer_single_line_comments = true,
542})
543
544require('kommentary.config').configure_language("typescript", {
545 prefer_single_line_comments = true,
546})
547
548require "bufferline".setup {
549 options = {
550 offsets = {{filetype = "NvimTree", text = "", padding = 1}},
551 modified_icon = "",
552 left_trunc_marker = "",
553 right_trunc_marker = "",
554 max_name_length = 14,
555 max_prefix_length = 13,
556 tab_size = 20,
557 show_tab_indicators = true,
558 show_buffer_close_icons = false,
559 show_close_icon = false,
560 enforce_regular_tabs = false,
561 view = "multiwindow",
562 separator_style = "slant", -- "thin" is also good
563 mappings = "true"
564 }
565}
566
567
568require './snips'
569
570
571-- function to close vim if last buffer is deleted, from https://github.com/ojroques/nvim-bufdel
572function delete_buffer()
573 local buflisted = fn.getbufinfo({buflisted = 1})
574 local cur_winnr, cur_bufnr = fn.winnr(), fn.bufnr()
575 if #buflisted < 2 then cmd 'confirm qall' return end
576 for _, winid in ipairs(fn.getbufinfo(cur_bufnr)[1].windows) do
577 cmd(string.format('%d wincmd w', fn.win_id2win(winid)))
578 cmd(cur_bufnr == buflisted[#buflisted].bufnr and 'bp' or 'bn')
579 end
580 cmd(string.format('%d wincmd w', cur_winnr))
581 local is_terminal = fn.getbufvar(cur_bufnr, '&buftype') == 'terminal'
582 cmd(is_terminal and 'bd! #' or 'silent! confirm bd #')
583end
584
585-- TODO need to make it so I can close a split w/out removing the buffer, will likely need to make a function
586map('n', '<leader>x', ':update|lua delete_buffer()<CR>', {noremap = true, silent = true}) -- make easy to close buffer (and save if changes occured)
587
588map('n', '<leader><leader>', '<C-w>q', {noremap = true, silent = true}) -- make easy to close buffer (and save if changes occured)
589
590
591require'nvim-treesitter.configs'.setup {
592 -- ensure_installed can be "all" or a list of languages { "python", "javascript" }
593 ensure_installed = {"javascript", "python", "typescript"},
594 highlight = { -- enable highlighting for all file types
595 enable = true, -- you can also use a table with list of langs here (e.g. { "python", "javascript" })
596 },
597 autotag = {
598 enable = true,
599 }
600}
601
602require('lualine').setup {
603 options = {
604 theme = 'tokyonight'
605 }
606}
607
608require("toggleterm").setup{
609 -- size can be a number or function which is passed the current terminal
610 size = 20,
611 open_mapping = [[<leader><BS>]],
612 hide_numbers = true, -- hide the number column in toggleterm buffers
613 shade_filetypes = {},
614 shade_terminals = true,
615 shading_factor = 1, -- the degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light
616 start_in_insert = true,
617 insert_mappings = true, -- whether or not the open mapping applies in insert mode
618 persist_size = true,
619 direction = 'float', --'vertical' | 'horizontal' | 'window' | 'float',
620 close_on_exit = true, -- close the terminal window when the process exits
621 shell = vim.o.shell, -- change the default shell
622 -- This field is only relevant if direction is set to 'float'
623 float_opts = {
624 border = 'single', -- 'single' | 'double' | 'shadow' | 'curved' | 'solid' | 'none' ... other options supported by win open
625 width = 100,
626 height = 35,
627 winblend = 3,
628 highlights = {
629 border = "Normal",
630 background = "Normal",
631 }
632 }
633}
634
635require "nvim-treesitter.configs".setup {
636 playground = {
637 enable = true,
638 disable = {},
639 updatetime = 30, -- Debounced time for highlighting nodes in the playground from source code
640 persist_queries = false, -- Whether the query persists across vim sessions
641 keybindings = {
642 toggle_query_editor = 'o',
643 toggle_hl_groups = 'i',
644 toggle_injected_languages = 't',
645 toggle_anonymous_nodes = 'a',
646 toggle_language_display = 'I',
647 focus_language = 'f',
648 unfocus_language = 'F',
649 update = 'R',
650 goto_node = '<cr>',
651 show_help = '?',
652 },
653 }
654}
655
656vim.opt.shadafile = ""
657