Last active
July 20, 2022 02:27
-
-
Save mvllow/0d8390281d849165459437157abfad76 to your computer and use it in GitHub Desktop.
neovim playground
This file contains 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
--- init.lua | |
--- https://github.com/mvllow/dots | |
--- | |
--- Single file neovim config. | |
--- | |
--- Filetype-specific settings are located in `/ftplugin`. | |
--- See `:help ftplugin`. | |
-- Set global leader key to later prefix custom keymaps. | |
-- See `:help leader`. | |
vim.g.mapleader = ' ' | |
vim.keymap.set('n', '<space>', '<nop>', { silent = true }) | |
-- Attempt to bootstrap plugin manager, packer. | |
-- See https://github.com/wbthomason/packer.nvim#quickstart for manual | |
-- installations. | |
local install_path = vim.fn.stdpath('data') | |
.. '/site/pack/packer/start/packer.nvim' | |
if vim.fn.empty(vim.fn.glob(install_path)) > 0 then | |
vim.fn.execute( | |
'!git clone --depth 1 https://github.com/wbthomason/packer.nvim ' | |
.. install_path | |
) | |
end | |
--- Plugins. | |
--- See `:help packer.use`. | |
require('packer').startup(function(use) | |
-- Manage packer via packer | |
use('wbthomason/packer.nvim') | |
-- Support .editorconfig (https://editorconfig.org). | |
use('editorconfig/editorconfig-vim') | |
-- Enable comments. | |
-- Common usage includes `gcc` to comment line and `gc` to comment selection. | |
use({ | |
'numToStr/Comment.nvim', | |
-- Packer's config takes a function that will run after the plugin loads. | |
-- The `require(...).setup()` seen below is a common convention to initialise | |
-- lua based plugins. | |
config = function() | |
require('comment').setup() | |
end, | |
}) | |
-- Improved syntax parser / highlighter. | |
use({ | |
'nvim-treesitter/nvim-treesitter', | |
run = ':TSUpdate', | |
config = function() | |
require('nvim-treesitter.configs').setup({ | |
ensure_installed = 'all', | |
ignore_install = { 'phpdoc' }, | |
highlight = { enable = true }, | |
}) | |
end, | |
}) | |
-- Colorscheme. | |
use({ | |
'rose-pine/neovim', | |
as = 'rose-pine', | |
config = function() | |
require('rose-pine').setup({ disable_italics = true }) | |
vim.cmd('colorscheme rose-pine') | |
end, | |
}) | |
-- Multi-purpose fuzzy search. | |
use({ | |
'nvim-telescope/telescope.nvim', | |
config = function() | |
require('telescope').setup({ | |
defaults = { | |
layout_config = { | |
horizontal = { | |
-- Increase preview width. | |
preview_width = 0.6, | |
}, | |
}, | |
}, | |
}) | |
end, | |
}) | |
-- File explorer sidebar. | |
use({ | |
'kyazdani42/nvim-tree.lua', | |
config = function() | |
require('nvim-tree').setup({ | |
actions = { | |
open_file = { | |
quit_on_open = true, | |
}, | |
}, | |
filters = { | |
-- Hide ".git" folder. | |
custom = { '^.git$' }, | |
}, | |
git = { | |
-- Do not hide gitignored files. | |
ignore = false, | |
}, | |
renderer = { | |
icons = { | |
-- Do not show tree icons. | |
show = { | |
file = false, | |
folder = false, | |
folder_arrow = false, | |
git = false, | |
}, | |
}, | |
}, | |
trash = { | |
cmd = 'trash', | |
}, | |
view = { | |
mappings = { | |
list = { | |
-- Replace destructive default with trash program. | |
-- E.g. https://github.com/sindresorhus/trash-cli | |
{ key = 'd', action = 'trash' }, | |
{ key = 'D', action = 'remove' }, | |
}, | |
}, | |
side = 'right', | |
}, | |
}) | |
end, | |
}) | |
-- Language servers. | |
use({ | |
'neovim/nvim-lspconfig', | |
requires = 'folke/lua-dev.nvim', | |
config = function() | |
local function on_attach(_, bufnr) | |
local opts = { buffer = bufnr, silent = true } | |
vim.keymap.set('i', '<c-k>', vim.lsp.buf.signature_help, opts) | |
vim.keymap.set('n', '<leader>a', vim.lsp.buf.code_action, opts) | |
vim.keymap.set('n', '<leader>r', vim.lsp.buf.rename, opts) | |
vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts) | |
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts) | |
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts) | |
vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) | |
end | |
local capabilities = vim.lsp.protocol.make_client_capabilities() | |
-- Improve compatibility with nvim-cmp completions. | |
capabilities = require('cmp_nvim_lsp').update_capabilities( | |
capabilities | |
) | |
local lspconfig = require('lspconfig') | |
lspconfig.sumneko_lua.setup(require('lua-dev').setup({ | |
lspconfig = { | |
on_attach = on_attach, | |
capabilities = capabilities, | |
}, | |
})) | |
-- Servers must be available in your path. | |
-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md | |
local servers = { | |
'cssls', | |
'gopls', | |
'html', | |
'jsonls', | |
'rust_analyzer', | |
'svelte', | |
'tailwindcss', | |
'tsserver', | |
} | |
for _, server in ipairs(servers) do | |
lspconfig[server].setup({ | |
on_attach = on_attach, | |
capabilities = capabilities, | |
}) | |
end | |
end, | |
}) | |
-- Code actions, diagnostics, and formatting. | |
use({ | |
'jose-elias-alvarez/null-ls.nvim', | |
requires = 'nvim-lua/plenary.nvim', | |
config = function() | |
local null_ls = require('null-ls') | |
-- Source cmd must be available in your path. | |
-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md | |
local sources = { | |
null_ls.builtins.code_actions.xo, | |
null_ls.builtins.diagnostics.xo, | |
null_ls.builtins.formatting.fish_indent, | |
null_ls.builtins.formatting.gofmt, | |
null_ls.builtins.formatting.goimports, | |
null_ls.builtins.formatting.prettierd.with({ | |
extra_filetypes = { 'jsonc', 'svelte' }, | |
}), | |
null_ls.builtins.formatting.rustfmt, | |
null_ls.builtins.formatting.shfmt, | |
null_ls.builtins.formatting.stylua, | |
} | |
null_ls.setup({ | |
sources = sources, | |
on_attach = function(client, bufnr) | |
-- Format on save. | |
if client.supports_method('textDocument/formatting') then | |
vim.api.nvim_create_autocmd('BufWritePre', { | |
buffer = bufnr, | |
callback = function() | |
vim.lsp.buf.format({ | |
bufnr = bufnr, | |
filter = function(lsp_client) | |
return lsp_client.name == 'null-ls' | |
end, | |
}) | |
end, | |
}) | |
end | |
end, | |
}) | |
end, | |
}) | |
-- Completions and snippets. | |
use({ | |
'hrsh7th/nvim-cmp', | |
requires = { 'hrsh7th/cmp-nvim-lsp', 'L3MON4D3/LuaSnip' }, | |
config = function() | |
local cmp = require('cmp') | |
cmp.setup({ | |
snippet = { | |
expand = function(args) | |
require('luasnip').lsp_expand(args.body) | |
end, | |
}, | |
mapping = cmp.mapping.preset.insert({ | |
['<c-space>'] = cmp.mapping.complete({ select = false }), | |
['<cr>'] = cmp.mapping.confirm({ | |
behavior = cmp.ConfirmBehavior.Replace, | |
select = false, | |
}), | |
['<tab>'] = function(fallback) | |
if cmp.visible() then | |
cmp.select_next_item() | |
else | |
fallback() | |
end | |
end, | |
['<s-tab>'] = function(fallback) | |
if cmp.visible() then | |
cmp.select_prev_item() | |
else | |
fallback() | |
end | |
end, | |
}), | |
sources = { | |
{ name = 'nvim_lsp' }, | |
}, | |
}) | |
end, | |
}) | |
end) | |
-- Add virtual color column per line only when necessary. | |
vim.cmd('hi LilColorColumn cterm=reverse gui=reverse') | |
vim.fn.matchadd('LilColorColumn', '\\%81v', 100) | |
--- Options. | |
--- See `:help <option>`. | |
-- Enable mouse support. | |
vim.opt.mouse = 'a' | |
-- Set tab width. The three options below apply to different scenarios, but | |
-- will usually be the same value. | |
vim.opt.tabstop = 4 | |
vim.opt.softtabstop = 4 | |
vim.opt.shiftwidth = 4 | |
-- Enable persistent undo through neovim sessions. | |
vim.opt.undofile = true | |
-- Set default direction when creating a new split. | |
vim.opt.splitbelow = true | |
vim.opt.splitright = true | |
-- Set scroll offset margin. E.g. start scrolling before your cursor gets to | |
-- end of the window. | |
vim.opt.scrolloff = 3 | |
-- Match line indentation when wrapping text. | |
vim.opt.breakindent = true | |
-- Ignore case unless search contains non-lowercase text. | |
vim.opt.ignorecase = true | |
vim.opt.smartcase = true | |
-- Time (ms) between swap file updates (file backups). | |
vim.opt.updatetime = 250 | |
-- Always show left gutter. This is where diagnostic information will be shown. | |
vim.opt.signcolumn = 'yes' | |
-- Enable global statusline. | |
-- %f current filename | |
-- %M file modified status, e.g. "+" | |
-- %= flexible spacer | |
-- %l current line | |
-- %c current column | |
-- ♥ love all | |
vim.opt.laststatus = 3 | |
vim.opt.statusline = ' %f %M %= %l:%c ♥ ' | |
-- Shorten system messages. | |
vim.opt.shortmess:append('c') | |
-- Recommended option from our completion plugin, nvim-cmp. | |
vim.opt.completeopt = 'menu,menuone,noselect' | |
-- Number of suggestions to show in the popup menu. | |
vim.opt.pumheight = 3 | |
--- Autocommands. | |
--- See `:help autocommand`. | |
-- Stop "o" continuing comments. | |
vim.api.nvim_create_autocmd('BufEnter', { | |
command = 'setlocal formatoptions-=o', | |
}) | |
-- Equally resize splits. | |
vim.api.nvim_create_autocmd('VimResized', { | |
command = 'tabdo wincmd =', | |
}) | |
-- Highlight copied text. | |
vim.api.nvim_create_autocmd('TextYankPost', { | |
callback = function() | |
vim.highlight.on_yank() | |
end, | |
}) | |
--- Keymaps. | |
--- See `:help vim.keymap.set`. | |
-- Set shared options for keymaps. | |
local opts = { silent = true } | |
vim.keymap.set({ 'n', 'v' }, 'j', 'gj', opts) -- move through wrapped lines | |
vim.keymap.set({ 'n', 'v' }, 'k', 'gk', opts) -- move through wrapped lines | |
vim.keymap.set('v', '<', '<gv', opts) -- dedent (keep selection) | |
vim.keymap.set('v', '>', '>gv', opts) -- indent (keep selection) | |
vim.keymap.set('n', '<esc>', ':noh<cr>', opts) -- clear search highlights | |
vim.keymap.set('n', '*', '*N', opts) -- search word under cursor (keep position) | |
vim.keymap.set('v', '*', [[y/\V<c-r>=escape(@",'/\')<cr><cr>N]], opts) -- search selection (keep position) | |
-- Goto keymaps. | |
vim.keymap.set('n', 'go', '<c-o>', opts) -- goto previous position | |
vim.keymap.set('n', 'gp', '<c-^>', opts) -- goto previously focused buffer | |
vim.keymap.set('n', 'gm', '%', opts) -- goto matching pair | |
vim.keymap.set('n', 'g[', vim.diagnostic.goto_prev, opts) -- goto previous diagnostic | |
vim.keymap.set('n', 'g]', vim.diagnostic.goto_next, opts) -- goto next diagnostic | |
vim.keymap.set('n', 'gl', vim.diagnostic.open_float, opts) -- goto line diagnostic message | |
-- Buffer keymaps. | |
vim.keymap.set('n', '<leader>bp', ':bp<cr>', opts) -- focus previous buffer | |
vim.keymap.set('n', '<leader>bn', ':bn<cr>', opts) -- focus next buffer | |
vim.keymap.set('n', '<leader>bm', ':bm<cr>', opts) -- focus modified buffer | |
vim.keymap.set('n', '<leader>bd', ':bd<cr>', opts) -- delete buffer | |
-- Window keymaps. | |
vim.keymap.set('n', '<leader>wh', '<c-w>h', opts) -- move to left window | |
vim.keymap.set('n', '<leader>wj', '<c-w>j', opts) -- move to below window | |
vim.keymap.set('n', '<leader>wk', '<c-w>k', opts) -- move to above window | |
vim.keymap.set('n', '<leader>wl', '<c-w>l', opts) -- move to right window | |
vim.keymap.set('n', '<leader>wr', '<c-w>r', opts) -- swap split positions | |
vim.keymap.set('n', '<leader>ws', '<c-w>s', opts) -- open horizontal split | |
vim.keymap.set('n', '<leader>wv', '<c-w>v', opts) -- open vertical split | |
vim.keymap.set('n', '<leader>ww', '<c-w>w', opts) -- focus next window | |
vim.keymap.set('n', '<leader>wc', '<c-w>c', opts) -- close current window | |
vim.keymap.set('n', '<leader>wo', ':only<cr>', opts) -- close other windows | |
vim.keymap.set( | |
'n', | |
'<leader>f', | |
':Telescope find_files find_command=fd,-t,f,-H,-E,.git,--strip-cwd-prefix theme=dropdown previewer=false<cr>', | |
opts | |
) | |
vim.keymap.set('n', '<leader>/', ':Telescope live_grep<cr>', opts) | |
vim.keymap.set('n', '<leader>p', ':Telescope commands theme=dropdown<cr>', opts) | |
vim.keymap.set('n', '<leader>e', ':NvimTreeFindFileToggle<cr>', opts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment