Config-files/nvim/init.lua
Victor Lalle c7e8bf1f56 nvim
2023-06-04 15:19:51 +02:00

217 lines
7.5 KiB
Lua

-- Make local variables, instead of typing long commands over and over again.
local map = vim.api.nvim_set_keymap
local s = { silent = true }
local function autocmd(event, pattern, cmd)
vim.api.nvim_create_autocmd(event, {
pattern = pattern,
command = cmd,
})
end
--This function loads a table of options into vim.opt
local function load(x)
for name, value in pairs(x) do
vim.opt[name] = value
end
end
-- Set leader key to comma
vim.g.mapleader = ","
--Autoload function
if vim.fn.filereadable(vim.fn.system('echo -n "${XDG_CONFIG_HOME:-$HOME/.config}/nvim/autoload/plug.vim"')) == 0 then
print("Downloading junegunn/vim-plug to manage plugins...")
vim.fn.system('mkdir -p ${XDG_CONFIG_HOME:-$HOME/.config}/nvim/autoload/')
vim.fn.system('curl "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" > ${XDG_CONFIG_HOME:-$HOME/.config}/nvim/autoload/plug.vim')
autocmd("VimEnter", "*", "PlugInstall")
end
map('n', ',,', [[:keepp /<++><CR>ca<]], s)
map('i', ',,', '<esc>:keepp /<++><CR>ca<', s)
-- Call plugins
local Plug = vim.fn['plug#']
vim.call('plug#begin', (vim.fn['system']('echo -n "${XDG_CONFIG_HOME:-$HOME/.config}/nvim/plugged"')))
Plug 'tpope/vim-surround'
Plug 'preservim/nerdtree'
Plug 'junegunn/goyo.vim'
Plug 'jreybert/vimagit'
Plug 'vimwiki/vimwiki'
Plug 'vim-airline/vim-airline'
Plug 'tpope/vim-commentary'
Plug 'ap/vim-css-color'
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'neovim/nvim-lspconfig'
Plug 'williamboman/mason.nvim'
Plug 'williamboman/mason-lspconfig.nvim'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'L3MON4D3/LuaSnip'
Plug 'VonHeikemen/lsp-zero.nvim'
Plug 'zbirenbaum/copilot.lua'
Plug 'kchmck/vim-coffee-script'
vim.call('plug#end')
-- Set options
local basics = {
title = true,
background = "light",
go = "a",
hlsearch = false,
clipboard = "unnamedplus",
showmode = false,
ruler = false,
laststatus = 0,
showcmd = false,
--filetype plugin on | this is default in neovim
syntax = "on", -- this is default in neovim
encoding = "utf-8",
number = true,
relativenumber = true,
--Enable autocompletion:
wildmode = "longest,list,full",
splitright = true,
splitbelow = true
}
-- Some basics
vim.keymap.set("n", "c", '"_c', s)
--Disables automatic commenting on newline:
autocmd("FileType", "markdown", "setlocal formatoptions-=c formatoptions-=r formatoptions-=o")
-- Perform dot commands over visual blocks:
map("v", ".", ":normal .<CR>",s)
--Goyo plugin makes text more readable when writing prose:
map('n', '<leader>f', ":Goyo | set bg=light | set linebreak<CR>",s)
--Spell-check set to <leader>o, 'o' for 'orthography':
map('n', '<leader>o', ":setlocal spell! spelllang=en_us<CR>",s)
--nerdtree
map("n", "<leader>n", ":NERDTreeToggle<CR>",s)
autocmd("bufenter *", "*", "if (winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree()) | q | endif")
if vim.fn.has('nvim') == 1 then
vim.b.NERDTreeBookmarksFile = vim.fn.stdpath('data') .. '/NERDTreeBookmarks'
else
vim.b.NERDTreeBookmarksFile = '~/.vim/NERDTreeBookmarks'
end
-- vimling
map("n","<leader>d", ":call ToggleDeadKeys()<CR>",s)
map("i", "<leader>d", "<esc>:call ToggleDeadKeys()<CR>a",s)
map("n","<leader>i", ":call ToggleIPA()<CR>",s)
map("i", "<leader>i", "<esc>:call ToggleIPA()<CR>a",s)
map("n", "<leader>q", ":call ToggleProse()<CR>",s)
--Shortcutting split navigation, saving a keypress:
map("n", "<C-h>", "<C-w>h", s)
map("n", "<C-j>", "<C-w>j", s)
map("n", "<C-k>", "<C-w>k", s)
map("n", "<C-l>", "<C-w>l", s)
--Replace ex mode with gq
map("n", "Q", "gq", s)
--Check file in shellcheck:
map("n", "<leader>s", ":!clear && spellcheck -x %<CR>", s)
--Open my bibliography file in split
map("n", "leader>b", ":vsp<space>$BIB<CR>", s)
map("n", "leader>r", ":vsp<space>$REFER<CR>", s)
--Replace all in aliased to S
map('n', 'S', ":%s//g<Left><Left>", s)
--Compile document, be it groff/LaTeX/markdown/etc.
map("n", "<leader>c", ":w! |!compiler '%:p'<CR>", s)
--Open corresponding .pdf/.html or preview
map("n", "<leader>p", ":!opout '%:p'<CR>", s)
--Runs a script that cleans out tex build files whenever I close out of a .tex file.
autocmd("VimLeave", "*.tex", "!texclear %")
-- Ensure files are read as what I want:
map("n", "<leader>v", ":VimwikiIndex<CR>", s)
vim.g.vimwiki_ext2syntax = {
['.Rmd'] = 'markdown',
['.rmd'] = 'markdown',
['.md'] = 'markdown',
['.markdown'] = 'markdown',
['.mdown'] = 'markdown'
}
--Runs a script that cleans out tex build files whenever I close out of a .tex file.
vim.g.vimwiki_list = {
{
path = '~/.local/share/nvim/vimwiki',
syntax = 'markdown',
ext = '.md'
}
}
--Save file as sudo on files that require root permission
vim.api.nvim_command('cabbrev w!! execute "silent! write !sudo tee % >/dev/null" <bar> edit!')
--Enable markdown syntax for .md files
autocmd({"BufRead", "BufNewFile"}, {"/tmp/calcurse*","~/.calcurse/notes/*"}, "set filetype=markdown")
autocmd({"BufRead", "BufNewFile"}, {"*.ms", "*.me", "*.mom", "*.man"}, "set filetype=groff")
autocmd({"BufRead", "BufNewFile"}, {"*.tex"}, "set filetype=tex")
-- Enable Goyo by default for mutt writing
autocmd({"BufRead", "BufNewFile"}, {"/tmp/neomutt*"}, "let g:goyo_width=80")
autocmd({"BufRead", "BufNewFile"}, {"/tmp/neomutt*"}, "set bg=light")
autocmd({"BufRead", "BufNewFile"}, {"/tmp/neomutt*"}, "map ZZ :Goyo|x!<CR>")
autocmd({"BufRead", "BufNewFile"}, {"/tmp/neomutt*"}, "map ZQ :Goyo|q!<CR>")
--Automatically deletes all trailing whitespace and newlines at end of file on save. & reset cursor position
autocmd("BufWritePre", "*", "let currPos = getpos('.')")
autocmd("BufWritePre", "*", "%s/\\s\\+$//e")
autocmd("BufWritePre", "*", "%s/\\n\\+\\%$//e")
autocmd("BufWritePre", "*", "cal cursor(currPos[1], currPos[2])")
--When shortcut files are updated, renew bash and ranger configs with new material:
autocmd("BufWritePost", "bm-files,bm-dirs", "!shortcuts")
--Run xrdb whenever Xdefaults or Xresources are updated.
autocmd("BufWritePost", "Xresources,Xdefaults,xresources,xdefaults", "set filetype=xdefaults")
autocmd("BufWritePost", "Xresources,Xdefaults,xresources,xdefaults", "!xrdb %")
--Recompile dwmblocks on config edit.
autocmd("BufWritePost", "config.h", "!cd ~/.local/src/dwmblocks/; sudo make install && { killall -q dwmblocks;setsid -f dwmblocks }")
-- Auto compile dwm on config.h change
function compile_dwm_config()
local current_file = vim.fn.expand('%:p') -- get current file path
if current_file == os.getenv("HOME")..'/.local/src/dwm/config.h' then
os.execute('cd ~/.local/src/dwm && sudo make clean install')
end
end
autocmd("BufWritePost", "config.h", "lua compile_dwm_config()")
--Turns off highlighting on the bits of code that are changed, so the line that is changed is highlighted but the actual text that has changed stands out on the line and is readable.
if vim.opt.diff:get() then
vim.cmd('highlight! link DiffText MatchParen')
end
--Function for toggling the bottom statusbar:
local h = {hidden_all = 0}
function Toggle_hidden_all()
local hideOptions = {}
if h.hidden_all == 0 then
h.hidden_all = 1
hideOptions = {
showmode = false,
ruler = false,
laststatus = 0,
showcmd = false,
}
else
h.hidden_all = 0
hideOptions = {
showmode = true,
ruler = true,
laststatus = 2,
showcmd = true,
}
end
load(hideOptions)
end
vim.keymap.set('n', '<leader>h', function() Toggle_hidden_all() end, {silent = true, noremap = true})
-- load options
load(basics)
vim.cmd('silent! source ~/.config/nvim/shortcuts.vim')
require("victor")