Skip to content

Instantly share code, notes, and snippets.

@coffebar
Last active December 5, 2022 20:27
Show Gist options
  • Save coffebar/6c09c75d5dafcf1d76cc1079e140939c to your computer and use it in GitHub Desktop.
Save coffebar/6c09c75d5dafcf1d76cc1079e140939c to your computer and use it in GitHub Desktop.
Neovim project-related lua config
-- Local project-level config for Neovim
-- Based on https://gist.github.com/coffebar/6c09c75d5dafcf1d76cc1079e140939c
local augroup_name = "tmp_local_group"
local augroup = vim.api.nvim_create_augroup(augroup_name, { clear = true })
local unbind_table = {}
local function bind(op, outer_opts)
outer_opts = outer_opts or { noremap = true }
return function(lhs, rhs, opts)
opts = vim.tbl_extend("force", outer_opts, opts or {})
vim.keymap.set(op, lhs, rhs, opts)
table.insert(unbind_table, {
op = op,
lhs = lhs,
})
end
end
vim.api.nvim_create_autocmd("DirChangedPre", {
group = augroup,
-- Undo all changes
callback = function()
vim.api.nvim_del_augroup_by_name(augroup_name)
-- undo keymaps
for _, b in ipairs(unbind_table) do
vim.keymap.set(b.op, b.lhs, "<nop>", { noremap = true })
end
end,
})
-- Setup
local nnoremap = bind("n")
-- local vnoremap = bind("v")
-- Add maps
nnoremap("<leader>ex", ":w!<cr>")
-- Add this to your nvim config
local augroup = vim.api.nvim_create_augroup("user_cmds", { clear = true })
vim.api.nvim_create_autocmd("DirChanged", {
group = augroup,
desc = "Source local nvim config",
callback = function()
local rc = ".vimrc.lua"
if vim.fn.filereadable(rc) == 1 then
local cwd = vim.fn.getcwd()
local confirm = "Do you trust " .. cwd .. "/" .. rc .. "?"
if vim.fn.confirm(confirm, "Yes\nNo") == 1 then
vim.api.nvim_command("source .vimrc.lua")
end
end
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment