Vim Tips Wiki
Advertisement

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created April 20, 2011 · complexity basic · author Nickboldt · version 7.0

After weeks of manually updating XML files' timestamps with the current number of milliseconds past the epoch, I finally snapped and went looking for a better way.

Rather than shell out from vim to run date +%s000 to get a current timestamp, then copy and paste that new string into a :%s/oldstamp/newstamp/ replacement, this script does it with three keystrokes and no mouse-based copy/paste.

" Add this to your ~/.vimrc file, then type '\ts' to update timestamp to current.
fun! ReplaceTimestamp()
  let tstamp = strftime("%s000")
  %s#<property name='p2.timestamp' value='\zs\d\+\ze'/>#\=tstamp#g
  echo "New time: " . tstamp
endfun
nnoremap <Leader>ts :call ReplaceTimestamp()<CR>

Comments

Thanks for the tip. Later I will insert a "new tip" header, and we will get around to discussing the tip at 201104, but here are some initial thoughts.

Relevant tips are:

The tip needs a couple of things:

  • Briefly explain what it does to someone who has never seen 'p2.timestamp' (readers shouldn't have to spend time decoding what the tip does).
  • Provide a short example (probably just one line?) of text that would be changed.
  • Quickly state what strftime('%s000') is, and mention that '%s' needs a Unix-based system (I think).

In the code, you don't need execute because substitute can replace with an expression. This should work:

  %s#<property name='p2.timestamp' value='\zs\d\+\ze'/>#\=tstamp#g

JohnBeckett 04:11, April 20, 2011 (UTC)

Per suggestions I've stripped the irrelevant bits from the example. The full version can be seen here on my blog. --Nickboldt 14:40, April 20, 2011 (UTC)
Thanks, but perhaps my suggestion was not clear. At any rate, I have replaced your change in the script (which would change all numbers between apostrophes) with my statement, showing that no "exe" is needed. Later, I will probably add a little more explanation and remove these comments which by then won't be needed. JohnBeckett 05:00, April 21, 2011 (UTC)
Advertisement