I have a function to change the file timestamp, I was been using the :s
for substitution but the substitute()
function does not mess with jumps and search patterns, making it better swited for the task, so until now I have the following:
--> :lua changeheader()
-- This function is called with the BufWritePre event (autocmd)
-- and when I want to save a file I use ":update" which
-- only writes a buffer if it was modified
M.changeheader = function()
-- We only can run this function if the file is modifiable
local bufnr = vim.api.nvim_get_current_buf()
if not vim.api.nvim_buf_get_option(bufnr, "modifiable") then
require("notify")("Current file not modifiable!")
return
end
-- if not vim.api.nvim_buf_get_option(bufnr, "modified") then
-- require("notify")("Current file has not changed!")
-- return
-- end
if vim.fn.line("$") >= 7 then
os.setlocale("en_US.UTF-8") -- show Sun instead of dom (portuguese)
local time = os.date("%a, %d %b %Y %H:%M:%S")
local l = 1
while l <= 7 do
vim.fn.setline(l, vim.fn.substitute(vim.fn.getline(l), 'last change: \\zs.*', time , 'gc'))
l = l + 1
end
require("notify")("Changed file header!")
end
end
This function is defined in a file called utils.lua that has at the beginning an empty table where we define the local modules:
local M = {}
At the end of the same file we have:
return M
This way we can call the function in this fashion:
lua require("core.utils").changeheader()<CR>