From 7402f3649ed07898c98d692d470f6e23170b62c2 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Fri, 4 Feb 2022 02:17:02 +0530 Subject: [PATCH] initial version of my neovim config in lua This took quite a while but I liked doing this. I never really got into vimscript but writing Lua is more fun. --- private_dot_config/nvim/ftplugin/lua.lua | 7 ++ private_dot_config/nvim/ftplugin/sh.lua | 7 ++ private_dot_config/nvim/init.lua | 6 +- private_dot_config/nvim/lua/autocmd.lua | 21 ++++ private_dot_config/nvim/lua/keymaps.lua | 32 ++++++ private_dot_config/nvim/lua/options.lua | 70 ++++++++++++ .../nvim/lua/plugcfg/colorscheme.lua | 7 ++ .../nvim/lua/plugcfg/lightspeed.lua | 32 ++++++ .../nvim/lua/plugcfg/lualine.lua | 10 ++ private_dot_config/nvim/lua/plugcfg/mini.lua | 22 ++++ .../nvim/lua/plugcfg/null-ls.lua | 47 ++++++++ .../nvim/lua/plugcfg/nvim-treesitter.lua | 21 ++++ .../nvim/lua/plugcfg/zen-mode.lua | 13 +++ private_dot_config/nvim/lua/plugins.lua | 102 ++++++++++++------ 14 files changed, 361 insertions(+), 36 deletions(-) create mode 100644 private_dot_config/nvim/ftplugin/lua.lua create mode 100644 private_dot_config/nvim/ftplugin/sh.lua create mode 100644 private_dot_config/nvim/lua/autocmd.lua create mode 100644 private_dot_config/nvim/lua/keymaps.lua create mode 100644 private_dot_config/nvim/lua/options.lua create mode 100644 private_dot_config/nvim/lua/plugcfg/colorscheme.lua create mode 100644 private_dot_config/nvim/lua/plugcfg/lightspeed.lua create mode 100644 private_dot_config/nvim/lua/plugcfg/lualine.lua create mode 100644 private_dot_config/nvim/lua/plugcfg/mini.lua create mode 100644 private_dot_config/nvim/lua/plugcfg/null-ls.lua create mode 100644 private_dot_config/nvim/lua/plugcfg/nvim-treesitter.lua create mode 100644 private_dot_config/nvim/lua/plugcfg/zen-mode.lua diff --git a/private_dot_config/nvim/ftplugin/lua.lua b/private_dot_config/nvim/ftplugin/lua.lua new file mode 100644 index 0000000..efc26a0 --- /dev/null +++ b/private_dot_config/nvim/ftplugin/lua.lua @@ -0,0 +1,7 @@ +vim.bo.shiftwidth = 2 +vim.bo.tabstop = 2 +vim.bo.softtabstop = 2 +vim.bo.expandtab = true +-- code folding using treesitter +vim.wo.foldmethod = 'expr' +vim.wo.foldexpr = 'nvim_treesitter#foldexpr()' diff --git a/private_dot_config/nvim/ftplugin/sh.lua b/private_dot_config/nvim/ftplugin/sh.lua new file mode 100644 index 0000000..efc26a0 --- /dev/null +++ b/private_dot_config/nvim/ftplugin/sh.lua @@ -0,0 +1,7 @@ +vim.bo.shiftwidth = 2 +vim.bo.tabstop = 2 +vim.bo.softtabstop = 2 +vim.bo.expandtab = true +-- code folding using treesitter +vim.wo.foldmethod = 'expr' +vim.wo.foldexpr = 'nvim_treesitter#foldexpr()' diff --git a/private_dot_config/nvim/init.lua b/private_dot_config/nvim/init.lua index e8a680c..583f520 100644 --- a/private_dot_config/nvim/init.lua +++ b/private_dot_config/nvim/init.lua @@ -1,4 +1,6 @@ --- NEOVIM SETTINGS +-- INIT.LUA -require('settings') +require('options') +require('keymaps') +require('autocmd') require('plugins') diff --git a/private_dot_config/nvim/lua/autocmd.lua b/private_dot_config/nvim/lua/autocmd.lua new file mode 100644 index 0000000..76116e0 --- /dev/null +++ b/private_dot_config/nvim/lua/autocmd.lua @@ -0,0 +1,21 @@ +-- NEOVIM AUTOCOMMANDS +-- status updates for lua API on https://github.com/neovim/neovim/pull/14661 + +cmd = vim.cmd + +-- reset the cursor shape back to horizontal when exiting nvim +cmd([[ +augroup tmux_cursor + autocmd! + autocmd VimLeave,VimSuspend * :set guicursor=a:hor100 +augroup END +]]) + +-- disable/hide lsp diagnostics when we enter insert mode, activate/show it in +-- normal mode +cmd([[ +augroup lsp_diagnostics + autocmd! + autocmd InsertEnter * silent! lua vim.diagnostic.hide() + autocmd InsertLeave * silent! lua vim.diagnostic.show() +]]) diff --git a/private_dot_config/nvim/lua/keymaps.lua b/private_dot_config/nvim/lua/keymaps.lua new file mode 100644 index 0000000..c3397ad --- /dev/null +++ b/private_dot_config/nvim/lua/keymaps.lua @@ -0,0 +1,32 @@ +-- NEOVIM KEYBINDINGS + +local g = vim.g +local map = vim.api.nvim_set_keymap +local nmap_opt = { noremap = true, silent = true } +local expr_opt = { noremap = true, expr = true } + +-- leader operations +map('n', '', '', {}) +g.mapleader = ' ' +g.maplocalleader = ' ' +map('n', 's', ':w', nmap_opt) +map('n', 'q', ':wq', nmap_opt) +map('n', 'z', ':ZenMode', nmap_opt) +map('n', 'u', ':PackerSync', nmap_opt) + +-- disable arrow keys (why would you use arrow keys in nvim?!) +map('n', '', '', nmap_opt) +map('n', '', '', nmap_opt) +map('n', '', '', nmap_opt) +map('n', '', '', nmap_opt) + +-- center the position of the cursor on the first search result +-- https://vi.stackexchange.com/a/10776 +map('c', '', 'getcmdtype() =~ "[/?]" ? "zz" : ""', expr_opt) + +-- keep the search results on the center position during navigation +map('n', 'n', 'nzz', nmap_opt) +map('n', 'N', 'Nzz', nmap_opt) + +-- clear any highlights when is pressed +map('n', '', ':noh', nmap_opt) diff --git a/private_dot_config/nvim/lua/options.lua b/private_dot_config/nvim/lua/options.lua new file mode 100644 index 0000000..18c7b88 --- /dev/null +++ b/private_dot_config/nvim/lua/options.lua @@ -0,0 +1,70 @@ +-- NEOVIM OPTIONS AND VARIABLES + +-- API aliases +local g, o, opt = vim.g, vim.o, vim.opt + +-- quit loading these files as soon as they're sourced +local runtime_files = { + 'netrw', + 'netrwPlugin', + 'netrwSettings', + 'netrwFileHandlers', + 'gzip', + 'zip', + 'zipPlugin', + 'tar', + 'tarPlugin', + 'getscript', + 'getscriptPlugin', + 'vimball', + 'vimballPlugin', + '2html_plugin', + 'logipat', + 'rrhelper', + 'spellfile_plugin', + 'matchit', + 'fzf' +} +for _, plug in pairs(runtime_files) do + g['loaded_' .. plug] = 1 +end + +-- don't make nvim search for these providers because we aren't using them +local providers = { + 'python_provider', + 'python3_provider', + 'ruby_provider', + 'node_provider' +} +for _, provider in pairs(providers) do + g['loaded_' .. provider] = 0 +end + +-- nvim configuration options +opt.shortmess:append 'I' +local options = { + guicursor = 'n-v-r:block,i:hor100', + secure = true, + number = true, + relativenumber = true, + termguicolors = true, + cursorline = true, + showmode = false, + signcolumn = 'number', + splitright = true, + splitbelow = true, + ignorecase = true, + smartcase = true, + lazyredraw = true, + synmaxcol = 240, + clipboard = 'unnamedplus', + background = 'dark' +} +for k, v in pairs(options) do + o[k] = v +end + +-- disable filetype.vim and enable filetype.lua whenever it becomes usable +-- https://teddit.net/r/neovim/comments/rvwsl3/introducing_filetypelua_and_a_call_for_help/ +-- g.did_load_filetypes = 0 +-- g.do_filetype_lua = 1 diff --git a/private_dot_config/nvim/lua/plugcfg/colorscheme.lua b/private_dot_config/nvim/lua/plugcfg/colorscheme.lua new file mode 100644 index 0000000..38f094c --- /dev/null +++ b/private_dot_config/nvim/lua/plugcfg/colorscheme.lua @@ -0,0 +1,7 @@ +local clr = 'onedark' + +local ok, _ = pcall(vim.cmd, 'colorscheme ' .. clr) +if not ok then + vim.notify('unable to find ' .. clr) + return +end diff --git a/private_dot_config/nvim/lua/plugcfg/lightspeed.lua b/private_dot_config/nvim/lua/plugcfg/lightspeed.lua new file mode 100644 index 0000000..75cd5e3 --- /dev/null +++ b/private_dot_config/nvim/lua/plugcfg/lightspeed.lua @@ -0,0 +1,32 @@ +--[[ +phaazon/hop.nvim is an excellent alternative and it needs equivalent amount of +keystrokes for jumping when you use `:HopPattern` but it won't jump to single +occurence chars without confirmation and I'm not willing to use multiple +different Hop commands mapped to multiple different keys. I want to be able to +reach each and every character on the screen with only `s` and `S`. + +The disadvantage of lightspeed is that the characters shown on the screen +aren't really deterministic and need to you understand how lightspeed works +with its indicators. It wants you to keep typing the characters that either +precede or follow the character you want to reach. This can be confusing and +might contribute to mistakes. I'll use lightspeed for now and if I make +mistakes, I'll switch to hop. + +the table for lazy loading lightsped is borrowed from +https://github.com/ggandor/lightspeed.nvim/issues/35#issuecomment-939156128 +--]] + +local keys = function() + vim.g.lightspeed_no_default_keymaps = true + local default_keymaps = { + { 'n', 'f', 'Lightspeed_s' }, + { 'n', 'F', 'Lightspeed_S' }, + { 'x', 'f', 'Lightspeed_s' }, + { 'x', 'F', 'Lightspeed_S' }, + } + for _, m in ipairs(default_keymaps) do + vim.api.nvim_set_keymap(m[1], m[2], m[3], { silent = true }) + end +end + +keys() diff --git a/private_dot_config/nvim/lua/plugcfg/lualine.lua b/private_dot_config/nvim/lua/plugcfg/lualine.lua new file mode 100644 index 0000000..85e0737 --- /dev/null +++ b/private_dot_config/nvim/lua/plugcfg/lualine.lua @@ -0,0 +1,10 @@ +local opts = { + options = { + icons_enabled = false, + theme = 'onedark', + component_separators = { '' }, + section_separators = { '' }, + }, +} + +require('lualine').setup(opts) diff --git a/private_dot_config/nvim/lua/plugcfg/mini.lua b/private_dot_config/nvim/lua/plugcfg/mini.lua new file mode 100644 index 0000000..b13b67a --- /dev/null +++ b/private_dot_config/nvim/lua/plugcfg/mini.lua @@ -0,0 +1,22 @@ +local ok, pairs = pcall(require, 'mini.pairs') +if not ok then + vim.notify('unable to find mini.pairs') + return +end + +local ok, surround = pcall(require, 'mini.surround') +if not ok then + vim.notify('unable to find mini.surround') + return +end + +local srd = { + mappings = { + find = '', + find_left = '', + update_n_lines = '' + } +} + +pairs.setup() +surround.setup(srd) diff --git a/private_dot_config/nvim/lua/plugcfg/null-ls.lua b/private_dot_config/nvim/lua/plugcfg/null-ls.lua new file mode 100644 index 0000000..58f0176 --- /dev/null +++ b/private_dot_config/nvim/lua/plugcfg/null-ls.lua @@ -0,0 +1,47 @@ +local ok, null = pcall(require, 'null-ls') +if not ok then + vim.notify('unable to find null-ls') + return +end + +local fn = vim.fn +local null = require('null-ls') +local format = null.builtins.formatting +local linter = null.builtins.diagnostics + +local stylua_path = fn.expand('~/.config/stylua/stylua.toml') +local stylua_status = fn.filereadable(stylua_path) + +-- specify the list of linters and formatters you want to use in this table +local src = { + format.shfmt.with({ + extra_args = { '-i', '2', '-ci', '-sr', '-bn' }, + }), + linter.shellcheck, + format.stylua.with({ + condition = function() + if stylua_status ~= 1 then + vim.notify('unable to find stylua.toml') + return false + else + return true + end + end, + extra_args = { '-f', stylua_path }, + }), +} + +null.setup({ + sources = src, + on_attach = function(client) + -- format on save + if client.resolved_capabilities.document_formatting then + vim.cmd([[ + augroup LspFormatting + autocmd! * + autocmd BufWritePre lua vim.lsp.buf.formatting_sync() + augroup END + ]]) + end + end, +}) diff --git a/private_dot_config/nvim/lua/plugcfg/nvim-treesitter.lua b/private_dot_config/nvim/lua/plugcfg/nvim-treesitter.lua new file mode 100644 index 0000000..6180db5 --- /dev/null +++ b/private_dot_config/nvim/lua/plugcfg/nvim-treesitter.lua @@ -0,0 +1,21 @@ +local ok, configs = pcall(require, 'nvim-treesitter.configs') +if not ok then + vim.notify('unable to find nvim-treesitter') + return +end + +configs.setup { + ensure_installed = { + 'bash', + 'fish', + 'json', + 'toml', + 'yaml', + 'lua', + 'vim' + }, + highlight = { + enable = true, + additional_vim_regex_highlighting = false + } +} diff --git a/private_dot_config/nvim/lua/plugcfg/zen-mode.lua b/private_dot_config/nvim/lua/plugcfg/zen-mode.lua new file mode 100644 index 0000000..8bc30dd --- /dev/null +++ b/private_dot_config/nvim/lua/plugcfg/zen-mode.lua @@ -0,0 +1,13 @@ +local ok, zen = pcall(require, 'zen-mode') +if not ok then + vim.notify('unable to find zen-mode') + return +end + +zen.setup { + plugins = { + tmux = { + enabled = true + } + } +} diff --git a/private_dot_config/nvim/lua/plugins.lua b/private_dot_config/nvim/lua/plugins.lua index cf3475e..45cf7cf 100644 --- a/private_dot_config/nvim/lua/plugins.lua +++ b/private_dot_config/nvim/lua/plugins.lua @@ -1,13 +1,14 @@ --- NEOVIM PLUGINS AND THEIR SETTINGS +-- NEOVIM PLUGINS -- API aliases -local fn = vim.fn +local fn, cmd = vim.fn, vim.cmd +local height = vim.api.nvim_win_get_height -- install packer.nvim if it isn't already installed local packer_path = fn.stdpath('data') .. '/site/pack/packer/start/packer.nvim' local packer_exists = fn.filewritable(fn.expand(packer_path)) -local git_installed = fn.system('command -v git') -if packer_exists ~= 2 and #git_installed > 0 then +local git_installed = fn.executable('git') +if packer_exists ~= 2 and git_installed == 1 then packer_bootstrap = fn.system({ 'git', 'clone', @@ -19,8 +20,8 @@ if packer_exists ~= 2 and #git_installed > 0 then end -- don't continue if packer throws an error for any reason -local status_ok, packer = pcall(require, 'packer') -if not status_ok then +local ok, packer = pcall(require, 'packer') +if not ok then vim.notify("unable to load packer") return end @@ -34,46 +35,79 @@ packer.init { }, } +-- update plugins if this file is written to +cmd([[ + augroup packer_update + autocmd! + autocmd BufWritePost plugins.lua source | PackerCompile + augroup end +]]) + +local cfg = function(name) + return string.format('require("plugcfg.%s")', name) +end + -- list of plugins return require('packer').startup(function(use) - -- manage packer.nvim using packer.nvim + -- package manager for neovim use 'wbthomason/packer.nvim' - -- use the onedark colorscheme + -- a plugin often used as a dependency by other plugins + use { 'nvim-lua/plenary.nvim', module = 'plenary' } + + -- the colorscheme that I like for dark mode use { 'navarasu/onedark.nvim', - config = function() - require('onedark').setup { - code_style = { - comments = 'none' - } - } - require('onedark').load() - end + config = cfg('colorscheme') + } + + -- use lualine for a more informative statusline when using linters + use { + 'nvim-lualine/lualine.nvim', + config = cfg('lualine') } - -- use treesitter for syntax highlighting + -- faster, but buggy, syntax highlighting and code folding use { 'nvim-treesitter/nvim-treesitter', run = ':TSUpdate', - config = function() - require('nvim-treesitter.configs').setup { - ensure_installed = { - 'bash', - 'fish', - 'json', - 'toml', - 'yaml', - 'lua', - 'vim' - }, - highlight = { - enable = true, - additional_vim_regex_highlighting = false - } - } - end + config = cfg('nvim-treesitter') + } + + -- a minimal and focused editing experience + use { + 'folke/zen-mode.nvim', + cmd = 'ZenMode', + config = cfg('zen-mode') + } + + -- jump to characters using lightspeed + use { + 'ggandor/lightspeed.nvim', + keys = { + 'Lightspeed_s', + 'Lightspeed_S' + }, + setup = cfg('lightspeed') + } + + -- create and delete pairs using mini.pairs + -- surround regions with characters using mini.surround + use { + 'echasnovski/mini.nvim', + branch = 'stable', + config = cfg('mini') + } + + -- insert comments using kommentary + -- could've used mini.comment but it doesn't support multi-line comments + use 'b3nj5m1n/kommentary' + + -- use null-ls for linting and formatting code + use { + 'jose-elias-alvarez/null-ls.nvim', + config = cfg('null-ls') } if packer_bootstrap then