From 01c2ad0ef540a240e0d1a0207179695bdb30379d Mon Sep 17 00:00:00 2001 From: Sam Al-Sapti Date: Thu, 11 May 2023 02:17:36 +0200 Subject: [PATCH] Rewrite Neovim config in Lua --- install.sh | 4 +- nvim/.config/nvim/init.lua | 250 +++++++++++++++++++++++++++++++++++++ nvim/.config/nvim/init.vim | 218 -------------------------------- 3 files changed, 252 insertions(+), 220 deletions(-) create mode 100644 nvim/.config/nvim/init.lua delete mode 100644 nvim/.config/nvim/init.vim diff --git a/install.sh b/install.sh index eada83c..13e3b41 100755 --- a/install.sh +++ b/install.sh @@ -27,9 +27,9 @@ for pkg in "$@"; do echo " -> Stowing $pkg" # Symlink only the individual files instead of the entire directory - [ "$pkg" = "zsh" ] && ZSH=1 && mkdir -p "$HOME/.config/$pkg" + [ "$pkg" = "nvim" ] && NVIM=1 && mkdir -p "$HOME/.config/$pkg" [ "$pkg" = "scripts" ] && DASH=1 && mkdir -p "$HOME/.local/bin" - [ "$pkg" = "nvim" ] && NVIM=1 + [ "$pkg" = "zsh" ] && ZSH=1 && mkdir -p "$HOME/.config/$pkg" LC_ALL="C" stow -t "$HOME" --ignore="README.md" "$pkg" done diff --git a/nvim/.config/nvim/init.lua b/nvim/.config/nvim/init.lua new file mode 100644 index 0000000..98497af --- /dev/null +++ b/nvim/.config/nvim/init.lua @@ -0,0 +1,250 @@ +-- plugins +require('packer').startup(function(use) + use 'wbthomason/packer.nvim' + use 'ii14/onedark.nvim' + use { + 'mengelbrecht/lightline-bufferline', + requires = { + 'itchyny/lightline.vim' + } + } + use 'kyazdani42/nvim-web-devicons' + use 'sheerun/vim-polyglot' + use 'editorconfig/editorconfig-vim' + use { + 'williamboman/mason-lspconfig.nvim', + requires = { + { + 'williamboman/mason.nvim', + run = ':MasonUpdate' + }, + 'neovim/nvim-lspconfig' + } + } + use { + 'hrsh7th/nvim-cmp', + requires = { + 'hrsh7th/cmp-nvim-lsp', + 'saadparwaiz1/cmp_luasnip', + 'L3MON4D3/LuaSnip' + } + } +end) + +-- basics +vim.opt.number = true +vim.opt.showmode = true +vim.opt.showtabline = 2 +vim.opt.tabstop = 4 +vim.opt.shiftwidth = 4 +vim.opt.softtabstop = 0 +vim.opt.expandtab = true +vim.opt.termguicolors = true +vim.opt.completeopt = { 'menu', 'menuone', 'noselect', 'noinsert' } +vim.cmd.syntax('on') +vim.cmd.colorscheme('onedark') + +-- settings for specific filetypes +vim.api.nvim_create_autocmd('FileType', { + pattern = { 'html', 'css', 'scss', 'json', 'yaml', 'yaml.*' }, + callback = function() + vim.opt_local.tabstop = 2 + vim.opt_local.shiftwidth = 2 + end +}) +vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, { + pattern = { 'zsh*', '.zsh*', '*.zsh' }, + callback = function() + vim.opt.filetype = 'sh' + end +}) + +-- appearance +vim.cmd.highlight({ 'Normal', 'guibg=NONE', 'ctermbg=NONE' }) +vim.g.background = 'dark' + +-- lightline config +vim.g.lightline = { + colorscheme = 'one', + tabline = { + left = { { 'buffers' } }, + right = { { 'close' } } + }, + component_expand = { + buffers = 'lightline#bufferline#buffers' + }, + component_type = { + buffers = 'tabsel' + } +} + +-- lightline-bufferline config +vim.g['lightline#bufferline#show_number'] = 2 +vim.g['lightline#bufferline#shorten_path'] = 0 +vim.g['lightline#bufferline#smart_path'] = 1 +vim.g['lightline#bufferline#enable_devicons'] = 1 +vim.g['lightline#bufferline#unnamed'] = '[No Name]' +vim.g['lightline#bufferline#icon_position'] = 'right' + +-- autocompletion config +local cmp = require('cmp') +local luasnip = require('luasnip') + +luasnip.config.setup {} + +cmp.setup { + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end + }, + mapping = { + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + else + fallback() + end + end, { 'i', 's' }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { 'i', 's' }), + }, + sources = { + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }, +} + +cmp.setup.cmdline('/', { + sources = { + { name = 'buffer' } + } +}) + +cmp.setup.cmdline(':', { + sources = cmp.config.sources({ + { name = 'path' } + }, + { + { name = 'cmdline' } + }) +}) + +-- lsp config +require('mason').setup {} +require('mason-lspconfig').setup { + automatic_installation = true +} +local lsp = require('lspconfig') +local capabilities = require('cmp_nvim_lsp').default_capabilities() + +lsp.ansiblels.setup { + capabilities = capabilities +} +lsp.bashls.setup { + capabilities = capabilities +} +lsp.clangd.setup { + capabilities = capabilities +} +lsp.dockerls.setup { + capabilities = capabilities +} +lsp.gopls.setup { + capabilities = capabilities +} +lsp.lua_ls.setup { + capabilities = capabilities, + settings = { + Lua = { + runtime = { + version = 'LuaJIT' + }, + diagnostics = { + globals = { 'vim' } + }, + workspace = { + library = vim.api.nvim_get_runtime_file('', true), + checkThirdParty = false + }, + telemetry = { + enable = false + } + } + } +} +lsp.pylsp.setup { + capabilities = capabilities +} +lsp.vimls.setup { + capabilities = capabilities, + init_options = { + diagnostic = { + enable = false + }, + isNeovim = true + } +} +lsp.yamlls.setup { + capabilities = capabilities, + settings = { + redhat = { + telemetry = { + enabled = false + } + }, + yaml = { + keyOrdering = false + } + } +} + +-- keyboard mappings +vim.g.mapleader = ';' + +-- buffer navigation +vim.keymap.set('n', '1', 'lightline#bufferline#go(1)') +vim.keymap.set('n', '2', 'lightline#bufferline#go(2)') +vim.keymap.set('n', '3', 'lightline#bufferline#go(3)') +vim.keymap.set('n', '4', 'lightline#bufferline#go(4)') +vim.keymap.set('n', '5', 'lightline#bufferline#go(5)') +vim.keymap.set('n', '6', 'lightline#bufferline#go(6)') +vim.keymap.set('n', '7', 'lightline#bufferline#go(7)') +vim.keymap.set('n', '8', 'lightline#bufferline#go(8)') +vim.keymap.set('n', '9', 'lightline#bufferline#go(9)') +vim.keymap.set('n', '0', 'lightline#bufferline#go(10)') + +-- buffer deletion +vim.keymap.set('n', 'd1', 'lightline#bufferline#delete(1)') +vim.keymap.set('n', 'd2', 'lightline#bufferline#delete(2)') +vim.keymap.set('n', 'd3', 'lightline#bufferline#delete(3)') +vim.keymap.set('n', 'd4', 'lightline#bufferline#delete(4)') +vim.keymap.set('n', 'd5', 'lightline#bufferline#delete(5)') +vim.keymap.set('n', 'd6', 'lightline#bufferline#delete(6)') +vim.keymap.set('n', 'd7', 'lightline#bufferline#delete(7)') +vim.keymap.set('n', 'd8', 'lightline#bufferline#delete(8)') +vim.keymap.set('n', 'd9', 'lightline#bufferline#delete(9)') +vim.keymap.set('n', 'd0', 'lightline#bufferline#delete(10)') + +-- Navigating splits +vim.keymap.set('n', '', '') +vim.keymap.set('n', '', '') +vim.keymap.set('n', '', '') +vim.keymap.set('n', '', '') diff --git a/nvim/.config/nvim/init.vim b/nvim/.config/nvim/init.vim deleted file mode 100644 index 53874b2..0000000 --- a/nvim/.config/nvim/init.vim +++ /dev/null @@ -1,218 +0,0 @@ -" -" ~/.config/nvim/init.vim -" - -""" PLUGINS - -call plug#begin(stdpath('data') . '/plugged') - -Plug 'ii14/onedark.nvim' -Plug 'itchyny/lightline.vim' -Plug 'mengelbrecht/lightline-bufferline' -Plug 'kyazdani42/nvim-web-devicons' -Plug 'sheerun/vim-polyglot' -Plug 'neovim/nvim-lspconfig' -Plug 'williamboman/nvim-lsp-installer' -Plug 'hrsh7th/cmp-nvim-lsp' -Plug 'hrsh7th/cmp-buffer' -Plug 'hrsh7th/cmp-path' -Plug 'hrsh7th/cmp-cmdline' -Plug 'hrsh7th/nvim-cmp' -Plug 'hrsh7th/cmp-vsnip' -Plug 'hrsh7th/vim-vsnip' -Plug 'editorconfig/editorconfig-vim' - -call plug#end() - - -""" LSP SETTINGS -lua << EOF -local lsp_installer = require("nvim-lsp-installer") - --- Register a handler that will be called for all installed servers. --- Alternatively, you may also register handlers on specific server instances instead (see example below). -lsp_installer.on_server_ready(function(server) - local opts = {} - - -- (optional) Customize the options passed to the server - -- if server.name == "tsserver" then - -- opts.root_dir = function() ... end - -- end - - -- This setup() function is exactly the same as lspconfig's setup function. - -- Refer to https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md - server:setup(opts) -end) - --- Setup nvim-cmp. -local has_words_before = function() - local line, col = unpack(vim.api.nvim_win_get_cursor(0)) - return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil -end - -local feedkey = function(key, mode) - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) -end - -local cmp = require'cmp' - -cmp.setup({ - snippet = { - -- REQUIRED - you must specify a snippet engine - expand = function(args) - vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. - -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. - -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. - -- require'snippy'.expand_snippet(args.body) -- For `snippy` users. - end, - }, - mapping = { - [''] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }), - [''] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }), - [''] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }), - [''] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `` mapping. - [''] = cmp.mapping({ - i = cmp.mapping.abort(), - c = cmp.mapping.close(), - }), - [''] = cmp.mapping.confirm({ select = true }), - [''] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_next_item() - elseif vim.fn["vsnip#available"](1) == 1 then - feedkey("(vsnip-expand-or-jump)", "") - elseif has_words_before() then - cmp.complete() - else - fallback() -- The fallback function sends a already mapped key. In this case, it's probably ``. - end - end, { "i", "s" }), - [''] = cmp.mapping(function() - if cmp.visible() then - cmp.select_prev_item() - elseif vim.fn["vsnip#jumpable"](-1) == 1 then - feedkey("(vsnip-jump-prev)", "") - end - end, { "i", "s" }), - }, - sources = cmp.config.sources({ - { name = 'nvim_lsp' }, - { name = 'vsnip' }, -- For vsnip users. - -- { name = 'luasnip' }, -- For luasnip users. - -- { name = 'ultisnips' }, -- For ultisnips users. - -- { name = 'snippy' }, -- For snippy users. - }, { - { name = 'buffer' }, - }) -}) - --- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline('/', { - sources = { - { name = 'buffer' } - } -}) - --- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline(':', { - sources = cmp.config.sources({ - { name = 'path' } - }, { - { name = 'cmdline' } - }) -}) -EOF - - -""" BASIC SETTINGS - -set number -set noshowmode -set showtabline=2 -set tabstop=4 -set shiftwidth=4 -set softtabstop=0 -set expandtab -syntax on -colorscheme onedark - -" Settings for specific filetypes -autocmd FileType html setlocal tabstop=2 shiftwidth=2 -autocmd FileType css setlocal tabstop=2 shiftwidth=2 -autocmd FileType scss setlocal tabstop=2 shiftwidth=2 -autocmd FileType svelte setlocal tabstop=2 shiftwidth=2 -autocmd FileType vue setlocal tabstop=2 shiftwidth=2 -autocmd FileType json setlocal tabstop=2 shiftwidth=2 -autocmd FileType yaml* setlocal tabstop=2 shiftwidth=2 -autocmd BufNewFile,BufRead zsh*,.zsh*,*.zsh set filetype=sh - -""" APPEARANCE - -"Use 24-bit (true-color) mode in Vim/Neovim when outside tmux. -"If you're using tmux version 2.2 or later, you can remove the outermost $TMUX check and use tmux's 24-bit color support -"(see < http://sunaku.github.io/tmux-24bit-color.html#usage > for more information.) -if (has("nvim")) - "For Neovim 0.1.3 and 0.1.4 < https://github.com/neovim/neovim/pull/2198 > - let $NVIM_TUI_ENABLE_TRUE_COLOR=1 -endif -"For Neovim > 0.1.5 and Vim > patch 7.4.1799 < https://github.com/vim/vim/commit/61be73bb0f965a895bfb064ea3e55476ac175162 > -"Based on Vim patch 7.4.1770 (`guicolors` option) < https://github.com/vim/vim/commit/8a633e3427b47286869aa4b96f2bfc1fe65b25cd > -" < https://github.com/neovim/neovim/wiki/Following-HEAD#20160511 > -if (has("termguicolors")) - set termguicolors -endif - -" Set dark, transparent background, should default to dark, but it's there just in case -hi Normal guibg=NONE ctermbg=NONE -let background = 'dark' - -" lightline config -let g:lightline = {} -let g:lightline.colorscheme = 'one' -let g:lightline.tabline = { 'left': [ [ 'buffers' ] ], 'right': [ [ 'close' ] ] } -let g:lightline.component_expand = { 'buffers': 'lightline#bufferline#buffers' } -let g:lightline.component_type = { 'buffers': 'tabsel' } - -" lightline-bufferline config -let g:lightline#bufferline#show_number = 2 -let g:lightline#bufferline#shorten_path = 0 -let g:lightline#bufferline#smart_path = 1 -let g:lightline#bufferline#enable_devicons = 1 -let g:lightline#bufferline#unnamed = '[No Name]' -let g:lightline#bufferline#icon_position = 'right' - - -""" KEYBOARD MAPPINGS - -" Leader key -let mapleader=';' - -" Navigating buffers -nmap 1 lightline#bufferline#go(1) -nmap 2 lightline#bufferline#go(2) -nmap 3 lightline#bufferline#go(3) -nmap 4 lightline#bufferline#go(4) -nmap 5 lightline#bufferline#go(5) -nmap 6 lightline#bufferline#go(6) -nmap 7 lightline#bufferline#go(7) -nmap 8 lightline#bufferline#go(8) -nmap 9 lightline#bufferline#go(9) -nmap 0 lightline#bufferline#go(10) - -" Deleting buffers -nmap d1 lightline#bufferline#delete(1) -nmap d2 lightline#bufferline#delete(2) -nmap d3 lightline#bufferline#delete(3) -nmap d4 lightline#bufferline#delete(4) -nmap d5 lightline#bufferline#delete(5) -nmap d6 lightline#bufferline#delete(6) -nmap d7 lightline#bufferline#delete(7) -nmap d8 lightline#bufferline#delete(8) -nmap d9 lightline#bufferline#delete(9) -nmap d0 lightline#bufferline#delete(10) - -" Navigating splits -nnoremap -nnoremap -nnoremap -nnoremap