Skip to content

Instantly share code, notes, and snippets.

@creativenull
Last active March 21, 2021 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creativenull/2be0b11bea95651cc66bad3b03a810fa to your computer and use it in GitHub Desktop.
Save creativenull/2be0b11bea95651cc66bad3b03a810fa to your computer and use it in GitHub Desktop.
My neovim config in lua - single file
-- =============================================================================
-- = Utils/Functions =
-- =============================================================================
-- Global option
local o = vim.o
-- Window option
local wo = vim.wo
-- One line execution, like :h execute
local cmd = vim.cmd
-- Global keymap
local function keymap(mode, lhs, rhs, opts)
local set_keymap, extend, default_opts = vim.api.nvim_set_keymap, vim.tbl_extend, { noremap = true, silent = true }
if opts == nil then set_keymap(mode, lhs, rhs, default_opts)
else set_keymap(mode, lhs, rhs, extend('force', default_opts, opts)) end
end
-- Buffer keymap
local function buf_keymap(mode, lhs, rhs, opts)
local set_keymap, extend, default_opts = vim.api.nvim_buf_set_keymap, vim.tbl_extend, { noremap = true, silent = true }
if opts == nil then set_keymap(mode, lhs, rhs, default_opts)
else set_keymap(mode, lhs, rhs, extend('force', default_opts, opts)) end
end
-- Auto group
local function set_augroup(name, cmds)
local aucmds = 'augroup ' .. name .. ' au! '
for k, _ in pairs(cmds) do
aucmds = aucmds .. k .. ' '
end
aucmds = aucmds .. 'augroup end'
vim.api.nvim_exec(aucmds, false)
end
-- =============================================================================
-- = Auto Commands =
-- =============================================================================
set_augroup('disable_char_list', { 'au FileType help lua vim.bo.list = false' })
-- =============================================================================
-- = Plugins =
-- =============================================================================
-- Shamelessly copied from https://github.com/wbthomason/packer.nvim#bootstrapping
local install_path = vim.fn.stdpath('data') .. '/site/pack/paqs/opt/paq-nvim'
if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
cmd('!git clone https://github.com/savq/paq-nvim ' .. install_path)
end
cmd 'packadd paq-nvim'
local paq = require 'paq-nvim'.paq
paq { 'savq/paq-nvim', opt = true }
-- Core
paq 'nvim-lua/plenary.nvim'
paq 'lewis6991/gitsigns.nvim'
paq 'tpope/vim-surround'
paq 'SirVer/ultisnips'
paq 'Shougo/context_filetype.vim'
paq 'tyru/caw.vim'
paq 'editorconfig/editorconfig-vim'
-- LSP
paq 'nvim-lua/lsp-status.nvim'
paq 'hrsh7th/nvim-compe'
paq 'neovim/nvim-lspconfig'
paq 'glepnir/lspsaga.nvim'
-- Theming and Looks
paq {
'nvim-treesitter/nvim-treesitter',
run = function() cmd 'TSUpdate' end
}
paq 'srcery-colors/srcery-vim'
paq 'norcalli/nvim-colorizer.lua'
paq 'sheerun/vim-polyglot'
-- =============================================================================
-- = Theming and Looks =
-- =============================================================================
wo.number = true
wo.relativenumber = true
o.termguicolors = true
cmd 'colorscheme srcery'
-- =============================================================================
-- = Options =
-- =============================================================================
-- Completion options
-- Do not re-append when sourcing the file
if string.match(o.shortmess, 'c') == nil then o.shortmess = o.shortmess .. 'c' end
o.completeopt = 'menuone,noinsert,noselect'
o.updatetime = 100
-- Search options
o.ignorecase = true
o.smartcase = true
o.wrapscan = true
-- Indent options
o.tabstop = 4
o.shiftwidth = 0
o.softtabstop = 4
o.expandtab = true
o.smartindent = true
-- Line options
o.showmatch = true
o.showbreak = '+++'
o.textwidth = 120
o.scrolloff = 5
wo.linebreak = true
wo.colorcolumn = '120' -- some prefer 80, I just like to break the rules :)
-- Move swapfiles and backupfiles into ~/.cache
o.swapfile = true
o.directory = os.getenv('HOME') .. '/.cache/nvim'
o.backup = true
o.backupdir = os.getenv('HOME') .. '/.cache/nvim'
-- Undo even after closing vim
o.undofile = true
o.undodir = os.getenv('HOME') .. '/.cache/nvim'
o.undolevels = 10000
o.history = 10000
-- Enable a little bit performance
o.lazyredraw = true
-- Buffers/Tabs/Windows
o.hidden = true
-- Set spelling
o.spell = false
-- For git
wo.signcolumn = 'yes'
-- Mouse support
o.mouse = 'a'
-- Backspace behaviour
o.backspace = 'indent,eol,start'
-- Status line
o.showmode = false
-- Better display
o.cmdheight = 2
-- =============================================================================
-- = Keybindings =
-- =============================================================================
-- Leader key
vim.g.mapleader = ' '
-- Unbind default bindings for arrow keys, trust me this is for your own good
keymap('v', '<up>', '<nop>')
keymap('v', '<down>', '<nop>')
keymap('v', '<left>', '<nop>')
keymap('v', '<right>', '<nop>')
keymap('i', '<up>', '<nop>')
keymap('i', '<down>', '<nop>')
keymap('i', '<left>', '<nop>')
keymap('i', '<right>', '<nop>')
-- Map Esc, to perform quick switching between Normal and Insert mode
keymap('i', 'jk', '<ESC>')
-- Map escape from terminal input to Normal mode
keymap('i', '<ESC>', [[<C-\><C-n>]])
keymap('i', '<C-[>', [[<C-\><C-n>]])
-- Open built-in file expolorer
keymap('n', '<F3>', ':Ex<CR>')
-- Manual completion
keymap('i', '<C-Space>', '<C-x><C-o>')
-- Disable highlights
keymap('n', '<leader><CR>', ':noh<CR>')
-- Buffer maps
-- You can remap them to any other keys
keymap('n', '<leader>bl', ':buffers<CR>')
-- Go to next buffer
keymap('n', '<C-l>', ':bnext<CR>')
-- Go to previous buffer
keymap('n', '<C-h>', ':bprevious<CR>')
-- Close the current buffer
keymap('n', '<leader>bd', ':bp<BAR>sp<BAR>bn<BAR>bd<CR>')
-- Resize window panes, we can use those arrow keys
-- to help use resize windows - at least we give them some purpose
keymap('n', '<up>', ':resize +2<CR>')
keymap('n', '<down>', ':resize -2<CR>')
keymap('n', '<left>', ':vertical resize -2<CR>')
keymap('n', '<right>', ':vertical resize +2<CR>')
-- Move a line of text Alt+[j/k]
keymap('n', '<M-j>', [[mz:m+<CR>`z]])
keymap('n', '<M-k>', [[mz:m-2<CR>`z]])
keymap('v', '<M-j>', [[:m'>+<CR>`<my`>mzgv`yo`z]])
keymap('v', '<M-k>', [[:m'<-2<CR>`>my`<mzgv`yo`z]])
-- Edit vimrc and gvimrc
keymap('n', '<leader>ve', ':e $MYVIMRC<CR>')
-- Source the vimrc to reflect changes
keymap('n', '<leader>vs', ':luafile $MYVIMRC<CR>')
-- Reload file
keymap('n', '<leader>r', ':e!<CR>')
-- =============================================================================
-- = Commands =
-- =============================================================================
cmd 'command! Config edit $MYVIMRC'
cmd 'command! ConfigReload luafile $MYVIMRC'
-- There are no such thing as mistakes, only happy accidents :)
cmd 'command! -nargs=* W w'
cmd 'command! -nargs=* Wq wq'
cmd 'command! -nargs=* WQ wq'
cmd 'command! -nargs=* Q q'
cmd 'command! -nargs=* Qa qa'
cmd 'command! -nargs=* QA qa'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment