Replies: 1 comment
-
Thanks for sharing! local function next_daily(step)
local buf_path = vim.api.nvim_buf_get_name(0)
if not buf_path or buf_path == '' then
vim.notify('buffer has no path', vim.log.levels.INFO)
return
end
local buf_dir = vim.fn.fnamemodify(buf_path, ':p:h')
local journal_dir = vim.fn.fnamemodify('~/notes/daily', ':p:h')
if buf_dir ~= journal_dir then
vim.notify('not a daily buffer', vim.log.levels.INFO)
return
end
local buf_file = vim.fn.fnamemodify(buf_path, ':t:r')
local year, month, day = buf_file:match('(%d+)%-(%d+)%-(%d+)')
if not year or not month or not day then
vim.notify('unexpected daily file format: ' .. buf_path, vim.log.levels.INFO)
return
end
local curr_date = os.time({ year = year, month = month, day = day })
for i = 1, 30 do -- only look for files within a month
local next_date = os.date('%Y-%m-%d', curr_date + i * step * 24 * 3600)
local next_file = journal_dir .. '/' .. next_date .. '.md'
if vim.loop.fs_stat(next_file) then
vim.cmd('edit ' .. next_file)
return
end
end
vim.notify('no more close daily files', vim.log.levels.INFO)
end
vim.keymap.set('n', '<leader>j;', function()
next_daily(1)
end, { desc = 'Open next daily file' })
vim.keymap.set('n', '<leader>j,', function()
next_daily(-1)
end, { desc = 'Open previous daily file' }) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have added 2 of my own commands that switch to the previous/next daily note based on the currently open note in buffer.
The only issue is that there is no support for custom date formats, so the commands only work on filename dates with the default
%Y-%m-%d
format.A PR could be started if custom date parsing is implemented.
Beta Was this translation helpful? Give feedback.
All reactions