1. 16
  1.  

  2. 1

    That’s too simple for a plugin? I’m definitely not going to write all that code. If left-pad is a npm package, that can definitely be a vim plugin.

    1. 1

      Have you considered using reltime()? Then you could just do

      g:time_stamp_start = reltime()
      
      " ...
      
      function! TimeStamp()
           let l:second_offset = reltimefloat(g:time_stamp_start, reltime())```
      
      1. 2

        I actually didn’t know about reltime(), so thanks for pointing that out.

        However, I think it would add complexity here, rather than remove it. It looks like it would give me better-than-second level precision, so I’d need some code to deal with that extra precision (in my case, just throwing it away) and I think that would more than make up for the savings in terms of computing the relative time.

        Still glad I learned about reltime(), though—vim is full of surprises.

      2. 1

        Assigning to a:current_time is kind of a strange thing to do. The a: space is intended for function arguments. I’m actually surprised Vim allows writing to this at all! It doesn’t work when you’re actually using it as arguments:

        :fun! Test(arg)
        :  let a:arg = 'zzzz'
        :endfun
        
        :call Test('zxc')
        E46: Cannot change read-only variable "a:arg"
        

        In :help a:var it’s mentioned that “The a: scope and the variables in it cannot be changed, they are fixed”, so this actually sounds like a bug(?)

        Using local scope l:current_time is much more standard (you don’t actually need a variable there, you can \<C-r>=strftime(..) directly).

        You may also want to consider making g:time_stamp_enabled a buffer-local variable, instead of a global one. That way enabling the timestamp insertion will only work on the current buffer, and not that other Python buffer you have or whatnot.

        1. 2

          Assigning to a:current_time is kind of a strange thing to do. The a: space is intended for function arguments.

          Thanks—I didn’t realize that (I’m definitely not a vimscript expert!). I’ll update the code accordingly.