Skip to content

Commit 2e4aa41

Browse files
Fix bug for function altFileOrOldFile
Closes #11
1 parent 5eb174d commit 2e4aa41

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

.config/nvim/lua/config/functions.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,21 @@ function M.gitBlameOpenCommitFileURL()
124124
end
125125

126126
-- alternate-file or last edited file
127-
-- `CTRL-^` for alternate-file
128-
-- `'0` for last edited file
129-
-- BUG: sometimes does not work when opening last edited file
130127
function M.altFileOrOldFile()
131128
local status_ok, _ = pcall(vim.cmd, 'e#')
132129
if not status_ok then
133-
vim.cmd([[norm '0]])
134-
local current_buf_name = vim.api.nvim_buf_get_name(0)
135-
vim.cmd('bp')
136-
local prev_current_buf_name = vim.api.nvim_buf_get_name(0)
137-
if prev_current_buf_name == '' then
138-
vim.cmd('bd')
139-
elseif current_buf_name == prev_current_buf_name then
140-
vim.cmd('e#<2')
130+
-- No alternate file, try oldfiles
131+
local oldfiles = vim.v.oldfiles
132+
local current_file = vim.api.nvim_buf_get_name(0)
133+
-- Find first oldfile that's not the current file
134+
for _, old_file in ipairs(oldfiles) do
135+
if old_file ~= current_file and vim.fn.filereadable(old_file) == 1 then
136+
vim.cmd('edit ' .. vim.fn.fnameescape(old_file))
137+
return
138+
end
141139
end
140+
-- If no oldfile found, just notify
141+
vim.notify('No previous file to open', vim.log.levels.INFO)
142142
end
143143
end
144144

0 commit comments

Comments
 (0)