From 6863d4c2ebfdd4b792ac0742fa619293ab17f9cc Mon Sep 17 00:00:00 2001 From: Thomas Preisner Date: Thu, 13 Oct 2022 19:29:40 +0200 Subject: [PATCH] config: nvim: rewrite config in lua and use packer as plugin manager --- .gitignore | 2 +- config/nvim/general.vim | 127 -------------------------------- config/nvim/init.lua | 3 + config/nvim/init.vim | 4 - config/nvim/keybindings.vim | 20 ----- config/nvim/lsp.vim | 25 ------- config/nvim/lua/general.lua | 74 +++++++++++++++++++ config/nvim/lua/keybindings.lua | 5 ++ config/nvim/lua/plugins.lua | 61 +++++++++++++++ config/nvim/lua/utils.lua | 30 ++++++++ config/nvim/plugins.vim | 20 ----- 11 files changed, 174 insertions(+), 197 deletions(-) delete mode 100644 config/nvim/general.vim create mode 100644 config/nvim/init.lua delete mode 100644 config/nvim/init.vim delete mode 100644 config/nvim/keybindings.vim delete mode 100644 config/nvim/lsp.vim create mode 100644 config/nvim/lua/general.lua create mode 100644 config/nvim/lua/keybindings.lua create mode 100644 config/nvim/lua/plugins.lua create mode 100644 config/nvim/lua/utils.lua delete mode 100644 config/nvim/plugins.vim diff --git a/.gitignore b/.gitignore index ad6f6c6..f1895c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ zsh/zsh_history zsh/cache -config/nvim/autoload +config/nvim/plugin diff --git a/config/nvim/general.vim b/config/nvim/general.vim deleted file mode 100644 index ce3cc5a..0000000 --- a/config/nvim/general.vim +++ /dev/null @@ -1,127 +0,0 @@ -" disable autocmd -set secure - -" file/directory matching -set wildmode=list:longest,full -" ignore case when completing -if exists('+wildignorecase') - set wildignorecase -endif -" ignorelist -set wildignore=*.o,*.d,*.so,*.class,*.aux,*.log,*.out,*.toc,*.pdf,*.pyc - -" increase command history -set history=1000 -" increase count of possible undos -set undolevels=1000 - -" use unix line-endings and recognize dos endings -set fileformats=unix,dos - -" tabs -set tabstop=4 -set shiftwidth=4 -set autoindent -set smartindent -set smarttab - -" allow backspacing over autoindent and line breaks -set backspace=indent,eol,start - -" already display matches while searching -set incsearch -" only check for case if the searched word contains a capital -set ignorecase -set smartcase -" highlight all matches. use to remove the highlights -set hlsearch - -" treat hyphenated words as a single word -set iskeyword+=- - -" syntax folding -if has('folding') - set foldmethod=syntax - " only use folding when there is enough space -" if &columns > 80 -" set foldcolumn=2 -" endif - set foldlevel=99 - - " dont open fold for block movements - set foldopen-=block -endif - -" allow buffers with changes to be hidden -set hidden - -" correct splitting -if has('vertsplit') - set splitright - set splitbelow -endif - -" Visual -" set text color for dark terminal background -set background=dark - -" activate syntax highlighting -if has('syntax') - syntax enable -endif - -" set colorscheme -colorscheme dichromatic -" TODO: use `colorscheme ron` as fallback? - -" display line at which the current is positioned -if exists('+cursorline') - set cursorline - " remove line and make the line bold instead - highlight CursorLine term=bold cterm=bold - highlight CursorLineNR term=bold cterm=bold ctermbg=darkgrey -endif - -" display warning column at 81 and 121 -let &colorcolumn="81,121" -highlight ColorColumn ctermbg=235 guibg=#2d2d2d - -" display line numbers -set number - -" show cursor position all the time -set ruler - -" show vim tabs all the time -set showtabline=2 - -" show currently typed command -set showcmd - -" define characters to display when using 'set list' -set listchars=tab:→\ ,trail:∙,eol:¬,extends:❯,precedes:❮,nbsp:␣ - -" enable nvim-colorizer -if has('nvim') - set termguicolors - lua require'colorizer'.setup() -endif - -" increase completion performance and reduce time until timeout -set updatetime=300 -set timeoutlen=500 - -" copy paste between vim and everything else -set clipboard=unnamedplus - -" Misc -set mouse=n - -" remove windows ^M when the encoding gets messed up -noremap m mmHmt:%s///ge'tzt'm - -" autoreload vimrc when writing init.vim -au! BufWritePost $MYVIMRC source % - -" add convenience command for force-saving read-only files -cmap w!! w !sudo tee % diff --git a/config/nvim/init.lua b/config/nvim/init.lua new file mode 100644 index 0000000..dbfc04f --- /dev/null +++ b/config/nvim/init.lua @@ -0,0 +1,3 @@ +require('general') +require('keybindings') +require('plugins') diff --git a/config/nvim/init.vim b/config/nvim/init.vim deleted file mode 100644 index aa92ce6..0000000 --- a/config/nvim/init.vim +++ /dev/null @@ -1,4 +0,0 @@ -source $HOME/.config/nvim/plugins.vim -source $HOME/.config/nvim/general.vim -source $HOME/.config/nvim/keybindings.vim -source $HOME/.config/nvim/lsp.vim diff --git a/config/nvim/keybindings.vim b/config/nvim/keybindings.vim deleted file mode 100644 index 3b2fe66..0000000 --- a/config/nvim/keybindings.vim +++ /dev/null @@ -1,20 +0,0 @@ -" allow easier pane switching -nnoremap -nnoremap -nnoremap -nnoremap - -" allow easier pane resizing -nnoremap :resize -2 -nnoremap :resize +2 -nnoremap :vertical resize -2 -nnoremap :vertical resize +2 - -" use TAB to switch to next buffers -nnoremap :bnext -" use Shift-TAB to switch to previous buffers -nnoremap :bprevious - -" use TAB for completion -inoremap pumvisible() ? "\" : "\" -inoremap pumvisible() ? "\" : "\" diff --git a/config/nvim/lsp.vim b/config/nvim/lsp.vim deleted file mode 100644 index f91d667..0000000 --- a/config/nvim/lsp.vim +++ /dev/null @@ -1,25 +0,0 @@ -" lsp-config -if has('nvim') - nnoremap gd lua vim.lsp.buf.definition() - nnoremap gD lua vim.lsp.buf.declaration() - nnoremap gr lua vim.lsp.buf.references() - nnoremap gi lua vim.lsp.buf.implementation() - nnoremap K lua vim.lsp.buf.hover() - nnoremap lua vim.lsp.buf.signature_help() - nnoremap lua vim.lsp.buf.rename() - nnoremap lua vim.lsp.diagnostic.goto_prev() - nnoremap lua vim.lsp.diagnostic.goto_next() - - " auto-format - autocmd BufWritePre *.js lua vim.lsp.buf.formatting_sync(nil, 100) - autocmd BufWritePre *.jsx lua vim.lsp.buf.formatting_sync(nil, 100) - autocmd BufWritePre *.py lua vim.lsp.buf.formatting_sync(nil, 100) - - " setup lsp-providers - " (-> need to be installed manually first and then added here) - lua require'lspconfig'.ccls.setup{} - lua require'lspconfig'.clangd.setup{} - lua require'lspconfig'.pyright.setup{} - lua require'lspconfig'.bashls.setup{} - lua require'lspconfig'.gopls.setup{} -endif diff --git a/config/nvim/lua/general.lua b/config/nvim/lua/general.lua new file mode 100644 index 0000000..42b3c8a --- /dev/null +++ b/config/nvim/lua/general.lua @@ -0,0 +1,74 @@ +-- reduce boilerplate by defining some shortcuts +local opt = vim.opt +local cmd = vim.cmd +local autocmd = vim.api.nvim_create_autocmd + +-------------------- +-- General +-------------------- +opt.mouse = 'n' -- enable mouse support in normal mode +opt.clipboard = 'unnamedplus' -- copy/paste to and from system clipboard + +opt.wildmode = 'list:longest,full' +opt.wildignorecase = true +opt.wildignore = '*.o,*.d,*.so,*.class,*.aux,*.log,*.out,*.toc,*.pdf,*.pyc' + +opt.fileformats = 'unix,dos' -- use identical lineendings for all platforms +opt.secure = true -- disable autocmd for security reasons + +-------------------- +-- Tabs +-------------------- +opt.tabstop = 4 -- 1 tab == N spaces +opt.shiftwidth = 4 -- shift N spaces when pressing tab +opt.smartindent = true -- take syntax into account for applying indentation + +-------------------- +-- Search +-------------------- +opt.ignorecase = true -- ignore capitalization on search +opt.smartcase = true -- ignore capitalication unless word contains a capital + +opt.iskeyword:append '-' -- treat hyphenated words as a single word + +-------------------- +-- Neovim UI +-------------------- +opt.number = true -- line numbers +opt.ruler = true -- cursor position in statusbar +opt.showtabline = 2 -- always show vim tabs +opt.showcmd = true -- display currently typed command +opt.termguicolors = true -- enable 24-bit RGB color + +-- highlight line at which the cursor is positioned +opt.cursorline = true +cmd 'highlight CursorLine term=bold cterm=bold' +cmd 'highlight CursorLineNR term=bold cterm=bold ctermbg=darkgrey' + +-- display warning column at 81 and 121 +opt.colorcolumn = '81,121' +cmd 'highlight ColorColumn ctermbg=235 guibg=#2d2d2d' + +opt.splitright = true -- horizontal split to the right +opt.splitbelow = true -- vertical split to the bottom + +-- define characters to display when using 'set list' +opt.listchars = 'tab:→ ,trail:∙,eol:¬,extends:❯,precedes:❮,nbsp:␣' + +-------------------- +-- Performance +-------------------- +opt.hidden = true -- allow buffers with changes to be hidden +opt.updatetime = 300 -- delay (in ms) to wait before triggering event +opt.timeoutlen = 500 -- time (in ms) to wait for event to complete +opt.history = 10000 -- remember N lines of history + +-------------------- +-- Misc +-------------------- + +-- remove redundant whitespaces on save +autocmd('BufWritePre', { + pattern = '*', + command = ':%s/\\s\\+$//e' +}) diff --git a/config/nvim/lua/keybindings.lua b/config/nvim/lua/keybindings.lua new file mode 100644 index 0000000..ac354e5 --- /dev/null +++ b/config/nvim/lua/keybindings.lua @@ -0,0 +1,5 @@ +local map = require('utils').map -- import map + +-- allow switching between buffers using tab and Shift-tab +map('n', '', ':bnext') +map('n', '', ':bprevious') diff --git a/config/nvim/lua/plugins.lua b/config/nvim/lua/plugins.lua new file mode 100644 index 0000000..01bab71 --- /dev/null +++ b/config/nvim/lua/plugins.lua @@ -0,0 +1,61 @@ +-------------------- +-- Packer Bootstrap +-------------------- +local packer_installed, _ = pcall(require, 'packer') +local packer_bootstrap = false +if (not packer_installed) then + -- ask user whether packer should be bootstrapped + packer_bootstrap = require('utils').prompt( + 'Plugin manager packer not found. Install it now?', + 'Bootstrapping packer now...', + 'Skipping setup of packer and plugins.') + + if (not packer_bootstrap) then + return + end + + -- perform actual bootstrap + local fn = vim.fn + local install_path = fn.stdpath('data') .. '/site/pack/packer/start/packer.nvim' + fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path}) + vim.cmd('packadd packer.nvim') +end + +-- helper function to be used `config` parameter of packer's use to externalize +-- plugin configuration, expects the name of the config file +function load_config(name) + return string.format('require("plugins/%s")', name) +end + +return require('packer').startup({ + function(use) + use 'wbthomason/packer.nvim' + + -------------------- + -- Visuals + -------------------- + use { -- colorblind-friendly colorscheme + 'romainl/vim-dichromatic', + config = function() vim.cmd('colorscheme dichromatic') end, + } + use { -- color highlighter + 'norcalli/nvim-colorizer.lua', + config = function() require('colorizer').setup() end, + } + + -- Automatically set up your configuration after cloning packer.nvim + -- Put this at the end after all plugins + if packer_bootstrap then + require('packer').sync() + end + end, + config = { + display = { + open_fn = require("packer.util").float, + }, + profile = { + enable = true, + threshold = 1, -- the amount in ms that a plugins load time must be over for it to be included in the profile + }, + }, +}) diff --git a/config/nvim/lua/utils.lua b/config/nvim/lua/utils.lua new file mode 100644 index 0000000..292c148 --- /dev/null +++ b/config/nvim/lua/utils.lua @@ -0,0 +1,30 @@ +local M = {} + +-- keybinding mapper +function M.map(mode, lhs, rhs, opts) + local options = { noremap = true, silent = true } + if opts then + options = vim.tbl_extend('force', options, opts) + end + vim.api.nvim_set_keymap(mode, lhs, rhs, options) +end + +-- simple prompt +function M.prompt(msg, accept_msg, deny_msg) + local answer = vim.fn.input(msg .. ' (YES/no) ') + while(true) do + -- clear vim cmdline before printing anything + vim.cmd('mode') + if answer == 'YES' then + print(accept_msg) + return true + elseif string.lower(answer) == 'no' then + print(deny_msg) + return false + end + -- re-request user input + answer = vim.fn.input('Please retry: ' .. msg .. ' (YES/no) ') + end +end + +return M diff --git a/config/nvim/plugins.vim b/config/nvim/plugins.vim deleted file mode 100644 index 7438321..0000000 --- a/config/nvim/plugins.vim +++ /dev/null @@ -1,20 +0,0 @@ -" warn if vim-plug is not available -if empty(glob('~/.config/nvim/autoload/plug.vim')) - echom "Install https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim - \ to ~/.config/nvim/autoload/plug.vim" -endif - -call plug#begin('~/.config/nvim/autoload/plugged') - -" vim colortheme for colorblind -Plug 'romainl/vim-dichromatic' - -" only for nvim! -if has('nvim') - " language server protocol support - Plug 'neovim/nvim-lspconfig' - " colorizes colorcodes - Plug 'norcalli/nvim-colorizer.lua' -endif - -call plug#end()