Open Neogit of the current buffer #1424
Answered
by
mangelozzi
mangelozzi
asked this question in
Q&A
-
The below snippers moves the cursor to the current buffer when neogit is opened, it also expands the file. If you dont like just remove the Adding this here because I could not find something similar in the Q&A so I made this. Could probably be done much easier if this feature was built in, but here's something that works: local function open_neogit_on_current_buffer()
local function cursor_to_line(pattern)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local esccaped_pattern = string.gsub(pattern, "[%p]", "%%%1")
for i, line in ipairs(lines) do
if line:match(esccaped_pattern) then
vim.api.nvim_win_set_cursor(0, {i, 0}) -- If pattern is found, move the cursor to the matching line
return
end
end
end
local function open_callback(augroup_id, file_rel)
vim.api.nvim_del_augroup_by_id(augroup_id)
-- Send a tab to open the file
local keys = vim.api.nvim_replace_termcodes('<tab>',true,false,true)
vim.api.nvim_feedkeys(keys,'i',false) -- Is insert mode!!
cursor_to_line(file_rel)
end
local filename = vim.api.nvim_buf_get_name(0)
if (filename ~= "") then
local file_rel = vim.fn.fnamemodify(vim.fn.expand('%'), ':.')
-- local escaped_file = vim.fn.escape(file_rel, '\\/.*$^~[]')
local MyNeogitGroup = vim.api.nvim_create_augroup("MyNeogitGroup", {clear = true})
vim.api.nvim_create_autocmd(
"User",
{
desc = "A temp autocmd to open neogit on current buffer",
pattern = "NeogitStatusRefreshed",
group = MyNeogitGroup,
callback = function()
open_callback(MyNeogitGroup, file_rel)
end
}
)
end
require('neogit').open({kind = 'tab'})
end
-- Use <leader>g as a prefix for bunch of other git related commands, this keep fast
vim.keymap.set("n", "<leader>i", function() open_neogit_on_current_buffer() end, {noremap = true, desc = "Neogit buffer"})
vim.keymap.set("n", "<leader>I", function() require('neogit').open({ kind = 'tab' }) end, {noremap = true, desc = "Neogit"}) |
Beta Was this translation helpful? Give feedback.
Answered by
mangelozzi
Jul 18, 2024
Replies: 1 comment
-
Answered above |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mangelozzi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answered above