@@ -30,24 +30,6 @@ autocmd("FileType", {
3030 end ,
3131})
3232
33- -- Go to last loc when opening a buffer
34- autocmd (" BufReadPost" , {
35- group = augroup (" last-loc" ),
36- callback = function (event )
37- local exclude = { " gitcommit" }
38- local buf = event .buf
39- if vim .tbl_contains (exclude , vim .bo [buf ].filetype ) or vim .b [buf ].dotvim_last_loc then
40- return
41- end
42- vim .b [buf ].dotvim_last_loc = true
43- local mark = vim .api .nvim_buf_get_mark (buf , ' "' )
44- local lcount = vim .api .nvim_buf_line_count (buf )
45- if mark [1 ] > 0 and mark [1 ] <= lcount then
46- pcall (vim .api .nvim_win_set_cursor , 0 , mark )
47- end
48- end ,
49- })
50-
5133-- Highlight when yanking text
5234autocmd (" TextYankPost" , {
5335 desc = " Highlight when yanking text" ,
@@ -98,24 +80,70 @@ autocmd("BufWritePre", {
9880 command = " %sort" ,
9981})
10082
101- -- Auto change to git root when opening a file or switching buffer
102- vim .api .nvim_create_autocmd ({ " BufEnter" , " BufWinEnter" }, {
103- group = augroup (" autocd-to-gitroot" ),
104- callback = function ()
105- local filepath = vim .api .nvim_buf_get_name (0 )
106- if filepath == " " then
107- return
108- end -- skip unnamed buffers
109- local git_root = vim .fn .systemlist (" git -C " .. vim .fn .fnameescape (vim .fn .fnamemodify (filepath , " :p:h" )) .. " rev-parse --show-toplevel" )[1 ]
110- if vim .v .shell_error == 0 and git_root ~= nil and git_root ~= " " then
111- local current_dir = vim .fn .getcwd ()
112- if current_dir ~= git_root then
113- vim .cmd (" lcd " .. git_root )
114- -- print("Changed directory to " .. git_root)
115- end
116- end
117- end ,
118- })
83+ -- -- Go to last loc when opening a buffer (currently using mini.misc instead)
84+ -- autocmd("BufReadPost", {
85+ -- group = augroup("last-loc"),
86+ -- callback = function(event)
87+ -- local exclude = { "gitcommit" }
88+ -- local buf = event.buf
89+ -- if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].dotvim_last_loc then
90+ -- return
91+ -- end
92+ -- vim.b[buf].dotvim_last_loc = true
93+ -- local mark = vim.api.nvim_buf_get_mark(buf, '"')
94+ -- local lcount = vim.api.nvim_buf_line_count(buf)
95+ -- if mark[1] > 0 and mark[1] <= lcount then
96+ -- pcall(vim.api.nvim_win_set_cursor, 0, mark)
97+ -- end
98+ -- end,
99+ -- })
100+
101+ -- -- Auto change to git root when opening a file or switching buffer (currently using mini.misc instead)
102+ -- vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
103+ -- group = augroup("autocd-to-gitroot"),
104+ -- callback = function()
105+ -- local filepath = vim.api.nvim_buf_get_name(0)
106+ -- if filepath == "" then
107+ -- return
108+ -- end -- skip unnamed buffers
109+ -- local git_root = vim.fn.systemlist("git -C " .. vim.fn.fnameescape(vim.fn.fnamemodify(filepath, ":p:h")) .. " rev-parse --show-toplevel")[1]
110+ -- if vim.v.shell_error == 0 and git_root ~= nil and git_root ~= "" then
111+ -- local current_dir = vim.fn.getcwd()
112+ -- if current_dir ~= git_root then
113+ -- vim.cmd("lcd " .. git_root)
114+ -- -- print("Changed directory to " .. git_root)
115+ -- end
116+ -- end
117+ -- end,
118+ -- })
119+ -- -- https://www.reddit.com/r/neovim/comments/zy5s0l/you_dont_need_vimrooter_usually_or_how_to_set_up/
120+ -- -- Array of file names indicating root directory. Modify to your liking.
121+ -- local root_names = { ".git", "Makefile" }
122+ -- -- Cache to use for speed up (at cost of possibly outdated results)
123+ -- local root_cache = {}
124+ -- vim.api.nvim_create_autocmd("BufEnter", {
125+ -- group = augroup("autocd-to-gitroot"),
126+ -- callback = function()
127+ -- -- Get directory path to start search from
128+ -- local path = vim.api.nvim_buf_get_name(0)
129+ -- if path == "" then
130+ -- return
131+ -- end
132+ -- path = vim.fs.dirname(path)
133+ -- -- Try cache and resort to searching upward for root directory
134+ -- local root = root_cache[path]
135+ -- if root == nil then
136+ -- local root_file = vim.fs.find(root_names, { path = path, upward = true })[1]
137+ -- if root_file == nil then
138+ -- return
139+ -- end
140+ -- root = vim.fs.dirname(root_file)
141+ -- root_cache[path] = root
142+ -- end
143+ -- -- Set current directory
144+ -- vim.fn.chdir(root)
145+ -- end,
146+ -- })
119147
120148-- Close fugitive buffers when navigating
121149-- Probably not needed any more but leaving it here for reference
0 commit comments