Last active
June 14, 2023 22:31
-
-
Save marcs-feh/a32c84de82ebba958684da5835aaf927 to your computer and use it in GitHub Desktop.
Extra thicc all-in-one-place init.lua for neovim, quite useful for remote machines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- General Options --- | |
do | |
local options = { | |
backup = false, | |
hidden = true, | |
clipboard = "unnamedplus", | |
cmdheight = 1, | |
completeopt = { "menuone", "noselect" }, | |
conceallevel = 0, | |
fileencoding = "utf-8", | |
hlsearch = false, | |
ignorecase = true, | |
mouse = "a", | |
pumheight = 10, | |
showmode = false, | |
showtabline = 2, | |
smartcase = true, | |
splitbelow = true, | |
splitright = true, | |
swapfile = false, | |
timeoutlen = 800, | |
undofile = true, | |
updatetime = 300, | |
smartindent = true, | |
writebackup = false, | |
expandtab = false, | |
termguicolors = true, | |
tabstop = 2, | |
shiftwidth = 2, | |
cursorline = false, | |
number = true, | |
relativenumber = false, | |
numberwidth = 2, | |
signcolumn = "yes", | |
wrap = false, | |
foldmethod = 'expr', | |
foldexpr = 'nvim_treesitter#foldexpr()', | |
foldlevelstart = 99, | |
scrolloff = 8, | |
sidescrolloff = 12, | |
} | |
-- Apply options | |
for opt, val in pairs(options) do | |
vim.opt[opt] = val | |
end | |
-- Stop making line comments when pressing o, this abomination is required | |
-- because Vim's ftplugins are fucking retarded. | |
vim.cmd [[autocmd FileType * set formatoptions-=o]] | |
vim.opt.shortmess:append "c" | |
end | |
--- Utilities --- | |
local utils = { | |
def_key_opts = { noremap = true, silent = true }, | |
-- Execute one or a series of vim commands | |
vim_cmd = function (cmd) | |
if type(cmd) ~= 'table' then | |
cmd = { cmd } | |
end | |
for _, c in ipairs(cmd) do | |
vim.cmd(c) | |
end | |
end, | |
-- Set global keymap | |
keymap = function(mode, seq, cmd, options) | |
if not options then | |
options = def_key_opts | |
end | |
vim.keymap.set(mode, seq, cmd, options) | |
end, | |
-- Use table of opt=val | |
vim_set = function (tbl_name, t) | |
for k, v in pairs(t) do | |
vim[tbl_name][k] = v | |
end | |
end, | |
-- Wrapper for mini.align | |
quick_align = function(buf_num, pattern) | |
local api, fn = vim.api, vim.fn | |
local from = api.nvim_buf_get_mark(buf_num, "<")[1] | |
local to = api.nvim_buf_get_mark(buf_num, ">")[1] | |
if from > to then | |
from, to = to, from | |
end | |
if not pattern then | |
pattern = fn.input('Pattern: ', '', 'buffer') | |
end | |
local lines = api.nvim_buf_get_lines(buf_num, from - 1, to, true) | |
local new_lines = MiniAlign.align_strings(lines, | |
{ split_pattern = pattern, justify_side = 'left' }) | |
api.nvim_buf_set_lines(buf_num, from - 1, to, true, new_lines) | |
end, | |
} | |
--- Keymaps --- | |
do | |
-- Set to true on systems which dont allow swapping ESC with CapsLock | |
local jk_esc = true | |
local map = utils.keymap | |
-- Remap space as leader key | |
map("", "<Space>", "<Nop>") | |
vim.g.mapleader = " " | |
vim.g.maplocalleader = " " | |
map("", "Q", "<Nop>") | |
map("", "K", "<Nop>") -- Make sure to override this *after* this block was loaded | |
map("n", "<C-s>", ":w!<CR>") | |
map("n", "<leader>l", ":noh<CR>:echo<CR>") | |
map("n", "<leader>W", ":%s/\\s\\+$//<CR>:noh<CR>") | |
map("n", "<leader>e", ":Telescope find_files<CR>") | |
if jk_esc then | |
map("i", "jk", "<ESC>") | |
map("i", "jk", "<ESC>") | |
-- Use Ctrl+x as a universal ESC replacement | |
map("", "<C-x>", "<ESC>") | |
end | |
-- Select all | |
map("n", "<C-a>", ":normal ggVG<CR>") | |
-- Navigate buffers | |
map("n", "L", ":bnext<CR>") | |
map("n", "H", ":bprevious<CR>") | |
-- New buffer | |
map("n", "<leader>n", ":enew<CR>") | |
-- Better page up/down | |
map("n", "<C-u>", "<C-u>zz") | |
map("n", "<C-d>", "<C-d>zz") | |
-- Split windows | |
map("n", "<leader>sh", ":split<CR>") | |
map("n", "<leader>sv", ":vsplit<CR>") | |
-- Expand window | |
map("n", "<leader>F", ":resize<CR>:vertical resize<CR>") | |
-- Better window navigation | |
map("n", "<A-h>", "<C-w>h") | |
map("n", "<A-j>", "<C-w>j") | |
map("n", "<A-k>", "<C-w>k") | |
map("n", "<A-l>", "<C-w>l") | |
map("n", "<leader>q", ":close<CR>") | |
map("n", "<leader>x", ":bdelete<CR>") | |
map("n", "<leader>X", ":bdelete!<CR>") | |
-- Resize windows | |
map("n", "<A-K>", ":resize +2<CR>") | |
map("n", "<A-J>", ":resize -2<CR>") | |
map("n", "<A-H>", ":vertical resize -2<CR>") | |
map("n", "<A-L>", ":vertical resize +2<CR>") | |
-- Re organize windows | |
map("n", "<leader>H", "<C-w>H") | |
map("n", "<leader>J", "<C-w>J") | |
map("n", "<leader>K", "<C-w>K") | |
map("n", "<leader>L", "<C-w>L") | |
-- Mini.completion | |
map("i", "<Tab>", [[pumvisible() ? "\<C-n>" : "\<Tab>"]], { noremap = true, expr = true } ) | |
map("i", "<S-Tab>", [[pumvisible() ? "\<C-p>" : "\<S-Tab>"]], { noremap = true, expr = true }) | |
-- Stay in indent mode | |
map("v", "<", "<gv") | |
map("v", ">", ">gv") | |
-- Quick align | |
map("v", "<CR>", "<ESC>:lua QuickAlign(0, nil)<CR>") | |
-- Toggle Pairs | |
map("n", "<leader><C-p>", function() | |
local b = vim.b.minipairs_disable | |
print(('Autopairs: %s'):format(b and 'ON' or 'OFF')) | |
vim.b.minipairs_disable = not b | |
end) | |
-- Move text | |
map("x", "<C-j>", ":move '>+1<cr>gv-gv") | |
map("x", "<C-k>", ":move '<-2<cr>gv-gv") | |
-- Open terminal | |
map("n", "<leader>T", | |
":split | :resize 6 | :terminal<CR>" | |
..":setlocal nonumber wrap signcolumn=no<CR>") | |
-- Better terminal navigation | |
map("t", "<A-h>", "<C-\\><C-N><C-w>h") | |
map("t", "<A-j>", "<C-\\><C-N><C-w>j") | |
map("t", "<A-k>", "<C-\\><C-N><C-w>k") | |
map("t", "<A-l>", "<C-\\><C-N><C-w>l") | |
map("t", "<A-ESC>", "<C-\\><C-N>") | |
map("t", "<C-A-d>", "<C-\\><C-N>:bdelete!<CR>") | |
-- Resize in terminal mode | |
map("t", "<A-K>", "<C-\\><C-N>:resize +2<CR>a") | |
map("t", "<A-J>", "<C-\\><C-N>:resize -2<CR>a") | |
map("t", "<A-H>", "<C-\\><C-N>:vertical resize -2<CR>a") | |
map("t", "<A-L>", "<C-\\><C-N>:vertical resize +2<CR>a") | |
end | |
--- Autocmd --- | |
do | |
local map = utils.keymap | |
local set = function(t) utils.vim_set('opt_local', t) end | |
local api = vim.api | |
local g = vim.g | |
local b = vim.b | |
-- Indent sensitive languages and/or languages that look weird with hard tabs | |
api.nvim_create_autocmd('FileType', { | |
pattern = 'markdown,ninja,scheme,org,python,nim,lisp,sml,clojure,vim', | |
callback = function() | |
set { | |
expandtab = true, | |
} | |
end | |
}) | |
-- Odin | |
api.nvim_create_autocmd('FileType', { | |
pattern = 'odin', | |
callback = function() | |
set { | |
expandtab = false, | |
commentstring = '// %s', | |
} | |
b.minipairs_disable = true | |
end | |
}) | |
-- Shell languages | |
api.nvim_create_autocmd('FileType', { | |
pattern = 'bash,zsh,sh,fish,ps1', | |
callback = function() | |
b.minipairs_disable = true | |
end | |
}) | |
-- SQL | |
api.nvim_create_autocmd('FileType', { | |
pattern = 'sql', | |
callback = function() | |
set { | |
expandtab = true, | |
commentstring = '--%s', | |
} | |
end | |
}) | |
-- XML, HTML | |
api.nvim_create_autocmd('FileType', { | |
pattern = 'xml,html', | |
callback = function() | |
set { | |
expandtab = true, | |
shiftwidth = 1, | |
tabstop = 1, | |
} | |
end, | |
}) | |
-- Python | |
api.nvim_create_autocmd('FileType', { | |
pattern = 'python', | |
callback = function() | |
g.python_recommended_style = 0 | |
set { | |
expandtab = true, | |
tabstop = 2, | |
shiftwidth = 2, | |
} | |
end, | |
}) | |
-- C/C++ | |
api.nvim_create_autocmd('FileType', { | |
pattern = 'c,cpp', | |
callback = function() | |
set { | |
commentstring = '// %s', | |
foldmethod = 'indent', | |
} | |
b.minipairs_disable = true | |
local opts = {noremap = true, silent = true, buffer = 0} | |
map('i', '<C-f>', '->', opts) | |
-- cmd [[TSDisable indent]] -- Treesitter indentation doesnt play very well with macros | |
end | |
}) | |
end | |
--- Plugins --- | |
do | |
local ensure_packer = function() | |
local fn = vim.fn | |
local install_path = fn.stdpath('config')..'/pack/packer/start/packer.nvim' | |
if fn.empty(fn.glob(install_path)) > 0 then | |
fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path}) | |
vim.cmd [[packadd packer.nvim]] | |
return true | |
end | |
return false | |
end | |
local packer_bootstrap = ensure_packer() | |
local packer = require 'packer' | |
packer.init { | |
package_root = vim.fn.stdpath('config')..'/pack', | |
compile_path = vim.fn.stdpath('config')..'/pack/packer/packer_compiled.lua', | |
} | |
packer.startup(function(use) | |
use 'wbthomason/packer.nvim' -- Package manager | |
use 'echasnovski/mini.nvim' -- Many small neovim extensions | |
use 'nvim-treesitter/nvim-treesitter' -- Good highlighting, folding, etc. | |
use 'nvim-lua/plenary.nvim' -- Utilities that some plugins depend on | |
use 'neovim/nvim-lspconfig' -- LSP configurations | |
use 'nvim-telescope/telescope.nvim' -- Extensible fuzzy finder | |
if packer_bootstrap then packer.sync() end | |
end) | |
end | |
--- Mini.nvim setup --- | |
do | |
-- Extend motions a/i | |
require 'mini.ai'.setup {} | |
-- Surround motions | |
require 'mini.surround'.setup {} | |
-- Completion | |
require 'mini.completion'.setup {} | |
-- Alignment motions | |
require 'mini.align'.setup {} | |
-- Statusline | |
require 'mini.statusline'.setup {} | |
-- Tabline | |
require 'mini.tabline'.setup {} | |
-- Comment motions | |
require 'mini.comment'.setup { | |
mappings = { | |
comment = '<leader>c', | |
comment_line = '<leader>c', | |
}, | |
} | |
-- Auto pairs | |
require 'mini.pairs'.setup() | |
vim.g.minipairs_disable = false -- Disable by default | |
-- Colors (base16) | |
require 'mini.base16'.setup { | |
palette = { -- Slimmed down version of marcs-feh/nvim-theme | |
base00 = '#1d2021', base01 = '#303536', base02 = '#434a4c', base03 = '#606a6c', | |
base04 = '#eed89f', base05 = '#f4e5bf', base06 = '#fcf8ee', base07 = '#707a7c', | |
base08 = '#ebe3cb', base09 = '#8ac87d', base0A = '#81aece', base0B = '#cd9169', | |
base0C = '#f4e5bf', base0D = '#ecdc8b', base0E = '#cda2e3', base0F = '#f4e5bf', | |
}, | |
} | |
-- Override some | |
vim.cmd [[hi! link Repeat Keyword]] | |
vim.cmd [[hi! link PreProc Keyword]] | |
vim.cmd [[hi! link Include Preproc]] | |
vim.cmd [[hi! link @keyword.return Keyword]] | |
vim.cmd [[hi! link @constant.builtin Constant]] | |
vim.cmd [[hi! link Constant Identifier]] | |
vim.cmd [[hi! link LineNr Comment]] | |
vim.cmd [[hi! link SignColumn Comment]] | |
end | |
--- Treesitter --- | |
do | |
local ts_config = require 'nvim-treesitter.configs' | |
ts_config.setup { | |
ensure_installed = { | |
--[[ Recommendations, you can delete this line to enable all | |
-- General purpose (Systems programming) | |
'c', 'cpp', 'odin', 'zig', 'rust', | |
-- General purpose (Managed) | |
'java', 'kotlin', 'c_sharp', 'go', 'python', 'ruby', 'haxe', | |
-- General purpose (Functional) | |
'scala', 'erlang', 'elixir', 'ocaml', 'haskell', 'clojure', 'commonlisp', | |
-- Web dev | |
'javascript', 'typescript', 'php', 'dart', 'hack', | |
-- Graphics and GPU accel. | |
'glsl', 'cuda', 'hlsl', | |
-- Scripting | |
'lua', 'scheme', 'vim', 'fish', 'bash', 'perl', | |
-- Build systems | |
'make', 'ninja', 'cmake', 'meson', | |
-- Markup and configuration | |
'html', 'css', 'json', 'org', 'latex', 'ini', 'toml', 'yaml', 'markdown', 'dockerfile', | |
-- Other | |
'gitignore', 'gitcommit', 'diff', 'sql', 'awk', 'graphql', 'verilog', 'nix', | |
--]] | |
}, | |
ignore_install = {'phpdoc', 'javadoc', 'v'}, | |
highlight = { | |
enable = true, | |
additional_vim_regex_highlighting = false, | |
}, | |
indent = { | |
enable = true, | |
}, | |
} | |
end | |
--- LSP --- | |
do | |
-- Enable a server by providing a config table or using `true` | |
local enabled_servers = { | |
-- Python | |
pyright = true, | |
-- C/C++ | |
clangd = { | |
cmd = {'clangd', '-header-insertion=never'} | |
}, | |
-- Zig | |
zls = nil, | |
} | |
local map = utils.keymap | |
local lsp_conf = require 'lspconfig' | |
-- Default on_attach function, sets keybindings | |
local def_on_attach = function(_, bufnr) | |
local opts = { noremap=true, silent=true, buffer=bufnr } | |
map('n', 'gD', vim.lsp.buf.declaration, opts) | |
map('n', 'gd', vim.lsp.buf.definition, opts) | |
map('n', 'K', vim.lsp.buf.hover, opts) | |
map('n', 'gi', vim.lsp.buf.implementation, opts) | |
map('n', '<C-k>', vim.lsp.buf.signature_help, opts) | |
map('n', '<leader>vrn', vim.lsp.buf.rename, opts) | |
map('n', '<leader>vca', vim.lsp.buf.code_action, opts) | |
map('n', '<leader>r', vim.lsp.buf.references, opts) | |
map('n', '<leader>D', ':Telescope diagnostics<CR>') | |
map('n', '<leader>vS', ':LspStop<CR>') | |
map('n', '<leader>vR', ':LspRestart<CR>') | |
end | |
-- Apply configs | |
for server, cfg in pairs(enabled_servers) do | |
if cfg then | |
local user_conf = (type(cfg) == 'table') and cfg or {} | |
if not user_conf.on_attach then | |
user_conf.on_attach = def_on_attach | |
end | |
lsp_conf[server].setup(user_conf) | |
end | |
end | |
end | |
--- Export globals --- | |
do | |
QuickAlign = utils.quick_align | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment