forked from NotAShelf/nvf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
104 lines (81 loc) · 3.66 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
vim.api.nvim_create_augroup("jump_last_position", { clear = true })
vim.api.nvim_create_autocmd("BufReadPost", {
group = "jump_last_position",
callback = function()
local last_pos = vim.fn.line([['"]])
local last_line = vim.fn.line("$")
if last_pos > 0 and last_pos <= last_line then
vim.cmd([[normal! g'"]])
end
end,
})
-- Remap splits navigation to just CTRL + hjkl
vim.keymap.set("n", "<C-h>", "<C-w>h", { noremap = true, silent = true })
vim.keymap.set("n", "<C-j>", "<C-w>j", { noremap = true, silent = true })
vim.keymap.set("n", "<C-k>", "<C-w>k", { noremap = true, silent = true })
vim.keymap.set("n", "<C-l>", "<C-w>l", { noremap = true, silent = true })
-- Make adjusting split sizes a bit more friendly
vim.keymap.set("n", "<C-Left>", ":vertical resize +3<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<C-Right>", ":vertical resize -3<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<C-Up>", ":resize +3<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<C-Down>", ":resize -3<CR>", { noremap = true, silent = true })
vim.opt.autoread = true
vim.api.nvim_create_autocmd({ "CursorHold", "FocusGained" }, {
pattern = "*",
callback = function()
vim.cmd("checktime")
end,
})
vim.opt.autowriteall = true
-- Map Ctrl+j to Esc in various modes
local modes = { "n", "i", "v", "s", "x", "o", "l" }
for _, mode in ipairs(modes) do
vim.keymap.set(mode, "<C-j>", "<Esc>", { noremap = true, silent = true })
end
-- Map Ctrl+j to Ctrl+c in command-line mode
vim.keymap.set("c", "<C-j>", "<C-c>", { noremap = true, silent = true })
-- Map Ctrl+j to exit terminal mode
vim.keymap.set("t", "<C-j>", "<C-\\><C-n>", { noremap = true, silent = true })
-- Map Ctrl+Shift+j (Ctrl-S-j) to Esc in various modes
for _, mode in ipairs(modes) do
vim.keymap.set(mode, "<C-S-j>", "<Esc>", { noremap = true, silent = true })
end
-- Map Ctrl+Shift+j to Ctrl+c in command-line mode
vim.keymap.set("c", "<C-S-j>", "<C-c>", { noremap = true, silent = true })
-- Map Ctrl+Shift+j to exit terminal mode
vim.keymap.set("t", "<C-S-j>", "<C-\\><C-n>", { noremap = true, silent = true })
-- Map Ctrl+k to Esc in various modes
for _, mode in ipairs(modes) do
vim.keymap.set(mode, "<C-k>", "<Esc>", { noremap = true, silent = true })
end
-- Map Ctrl+k to Ctrl+c in command-line mode
vim.keymap.set("c", "<C-k>", "<C-c>", { noremap = true, silent = true })
-- Map Ctrl+k to exit terminal mode
vim.keymap.set("t", "<C-k>", "<C-\\><C-n>", { noremap = true, silent = true })
-- Map Ctrl+F to toggle NvimTree
vim.keymap.set("n", "<C-F>", ":NvimTreeToggle<CR>", { noremap = true, silent = true })
for _, mode in ipairs(modes) do
vim.keymap.set(mode, "<C-e>", "<Cmd>wq<CR>", { noremap = true, silent = true })
end
-- Array of file names indicating root directory. Modify to your liking.
local root_names = { '.git', 'Makefile', 'CMakeList.txt', 'flake.nix', 'package.json', 'Cargo.lock'}
-- Cache to use for speed up (at cost of possibly outdated results)
local root_cache = {}
local set_root = function()
-- Get directory path to start search from
local path = vim.api.nvim_buf_get_name(0)
if path == '' then return end
path = vim.fs.dirname(path)
-- Try cache and resort to searching upward for root directory
local root = root_cache[path]
if root == nil then
local root_file = vim.fs.find(root_names, { path = path, upward = true })[1]
if root_file == nil then return end
root = vim.fs.dirname(root_file)
root_cache[path] = root
end
-- Set current directory
vim.fn.chdir(root)
end
local root_augroup = vim.api.nvim_create_augroup('MyAutoRoot', {})
vim.api.nvim_create_autocmd('BufEnter', { group = root_augroup, callback = set_root })