0

I use the following code

function! LastModified()
  if &modified
    let save_cursor = getpos(".")
    let n = min([6, line("$")])
    keepjumps execute '"1," . n . "s#^% Modified:\zs.*# " . strftime("%H:%M %A, %-d %B %Y") . "#"'
    call histdel('search', -2)
    keepjumps call setpos('.', save_cursor)
  endif
endfun

autocmd BufWritePre *.tex call LastModified()

to automatically add a timestamp to latex files. The top of my tex contain lines like

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title:    ...some title...
% Author:   ...author...
% Modified:  8:31 Monday, 19 Mar 2018
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

so the date at the end of the Modified line should be updated to the current date and time. This used to work fine but for reasons I don't understand it stopped working a month or so ago. Can anyone see what the problem is? I am using:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Feb 18 2018 06:43:13) macOS version Included patches: 1-1522 Compiled by Homebrew

  • 1
    When reading :h :keepjumps I noticed the following line: "Also, for ":keepjumps exe 'command '" the "command" won't keep jumps. Instead use: ":exe 'keepjumps command'"." You might want to change line 5 to make keepjumps work. – Octaviour Mar 20 '18 at 10:23
1

The quoting of your execute statement looks wrong. You don't need to quote the entire thing, just the strings.

Try this:

keepjumps exec ':1,' . n . 's#^% Modified:\zs.*# ' . strftime('%H:%M %A, %d %B %Y') . '#'

I'm using single-quotes to ensure no unexpected expansions occur.

Plus, the strftime parameter -%d also doesn't look right. (And, indeed, I get an error). I removed the - here.

Example result:

% Modified: 22:27 Sunday, 18 March 2018

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.