Vim结构时间计算



我有一个非常简单的ToDo文件,看起来像这样:

130821 Go to the dentist
130824 Ask a question to StackOverflow
130827 Read the Vim Manual
130905 Stop reading the Vim Manual

每次打开文件时,我都想计算到不同到期日(今天是2013年8月22日,即130822日,在巴黎)之前的剩余天数,从而获得如下信息:

130821 -1 Go to the dentist
130824 2 Ask a question to StackOverflow
130827 5 Read the Vim Manual
130905 14 Stop reading the Vim Manual

但我不知道如何实现这一点(我也不知道这是否合理可行:参见glts'comment)

非常感谢您的帮助。

此命令将执行所需的替换,但计算错误(无法正常工作):

%s#v^(d{6})( -?d+)?#=submatch(1).' '.(submatch(1)-strftime("%y%m%d"))

请参阅:help sub-replace-expression、:help submatch()、:help strftime()。

请注意,我使用v将Vim的正则表达式解析器置于"非常神奇"的模式。

无论何时使用BufReadPost autocmd加载文件,都可以轻松地应用此功能。

类似于:

augroup TODO_DATE_CALC
au!
au BufReadPost myToDoFileName %s#v^(d{6})( -?d+)?#=submatch(1).' '.(submatch(1)-strftime("%y%m%d"))
augroup END

找出自unix epoch以来某个日期时间的时间?显示了如何获取特定日期的unix时间,您可以使用Vim中的system()函数来获取结果。但我目前还没有一个系统来测试这一点。我想你可能运气不好,在Windows上。

除非您可以更改文件格式以包含unix时间。。。那么这应该相当容易。

虽然被他们说服了,但我对问题的答案有点失望。我试图找到一个解决方案,似乎我几乎成功了。不用说,这是一个笨拙的装置,但它确实有效。

首先,文件(出于测试目的稍作修改):

130825 Past ToDo test 
130827 Today's ToDo test 
130829 In two days ToDo test 
130831 Another test 
130902 Change of month ToDo test 
131025 Another change of month test 

第二http://www.epochconverter.com:

1 day                   = 86400 seconds
1 month (30.44 days)    = 2629743 seconds
1 year (365.24 days)    = 31556926 seconds

第三,我修改的功能:

function! DaysLeft()
  :normal! gg
  let linenr = 1
  while linenr <= line("$")
  let linenr += 1
  let line = getline(linenr)
  :normal! 0"ayiw
  :.s/((dd))(dd)(dd)>/1
  :normal! 0"byiw
  :execute "normal! diw0i<C-R>a"
  :normal! 0"ayiw
  :.s/(dd)((dd))(dd)>/2
  :normal! 0"cyiw
  :execute "normal! diw0i<C-R>a"
  :normal! 0"ayiw
  :.s/(dd)(dd)((dd))>/3
  :normal! 0"dyiw
  :execute "normal! diw0i<C-R>a"
  let @l = strftime("%s")
  :execute "normal! 0wi<C-R>=((<C-R>b+30)*31556926+(<C-R>c-1)*2629743+(<C-    R>d-1)*86400+1-<C-R>l)/86400<Enter><tab>"
  exe linenr
  endwhile
  endfunction

第四,结果:

130825 -2   Past ToDo test
130827 0    Today's ToDo test
130829 1    In two days ToDo test
130831 3    Another test
130902 5    Change of month ToDo test
131025 58   Another change of month test

正如你所看到的,有一个小故障:130829 ToDo显示为1天,而不是2天(因为我没有做浮点计算)。但事实上,我认为这是一个编程故障(除其他外……),但心理上是健全的:事实上我只有一整天的工作可用。

这可能是一个徒劳的练习,但这让我学习了:捕获、循环、寄存器,当然还有以前的StackOverflow宝贵的答案,以便给出一个纯粹的Vim答案。

谢谢你对我的回答所做的一切改进。

最新更新