config: nvim: rewrite config in lua and use packer as plugin manager

This commit is contained in:
Thomas Preisner 2022-10-13 19:29:40 +02:00
parent 07be5a4e7a
commit 6863d4c2eb
11 changed files with 174 additions and 197 deletions

View file

@ -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'
})

View file

@ -0,0 +1,5 @@
local map = require('utils').map -- import map
-- allow switching between buffers using tab and Shift-tab
map('n', '<TAB>', ':bnext<CR>')
map('n', '<S-TAB>', ':bprevious<CR>')

View file

@ -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
},
},
})

30
config/nvim/lua/utils.lua Normal file
View file

@ -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